PhpArrayAdapterTest.php
5.7 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Cache\Tests\Adapter;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
/**
* @group time-sensitive
*/
class PhpArrayAdapterTest extends AdapterTestCase
{
protected $skippedTests = [
'testGet' => 'PhpArrayAdapter is read-only.',
'testRecursiveGet' => 'PhpArrayAdapter is read-only.',
'testBasicUsage' => 'PhpArrayAdapter is read-only.',
'testBasicUsageWithLongKey' => 'PhpArrayAdapter is read-only.',
'testClear' => 'PhpArrayAdapter is read-only.',
'testClearWithDeferredItems' => 'PhpArrayAdapter is read-only.',
'testDeleteItem' => 'PhpArrayAdapter is read-only.',
'testSaveExpired' => 'PhpArrayAdapter is read-only.',
'testSaveWithoutExpire' => 'PhpArrayAdapter is read-only.',
'testDeferredSave' => 'PhpArrayAdapter is read-only.',
'testDeferredSaveWithoutCommit' => 'PhpArrayAdapter is read-only.',
'testDeleteItems' => 'PhpArrayAdapter is read-only.',
'testDeleteDeferredItem' => 'PhpArrayAdapter is read-only.',
'testCommit' => 'PhpArrayAdapter is read-only.',
'testSaveDeferredWhenChangingValues' => 'PhpArrayAdapter is read-only.',
'testSaveDeferredOverwrite' => 'PhpArrayAdapter is read-only.',
'testIsHitDeferred' => 'PhpArrayAdapter is read-only.',
'testExpiresAt' => 'PhpArrayAdapter does not support expiration.',
'testExpiresAtWithNull' => 'PhpArrayAdapter does not support expiration.',
'testExpiresAfterWithNull' => 'PhpArrayAdapter does not support expiration.',
'testDeferredExpired' => 'PhpArrayAdapter does not support expiration.',
'testExpiration' => 'PhpArrayAdapter does not support expiration.',
'testGetItemInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
'testGetItemsInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
'testHasItemInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
'testDeleteItemInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
'testDeleteItemsInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
'testDefaultLifeTime' => 'PhpArrayAdapter does not allow configuring a default lifetime.',
'testPrune' => 'PhpArrayAdapter just proxies',
];
protected static $file;
public static function setUpBeforeClass(): void
{
self::$file = sys_get_temp_dir().'/symfony-cache/php-array-adapter-test.php';
}
protected function tearDown(): void
{
if (file_exists(sys_get_temp_dir().'/symfony-cache')) {
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
}
}
public function createCachePool($defaultLifetime = 0, $testMethod = null)
{
if ('testGetMetadata' === $testMethod) {
return new PhpArrayAdapter(self::$file, new FilesystemAdapter());
}
return new PhpArrayAdapterWrapper(self::$file, new NullAdapter());
}
public function testStore()
{
$arrayWithRefs = [];
$arrayWithRefs[0] = 123;
$arrayWithRefs[1] = &$arrayWithRefs[0];
$object = (object) [
'foo' => 'bar',
'foo2' => 'bar2',
];
$expected = [
'null' => null,
'serializedString' => serialize($object),
'arrayWithRefs' => $arrayWithRefs,
'object' => $object,
'arrayWithObject' => ['bar' => $object],
];
$adapter = $this->createCachePool();
$adapter->warmUp($expected);
foreach ($expected as $key => $value) {
$this->assertSame(serialize($value), serialize($adapter->getItem($key)->get()), 'Warm up should create a PHP file that OPCache can load in memory');
}
}
public function testStoredFile()
{
$data = [
'integer' => 42,
'float' => 42.42,
'boolean' => true,
'array_simple' => ['foo', 'bar'],
'array_associative' => ['foo' => 'bar', 'foo2' => 'bar2'],
];
$expected = [
[
'integer' => 0,
'float' => 1,
'boolean' => 2,
'array_simple' => 3,
'array_associative' => 4,
],
[
0 => 42,
1 => 42.42,
2 => true,
3 => ['foo', 'bar'],
4 => ['foo' => 'bar', 'foo2' => 'bar2'],
],
];
$adapter = $this->createCachePool();
$adapter->warmUp($data);
$values = eval(substr(file_get_contents(self::$file), 6));
$this->assertSame($expected, $values, 'Warm up should create a PHP file that OPCache can load in memory');
}
}
class PhpArrayAdapterWrapper extends PhpArrayAdapter
{
protected $data = [];
public function save(CacheItemInterface $item)
{
(\Closure::bind(function () use ($item) {
$key = $item->getKey();
$this->keys[$key] = $id = \count($this->values);
$this->data[$key] = $this->values[$id] = $item->get();
$this->warmUp($this->data);
list($this->keys, $this->values) = eval(substr(file_get_contents($this->file), 6));
}, $this, PhpArrayAdapter::class))();
return true;
}
}