|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Tests\ORM\Functional\Ticket; |
| 6 | + |
| 7 | +use DateTimeImmutable; |
| 8 | +use Doctrine\Tests\OrmFunctionalTestCase; |
| 9 | +use function ltrim; |
| 10 | +use function strlen; |
| 11 | + |
| 12 | +/** @group GH7941 */ |
| 13 | +final class GH7941Test extends OrmFunctionalTestCase |
| 14 | +{ |
| 15 | + private const PRODUCTS = [ |
| 16 | + ['name' => 'Test 1', 'price' => '100', 'square_root' => '/^10(\.0+)?$/'], |
| 17 | + ['name' => 'Test 2', 'price' => '100', 'square_root' => '/^10(\.0+)?$/'], |
| 18 | + ['name' => 'Test 3', 'price' => '100', 'square_root' => '/^10(\.0+)?$/'], |
| 19 | + ['name' => 'Test 4', 'price' => '25', 'square_root' => '/^5(\.0+)?$/'], |
| 20 | + ['name' => 'Test 5', 'price' => '25', 'square_root' => '/^5(\.0+)?$/'], |
| 21 | + ['name' => 'Test 6', 'price' => '-25', 'square_root' => '/^5(\.0+)?$/'], |
| 22 | + ]; |
| 23 | + |
| 24 | + protected function setUp() : void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + $this->setUpEntitySchema([GH7941Product::class]); |
| 29 | + |
| 30 | + foreach (self::PRODUCTS as $product) { |
| 31 | + $this->_em->persist(new GH7941Product($product['name'], $product['price'])); |
| 32 | + } |
| 33 | + |
| 34 | + $this->_em->flush(); |
| 35 | + $this->_em->clear(); |
| 36 | + } |
| 37 | + |
| 38 | + /** @test */ |
| 39 | + public function typesShouldBeConvertedForDQLFunctions() : void |
| 40 | + { |
| 41 | + $query = $this->_em->createQuery( |
| 42 | + 'SELECT |
| 43 | + COUNT(product.id) as count, |
| 44 | + SUM(product.price) as sales, |
| 45 | + AVG(product.price) as average |
| 46 | + FROM ' . GH7941Product::class . ' product' |
| 47 | + ); |
| 48 | + |
| 49 | + $result = $query->getSingleResult(); |
| 50 | + |
| 51 | + self::assertSame(6, $result['count']); |
| 52 | + self::assertSame('325', $result['sales']); |
| 53 | + self::assertRegExp('/^54\.16+7$/', $result['average']); |
| 54 | + |
| 55 | + $query = $this->_em->createQuery( |
| 56 | + 'SELECT |
| 57 | + ABS(product.price) as absolute, |
| 58 | + SQRT(ABS(product.price)) as square_root, |
| 59 | + LENGTH(product.name) as length |
| 60 | + FROM ' . GH7941Product::class . ' product' |
| 61 | + ); |
| 62 | + |
| 63 | + foreach ($query->getResult() as $i => $item) { |
| 64 | + $product = self::PRODUCTS[$i]; |
| 65 | + |
| 66 | + self::assertSame(ltrim($product['price'], '-'), $item['absolute']); |
| 67 | + self::assertSame(strlen($product['name']), $item['length']); |
| 68 | + self::assertRegExp($product['square_root'], $item['square_root']); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * @Entity |
| 75 | + * @Table() |
| 76 | + */ |
| 77 | +class GH7941Product |
| 78 | +{ |
| 79 | + /** |
| 80 | + * @Id |
| 81 | + * @GeneratedValue |
| 82 | + * @Column(type="integer") |
| 83 | + */ |
| 84 | + public $id; |
| 85 | + |
| 86 | + /** @Column(type="string") */ |
| 87 | + public $name; |
| 88 | + |
| 89 | + /** @Column(type="decimal") */ |
| 90 | + public $price; |
| 91 | + |
| 92 | + /** @Column(type="datetime_immutable") */ |
| 93 | + public $createdAt; |
| 94 | + |
| 95 | + public function __construct(string $name, string $price) |
| 96 | + { |
| 97 | + $this->name = $name; |
| 98 | + $this->price = $price; |
| 99 | + $this->createdAt = new DateTimeImmutable(); |
| 100 | + } |
| 101 | +} |
0 commit comments