|
1
|
|
|
<?php |
|
2
|
|
|
namespace Doctrine\Tests\ORM\Functional\Ticket\Issue7735; |
|
3
|
|
|
|
|
4
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
|
6
|
|
|
use Doctrine\DBAL\Driver\Connection; |
|
7
|
|
|
use Doctrine\DBAL\Logging\DebugStack; |
|
8
|
|
|
use Doctrine\ORM\Cache\CacheConfiguration; |
|
9
|
|
|
use Doctrine\ORM\Cache\DefaultCacheFactory; |
|
10
|
|
|
use Doctrine\ORM\Cache\Logging\StatisticsCacheLogger; |
|
11
|
|
|
use Doctrine\ORM\Configuration; |
|
12
|
|
|
use Doctrine\ORM\EntityManager; |
|
13
|
|
|
use Doctrine\ORM\Tools\DebugUnitOfWorkListener; |
|
14
|
|
|
use Doctrine\Tests\EventListener\CacheMetadataListener; |
|
15
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Issue7735Test |
|
19
|
|
|
* @package Doctrine\Tests\ORM\Functional\Ticket\Issue7735 |
|
20
|
|
|
*/ |
|
21
|
|
|
class Issue7735Test extends OrmFunctionalTestCase |
|
22
|
|
|
{ |
|
23
|
|
|
protected const DEFAULT_CACHE_REGION_LIFE_TIME = 1; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The metadata cache shared between all functional tests. |
|
27
|
|
|
* |
|
28
|
|
|
* @var \Doctrine\Common\Cache\Cache|null |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $_metadataCacheImpl = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The query cache shared between all functional tests. |
|
34
|
|
|
* |
|
35
|
|
|
* @var \Doctrine\Common\Cache\Cache|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private static $_queryCacheImpl = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \Doctrine\ORM\Cache |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $cache; |
|
43
|
|
|
|
|
44
|
|
|
public function setUp() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->enableSecondLevelCache(); |
|
47
|
|
|
$this->useModelSet('issue7735'); |
|
48
|
|
|
parent::setUp(); |
|
49
|
|
|
$this->cache = $this->_em->getCache(); |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
$engine = new Engine('turbo'); |
|
53
|
|
|
$power = new Power($engine); |
|
54
|
|
|
$car = new Car($engine); |
|
55
|
|
|
|
|
56
|
|
|
$this->_em->persist($car); |
|
57
|
|
|
$this->_em->persist($power); |
|
58
|
|
|
$this->_em->persist($engine); |
|
59
|
|
|
|
|
60
|
|
|
$this->_em->flush(); |
|
61
|
|
|
$this->_em->clear(); |
|
62
|
|
|
$this->evictRegions(); |
|
63
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testFindByReturnsCachedEntity() |
|
67
|
|
|
{ |
|
68
|
|
|
for ($i = 1000000; $i > 0; $i--) { |
|
69
|
|
|
$carRepository = $this->_em->getRepository(Car::class); |
|
70
|
|
|
$car = $carRepository->find(1); |
|
71
|
|
|
$engine = $car->getEngine(); |
|
72
|
|
|
$power = $engine->getPower(); |
|
|
|
|
|
|
73
|
|
|
$model = $engine->getModel(); |
|
74
|
|
|
$this->assertNotEmpty($model); |
|
75
|
|
|
$this->_em->clear(); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function evictRegions() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->cache->evictQueryRegions(); |
|
82
|
|
|
$this->cache->evictEntityRegions(); |
|
83
|
|
|
$this->cache->evictCollectionRegions(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Gets an EntityManager for testing purposes. |
|
88
|
|
|
* |
|
89
|
|
|
* @return EntityManager |
|
90
|
|
|
* |
|
91
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function _getEntityManager( |
|
94
|
|
|
Connection $connection = null, |
|
95
|
|
|
MappingDriver $mappingDriver = null |
|
96
|
|
|
) |
|
97
|
|
|
{ |
|
98
|
|
|
// NOTE: Functional tests use their own shared metadata cache, because |
|
99
|
|
|
// the actual database platform used during execution has effect on some |
|
100
|
|
|
// metadata mapping behaviors (like the choice of the ID generation). |
|
101
|
|
|
if (is_null(self::$_metadataCacheImpl)) { |
|
102
|
|
|
if (isset($GLOBALS['DOCTRINE_CACHE_IMPL'])) { |
|
103
|
|
|
self::$_metadataCacheImpl = new $GLOBALS['DOCTRINE_CACHE_IMPL']; |
|
104
|
|
|
} else { |
|
105
|
|
|
self::$_metadataCacheImpl = new ArrayCache(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (is_null(self::$_queryCacheImpl)) { |
|
110
|
|
|
self::$_queryCacheImpl = new ArrayCache(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$this->_sqlLoggerStack = new DebugStack(); |
|
114
|
|
|
$this->_sqlLoggerStack->enabled = false; |
|
115
|
|
|
|
|
116
|
|
|
//FIXME: two different configs! $conn and the created entity manager have |
|
117
|
|
|
// different configs. |
|
118
|
|
|
$config = new Configuration(); |
|
119
|
|
|
$config->setMetadataCacheImpl(self::$_metadataCacheImpl); |
|
120
|
|
|
$config->setQueryCacheImpl(self::$_queryCacheImpl); |
|
121
|
|
|
$config->setProxyDir(__DIR__ . '/../../../../Proxies'); |
|
122
|
|
|
$config->setProxyNamespace('Doctrine\Tests\Proxies'); |
|
123
|
|
|
|
|
124
|
|
|
if (null !== $this->resultCacheImpl) { |
|
125
|
|
|
$config->setResultCacheImpl($this->resultCacheImpl); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$enableSecondLevelCache = getenv('ENABLE_SECOND_LEVEL_CACHE'); |
|
129
|
|
|
|
|
130
|
|
|
if ($this->isSecondLevelCacheEnabled || $enableSecondLevelCache) { |
|
131
|
|
|
|
|
132
|
|
|
$cacheConfig = new CacheConfiguration(); |
|
133
|
|
|
$cache = $this->getSharedSecondLevelCacheDriverImpl(); |
|
134
|
|
|
|
|
135
|
|
|
$regionConfig = $cacheConfig->getRegionsConfiguration(); |
|
136
|
|
|
$regionConfig->setDefaultLifetime(static::DEFAULT_CACHE_REGION_LIFE_TIME); |
|
137
|
|
|
$factory = new DefaultCacheFactory($regionConfig, $cache); |
|
138
|
|
|
|
|
139
|
|
|
$this->secondLevelCacheFactory = $factory; |
|
140
|
|
|
|
|
141
|
|
|
if ($this->isSecondLevelCacheLogEnabled) { |
|
142
|
|
|
$this->secondLevelCacheLogger = new StatisticsCacheLogger(); |
|
143
|
|
|
$cacheConfig->setCacheLogger($this->secondLevelCacheLogger); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$cacheConfig->setCacheFactory($factory); |
|
147
|
|
|
$config->setSecondLevelCacheEnabled(true); |
|
148
|
|
|
$config->setSecondLevelCacheConfiguration($cacheConfig); |
|
149
|
|
|
|
|
150
|
|
|
$this->isSecondLevelCacheEnabled = true; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$config->setMetadataDriverImpl( |
|
154
|
|
|
$mappingDriver ?? $config->newDefaultAnnotationDriver( |
|
155
|
|
|
[ |
|
156
|
|
|
realpath(__DIR__ . '/../../../../Models/Cache'), |
|
157
|
|
|
realpath(__DIR__ . '/../../../../Models/GeoNames') |
|
158
|
|
|
], |
|
159
|
|
|
true |
|
160
|
|
|
) |
|
161
|
|
|
); |
|
162
|
|
|
|
|
163
|
|
|
$conn = $connection ?: static::$_sharedConn; |
|
164
|
|
|
$conn->getConfiguration()->setSQLLogger($this->_sqlLoggerStack); |
|
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
// get rid of more global state |
|
167
|
|
|
$evm = $conn->getEventManager(); |
|
|
|
|
|
|
168
|
|
|
foreach ($evm->getListeners() AS $event => $listeners) { |
|
169
|
|
|
foreach ($listeners AS $listener) { |
|
170
|
|
|
$evm->removeEventListener([$event], $listener); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
if ($enableSecondLevelCache) { |
|
175
|
|
|
$evm->addEventListener('loadClassMetadata', new CacheMetadataListener()); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
if (isset($GLOBALS['db_event_subscribers'])) { |
|
179
|
|
|
foreach (explode(",", $GLOBALS['db_event_subscribers']) AS $subscriberClass) { |
|
180
|
|
|
$subscriberInstance = new $subscriberClass(); |
|
181
|
|
|
$evm->addEventSubscriber($subscriberInstance); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if (isset($GLOBALS['debug_uow_listener'])) { |
|
186
|
|
|
$evm->addEventListener(['onFlush'], new DebugUnitOfWorkListener()); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return EntityManager::create($conn, $config); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|