1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Functional\Ticket; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
8
|
|
|
use Doctrine\ORM\Annotation as ORM; |
9
|
|
|
use Doctrine\ORM\Cache\{DefaultCacheFactory, RegionsConfiguration}; |
|
|
|
|
10
|
|
|
use Doctrine\ORM\EntityManager; |
11
|
|
|
use Doctrine\ORM\Tools\{SchemaTool, Setup}; |
|
|
|
|
12
|
|
|
use Doctrine\Tests\TestUtil; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
|
|
|
15
|
|
|
|
16
|
|
|
class GH7708Test extends TestCase |
17
|
|
|
{ |
18
|
|
|
protected $entityManager; |
19
|
|
|
|
20
|
|
|
protected function getEntityManager() |
21
|
|
|
{ |
22
|
|
|
if (!$this->entityManager) { |
|
|
|
|
23
|
|
|
$regionsConfig = new RegionsConfiguration(); |
24
|
|
|
|
25
|
|
|
$cacheDriver = new GH7708ArrayCacheMock(); |
26
|
|
|
|
27
|
|
|
$cacheFactory = new DefaultCacheFactory($regionsConfig, $cacheDriver); |
28
|
|
|
|
29
|
|
|
$conn = TestUtil::getConnection(); |
30
|
|
|
|
31
|
|
|
$config = Setup::createAnnotationMetadataConfiguration([]); |
32
|
|
|
|
33
|
|
|
$config->setMetadataCacheImpl($cacheDriver); |
34
|
|
|
$config->setQueryCacheImpl($cacheDriver); |
35
|
|
|
$config->setResultCacheImpl($cacheDriver); |
36
|
|
|
$config->setHydrationCacheImpl($cacheDriver); |
37
|
|
|
|
38
|
|
|
$config->setSecondLevelCacheEnabled(); |
39
|
|
|
$secondLevelCacheConfig = $config->getSecondLevelCacheConfiguration(); |
40
|
|
|
$secondLevelCacheConfig->setCacheFactory($cacheFactory); |
41
|
|
|
|
42
|
|
|
$this->entityManager = EntityManager::create($conn, $config); |
43
|
|
|
|
44
|
|
|
// Creating a schema |
45
|
|
|
|
46
|
|
|
$schemaTool = new SchemaTool($this->entityManager); |
47
|
|
|
$schemaTool->createSchema([ |
48
|
|
|
$this->entityManager->getClassMetadata(GH7708Car::class), |
49
|
|
|
$this->entityManager->getClassMetadata(GH7708Model::class), |
50
|
|
|
$this->entityManager->getClassMetadata(GH7708Drive::class) |
|
|
|
|
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
// Populating with data |
54
|
|
|
|
55
|
|
|
$frontDrive = new GH7708Drive('Front'); |
56
|
|
|
$rearDrive = new GH7708Drive('Rear'); |
|
|
|
|
57
|
|
|
$fullDrive = new GH7708Drive('Full'); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('Audi A4'), $rearDrive)); |
60
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('Audi A5'), $rearDrive)); |
61
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('Audi A6'), $rearDrive)); |
62
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('Audi A8'), $rearDrive)); |
63
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('Audi Q3'), $fullDrive)); |
64
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('Audi Q5'), $fullDrive)); |
65
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('Audi Q7'), $fullDrive)); |
66
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('BMW 1-series'), $rearDrive)); |
67
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('BMW 3-series'), $rearDrive)); |
68
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('BMW 5-series'), $rearDrive)); |
69
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('BMW 7-series'), $rearDrive)); |
70
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('BMW X1'), $fullDrive)); |
71
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('BMW X3'), $fullDrive)); |
72
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('BMW X5'), $fullDrive)); |
73
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('BMW X6'), $fullDrive)); |
74
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('Volkswagen Golf'), $frontDrive)); |
75
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('Volkswagen Jetta'), $frontDrive)); |
76
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('Volkswagen Passat'), $frontDrive)); |
77
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('Volkswagen Polo'), $frontDrive)); |
78
|
|
|
$this->entityManager->persist(new GH7708Car(new GH7708Model('Volkswagen Tiguan'), $fullDrive)); |
79
|
|
|
|
80
|
|
|
$this->entityManager->flush(); |
81
|
|
|
} |
82
|
|
|
return $this->entityManager; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function executeQuery(): array |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
return $this->getEntityManager()->getRepository(GH7708Car::class)->createQueryBuilder('ca') |
88
|
|
|
->addSelect('mo') |
89
|
|
|
->addSelect('dr') |
90
|
|
|
->innerJoin('ca.model', 'mo') |
91
|
|
|
->innerJoin('ca.drive', 'dr') |
92
|
|
|
->getQuery() |
93
|
|
|
->setMaxResults(20) |
94
|
|
|
->useResultCache(true, 3600, 'result') |
95
|
|
|
->setCacheable(true) |
96
|
|
|
->getResult(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testSecondLevelCacheCartesianProduct() : void |
100
|
|
|
{ |
101
|
|
|
// Executing the query for the first time to populate the cache |
102
|
|
|
$this->executeQuery(); |
103
|
|
|
|
104
|
|
|
// Reseting the mock array |
105
|
|
|
GH7708ArrayCacheMock::$fetchRequests = []; |
106
|
|
|
|
107
|
|
|
// The second query execution for retreiving data from cache |
108
|
|
|
$result = $this->executeQuery(); |
109
|
|
|
|
110
|
|
|
// I expect that cache requests count is less than query result (20 rows). |
111
|
|
|
$resultObjectsCount = count($result); |
|
|
|
|
112
|
|
|
$fetchRequestsCount = count(GH7708ArrayCacheMock::$fetchRequests); |
|
|
|
|
113
|
|
|
$this->assertLessThan($resultObjectsCount, $fetchRequestsCount); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
class GH7708ArrayCacheMock extends ArrayCache |
118
|
|
|
{ |
119
|
|
|
public static $fetchRequests = []; |
120
|
|
|
|
121
|
|
|
protected function doFetch($id) |
122
|
|
|
{ |
123
|
|
|
self::$fetchRequests[] = [ |
124
|
|
|
'id' => $id, |
125
|
|
|
'cacheDriverObject' => spl_object_hash($this) |
|
|
|
|
126
|
|
|
]; |
127
|
|
|
return parent::doFetch($id); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @ORM\Entity |
133
|
|
|
* @ORM\Cache(usage="NONSTRICT_READ_WRITE") |
134
|
|
|
**/ |
135
|
|
|
class GH7708Car |
136
|
|
|
{ |
137
|
|
|
/** |
138
|
|
|
* @ORM\Id |
139
|
|
|
* @ORM\Column(type="integer") |
140
|
|
|
* @ORM\GeneratedValue |
141
|
|
|
**/ |
142
|
|
|
protected $id; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @ORM\ManyToOne(targetEntity=GH7708Model::class, cascade={"persist"}) |
146
|
|
|
* @ORM\Cache(usage="NONSTRICT_READ_WRITE") |
147
|
|
|
**/ |
148
|
|
|
protected $model; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @ORM\ManyToOne(targetEntity=GH7708Drive::class, cascade={"persist"}) |
152
|
|
|
* @ORM\Cache(usage="NONSTRICT_READ_WRITE") |
153
|
|
|
**/ |
154
|
|
|
protected $drive; |
155
|
|
|
|
156
|
|
|
public function __construct(GH7708Model $model, GH7708Drive $drive) |
157
|
|
|
{ |
158
|
|
|
$this->model = $model; |
159
|
|
|
$this->drive = $drive; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @ORM\Entity |
165
|
|
|
* @ORM\Cache(usage="NONSTRICT_READ_WRITE") |
166
|
|
|
**/ |
167
|
|
|
class GH7708Model |
168
|
|
|
{ |
169
|
|
|
/** |
170
|
|
|
* @ORM\Id |
171
|
|
|
* @ORM\Column(type="integer") |
172
|
|
|
* @ORM\GeneratedValue |
173
|
|
|
**/ |
174
|
|
|
protected $id; |
175
|
|
|
|
176
|
|
|
/** |
|
|
|
|
177
|
|
|
* @ORM\Column(type="string", length=64) |
178
|
|
|
**/ |
179
|
|
|
protected $title; |
180
|
|
|
|
181
|
|
|
public function __construct(string $title) |
182
|
|
|
{ |
183
|
|
|
$this->title = $title; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @ORM\Entity |
189
|
|
|
* @ORM\Cache(usage="NONSTRICT_READ_WRITE") |
190
|
|
|
**/ |
191
|
|
|
class GH7708Drive |
192
|
|
|
{ |
193
|
|
|
/** |
194
|
|
|
* @ORM\Id |
195
|
|
|
* @ORM\Column(type="integer") |
196
|
|
|
* @ORM\GeneratedValue |
197
|
|
|
**/ |
198
|
|
|
protected $id; |
199
|
|
|
|
200
|
|
|
/** |
|
|
|
|
201
|
|
|
* @ORM\Column(type="string", length=64) |
202
|
|
|
**/ |
203
|
|
|
protected $title; |
204
|
|
|
|
205
|
|
|
public function __construct(string $title) |
206
|
|
|
{ |
207
|
|
|
$this->title = $title; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|