| 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' => '1.5', 'square_root' => '1.22474487139159'], | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         ['name' => 'Test 2', 'price' => '3.5', 'square_root' => '1.87082869338697'], | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         ['name' => 'Test 3', 'price' => '10', 'square_root' => '3.16227766016838'], | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         ['name' => 'Test 4', 'price' => '2', 'square_root' => '1.4142135623731'], | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         ['name' => 'Test 5', 'price' => '1.28', 'square_root' => '1.13137084989848'], | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         ['name' => 'Test 6', 'price' => '-1.28', 'square_root' => null], | 
            
                                                                        
                            
            
                                    
            
            
                | 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('17.0', $result['sales']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         self::assertSame('2.83333333333333', $result['average']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $query = $this->_em->createQuery( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             'SELECT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                  ABS(product.price) as absolute, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                  SQRT(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($product['square_root'], $item['square_root']); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  |             self::assertSame(strlen($product['name']), $item['length']); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 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 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  |