Skip to content

Commit 154a465

Browse files
authored
Merge pull request doctrine#12086 from mvorisek/add_cache_rw_strict_locking_test
Add functional strict-locking 2nd level cache test
2 parents 1ee01f4 + ae7489f commit 154a465

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\ORM\Functional;
6+
7+
use Doctrine\ORM\Mapping\ClassMetadata;
8+
use Doctrine\ORM\Mapping\ClassMetadataInfo;
9+
use Doctrine\Tests\Models\Cache\Country;
10+
use ReflectionProperty;
11+
12+
use function array_diff;
13+
use function array_filter;
14+
use function file_exists;
15+
use function rmdir;
16+
use function scandir;
17+
use function strpos;
18+
use function sys_get_temp_dir;
19+
20+
use const DIRECTORY_SEPARATOR;
21+
22+
/**
23+
* @group DDC-2183
24+
* @phpstan-type SupportedCacheUsage 0|ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE|ClassMetadata::CACHE_USAGE_READ_WRITE
25+
*/
26+
class SecondLevelCacheCountQueriesTest extends SecondLevelCacheFunctionalTestCase
27+
{
28+
/** @var string */
29+
private $tmpDir;
30+
31+
protected function tearDown(): void
32+
{
33+
if ($this->tmpDir !== null && file_exists($this->tmpDir)) {
34+
foreach (array_diff(scandir($this->tmpDir), ['.', '..']) as $f) {
35+
rmdir($this->tmpDir . DIRECTORY_SEPARATOR . $f);
36+
}
37+
38+
rmdir($this->tmpDir);
39+
}
40+
41+
parent::tearDown();
42+
}
43+
44+
/** @param SupportedCacheUsage $cacheUsage */
45+
private function setupCountryModel(int $cacheUsage): void
46+
{
47+
$metadata = $this->_em->getClassMetaData(Country::class);
48+
49+
if ($cacheUsage === 0) {
50+
$metadataCacheReflection = new ReflectionProperty(ClassMetadataInfo::class, 'cache');
51+
$metadataCacheReflection->setAccessible(true);
52+
$metadataCacheReflection->setValue($metadata, null);
53+
54+
return;
55+
}
56+
57+
if ($cacheUsage === ClassMetadata::CACHE_USAGE_READ_WRITE) {
58+
$this->tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . self::class;
59+
$this->secondLevelCacheFactory->setFileLockRegionDirectory($this->tmpDir);
60+
}
61+
62+
$metadata->enableCache(['usage' => $cacheUsage]);
63+
}
64+
65+
/** @param 'INSERT'|'UPDATE'|'DELETE' $type */
66+
private function assertQueryCountByType(string $type, int $expectedCount): void
67+
{
68+
$queries = array_filter($this->getQueryLog()->queries, static function (array $entry) use ($type): bool {
69+
return strpos($entry['sql'], $type) === 0;
70+
});
71+
72+
self::assertCount($expectedCount, $queries);
73+
}
74+
75+
/**
76+
/* @param SupportedCacheUsage $cacheUsage
77+
*
78+
* @dataProvider cacheUsageProvider
79+
*/
80+
public function testInsert(int $cacheUsage): void
81+
{
82+
$this->setupCountryModel($cacheUsage);
83+
84+
self::assertQueryCountByType('INSERT', 0);
85+
86+
$this->loadFixturesCountries();
87+
88+
self::assertCount(2, $this->countries);
89+
self::assertQueryCountByType('INSERT', 2);
90+
}
91+
92+
/**
93+
/* @param SupportedCacheUsage $cacheUsage
94+
*
95+
* @dataProvider cacheUsageProvider
96+
*/
97+
public function testDelete(int $cacheUsage): void
98+
{
99+
$this->setupCountryModel($cacheUsage);
100+
$this->loadFixturesCountries();
101+
102+
$c1 = $this->_em->find(Country::class, $this->countries[0]->getId());
103+
$c2 = $this->_em->find(Country::class, $this->countries[1]->getId());
104+
105+
$this->_em->remove($c1);
106+
$this->_em->remove($c2);
107+
$this->_em->flush();
108+
109+
self::assertQueryCountByType('DELETE', 2);
110+
}
111+
112+
/**
113+
/* @param SupportedCacheUsage $cacheUsage
114+
*
115+
* @dataProvider cacheUsageProvider
116+
*/
117+
public function testUpdate(int $cacheUsage): void
118+
{
119+
$this->setupCountryModel($cacheUsage);
120+
$this->loadFixturesCountries();
121+
122+
$c1 = $this->_em->find(Country::class, $this->countries[0]->getId());
123+
$c2 = $this->_em->find(Country::class, $this->countries[1]->getId());
124+
125+
$c1->setName('Czech Republic');
126+
$c2->setName('Hungary');
127+
128+
$this->_em->persist($c1);
129+
$this->_em->persist($c2);
130+
$this->_em->flush();
131+
132+
self::assertQueryCountByType('UPDATE', 2);
133+
}
134+
135+
public static function cacheUsageProvider(): array
136+
{
137+
return [
138+
[0],
139+
[ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE],
140+
[ClassMetadata::CACHE_USAGE_READ_WRITE],
141+
];
142+
}
143+
}

0 commit comments

Comments
 (0)