-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathCachePoolsTest.php
127 lines (107 loc) · 4 KB
/
CachePoolsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\HttpKernel\KernelInterface;
class CachePoolsTest extends AbstractWebTestCase
{
public function testCachePools()
{
$this->doTestCachePools([], AdapterInterface::class);
}
/**
* @requires extension redis
*
* @group integration
*/
public function testRedisCachePools()
{
$this->skipIfRedisUnavailable();
try {
$this->doTestCachePools(['root_config' => 'redis_config.yml', 'environment' => 'redis_cache'], RedisAdapter::class);
} catch (\PHPUnit\Framework\Error\Warning $e) {
if (!str_starts_with($e->getMessage(), 'unable to connect to')) {
throw $e;
}
$this->markTestSkipped($e->getMessage());
} catch (InvalidArgumentException $e) {
if (!str_starts_with($e->getMessage(), 'Redis connection ')) {
throw $e;
}
$this->markTestSkipped($e->getMessage());
}
}
/**
* @requires extension redis
*
* @group integration
*/
public function testRedisCustomCachePools()
{
$this->skipIfRedisUnavailable();
try {
$this->doTestCachePools(['root_config' => 'redis_custom_config.yml', 'environment' => 'custom_redis_cache'], RedisAdapter::class);
} catch (\PHPUnit\Framework\Error\Warning $e) {
if (!str_starts_with($e->getMessage(), 'unable to connect to')) {
throw $e;
}
$this->markTestSkipped($e->getMessage());
}
}
private function doTestCachePools($options, $adapterClass)
{
static::bootKernel($options);
$container = static::getContainer();
$pool1 = $container->get('cache.pool1');
$this->assertInstanceOf($adapterClass, $pool1);
$key = 'foobar';
$pool1->deleteItem($key);
$item = $pool1->getItem($key);
$this->assertFalse($item->isHit());
$item->set('baz');
$pool1->save($item);
$item = $pool1->getItem($key);
$this->assertTrue($item->isHit());
$pool2 = $container->get('cache.pool2');
$pool2->save($item);
$item = $pool2->getItem($key);
$this->assertTrue($item->isHit());
$prefix = "\0".TagAwareAdapter::class."\0";
$pool4 = $container->get('cache.pool4');
$this->assertInstanceof(TagAwareAdapter::class, $pool4);
$pool4 = (array) $pool4;
$this->assertSame($pool4[$prefix.'pool'], $pool4[$prefix.'tags'] ?? $pool4['tags']);
$pool5 = $container->get('cache.pool5');
$this->assertInstanceof(TagAwareAdapter::class, $pool5);
$pool5 = (array) $pool5;
$this->assertSame($pool2, $pool5[$prefix.'tags'] ?? $pool5['tags']);
$pool6 = $container->get('cache.pool6');
$this->assertInstanceof(TagAwareAdapter::class, $pool6);
$pool6 = (array) $pool6;
$this->assertSame($pool4[$prefix.'pool'], $pool6[$prefix.'tags'] ?? $pool6['tags']);
$pool7 = $container->get('cache.pool7');
$this->assertNotInstanceof(TagAwareAdapter::class, $pool7);
}
protected static function createKernel(array $options = []): KernelInterface
{
return parent::createKernel(['test_case' => 'CachePools'] + $options);
}
private function skipIfRedisUnavailable()
{
try {
(new \Redis())->connect(...explode(':', getenv('REDIS_HOST')));
} catch (\Exception $e) {
self::markTestSkipped($e->getMessage());
}
}
}