Completed
Pull Request — master (#6417)
by Luís
11:31
created

GH2947Car::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Common\Cache\ArrayCache;
6
use Doctrine\Tests\OrmFunctionalTestCase;
7
8
/**
9
 * @group 2947
10
 */
11
class GH2947Test extends OrmFunctionalTestCase
12
{
13
    protected function setUp()
14
    {
15
        $this->resultCacheImpl = new ArrayCache();
16
17
        parent::setUp();
18
19
        $this->_schemaTool->createSchema([$this->_em->getClassMetadata(GH2947Car::class)]);
20
    }
21
22
    public function testIssue()
23
    {
24
        $this->createData();
25
        $initialQueryCount = $this->getCurrentQueryCount();
26
27
        $query = $this->createQuery();
28
        self::assertEquals('BMW', (string) $query->getSingleResult());
29
        self::assertEquals($initialQueryCount + 1, $this->getCurrentQueryCount());
30
31
        $this->updateData();
32
        self::assertEquals('BMW', (string) $query->getSingleResult());
33
        self::assertEquals($initialQueryCount + 2, $this->getCurrentQueryCount());
34
35
        $query->expireResultCache(true);
36
        self::assertEquals('Dacia', (string) $query->getSingleResult());
37
        self::assertEquals($initialQueryCount + 3, $this->getCurrentQueryCount());
38
39
        $query->expireResultCache(false);
40
        self::assertEquals('Dacia', (string) $query->getSingleResult());
41
        self::assertEquals($initialQueryCount + 3, $this->getCurrentQueryCount());
42
    }
43
44
    private function createQuery()
45
    {
46
        return $this->_em->createQueryBuilder()
47
                         ->select('car')
48
                         ->from(GH2947Car::class, 'car')
49
                         ->getQuery()
50
                         ->useResultCache(true, 3600, 'foo-cache-id');
51
    }
52
53
    private function createData()
54
    {
55
        $this->_em->persist(new GH2947Car('BMW'));
56
        $this->_em->flush();
57
        $this->_em->clear();
58
    }
59
60
    private function updateData()
61
    {
62
        $this->_em->createQueryBuilder()
63
                  ->update(GH2947Car::class, 'car')
64
                  ->set('car.brand', ':newBrand')
65
                  ->where('car.brand = :oldBrand')
66
                  ->setParameter('newBrand', 'Dacia')
67
                  ->setParameter('oldBrand', 'BMW')
68
                  ->getQuery()
69
                  ->execute();
70
    }
71
}
72
73
/**
74
 * @Entity
75
 * @Table(name="GH2947_car")
76
 */
77
class GH2947Car
78
{
79
    /**
80
     * @Id
81
     * @Column(type="string", length=25)
82
     * @GeneratedValue(strategy="NONE")
83
     */
84
    public $brand;
85
86
    public function __construct(string $brand)
87
    {
88
        $this->brand = $brand;
89
    }
90
91
    public function __toString(): string
92
    {
93
        return $this->brand;
94
    }
95
}
96