Completed
Pull Request — 2.8.x (#7941)
by Luís
08:27
created

GH7941Product   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 8
c 1
b 0
f 0
dl 0
loc 23
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
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' => 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('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(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
69
            if ($product['square_root'] === null) {
70
                self::assertNull($item['square_root']);
71
72
                continue;
73
            }
74
75
            self::assertRegExp($product['square_root'], $item['square_root']);
76
        }
77
    }
78
}
79
80
/**
81
 * @Entity
82
 * @Table()
83
 */
84
class GH7941Product
85
{
86
    /**
87
     * @Id
88
     * @GeneratedValue
89
     * @Column(type="integer")
90
     */
91
    public $id;
92
93
    /** @Column(type="string") */
94
    public $name;
95
96
    /** @Column(type="decimal") */
97
    public $price;
98
99
    /** @Column(type="datetime_immutable") */
100
    public $createdAt;
101
102
    public function __construct(string $name, string $price)
103
    {
104
        $this->name      = $name;
105
        $this->price     = $price;
106
        $this->createdAt = new DateTimeImmutable();
107
    }
108
}
109