ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
CacheTest.php
Go to the documentation of this file.
1 <?php
2 
19 namespace ILIAS\Cache;
20 
31 
32 require_once(__DIR__ . '/../../../../vendor/composer/vendor/autoload.php');
33 
37 class CacheTest extends TestCase
38 {
39  public const TEST_CONTAINER = 'test_container';
43  private \ilLanguage $language_mock;
44  private \ILIAS\Refinery\Factory $refinery;
45 
46  protected function setUp(): void
47  {
48  $this->language_mock = $this->createMock(\ilLanguage::class);
49  $this->refinery = new \ILIAS\Refinery\Factory(
50  new \ILIAS\Data\Factory(),
51  $this->language_mock
52  );
53  // prevent chached values between tests
54  $static_flush = new PHPStatic($this->getConfig(Config::PHPSTATIC));
55  $static_flush->flush();
56  }
57 
61  protected function getConfig(string $adaptor_name = Config::PHPSTATIC): Config
62  {
63  return new Config(
64  $adaptor_name,
65  true,
66  [self::TEST_CONTAINER],
67  new class () extends NullNodeRepository {
68  public function getNodes(): array
69  {
70  return [new Node('127.0.0.1', 11211, 100)];
71  }
72  }
73  );
74  }
75 
76  public function testActivatedComponents(): void
77  {
78  $config = new Config(
80  true,
81  ['one', 'two']
82  );
83 
84  $services = new Services($config);
85  $this->assertInstanceOf(ActiveContainer::class, $services->get($this->getDummyRequest('one')));
86  $this->assertInstanceOf(ActiveContainer::class, $services->get($this->getDummyRequest('two')));
87  $this->assertInstanceOf(VoidContainer::class, $services->get($this->getDummyRequest('three')));
88 
89  $config = new Config(
91  true,
92  ['*']
93  );
94  $services = new Services($config);
95  $this->assertInstanceOf(ActiveContainer::class, $services->get($this->getDummyRequest('one')));
96  $this->assertInstanceOf(ActiveContainer::class, $services->get($this->getDummyRequest('two')));
97  $this->assertInstanceOf(ActiveContainer::class, $services->get($this->getDummyRequest('three')));
98  }
99 
100  public function testMultipleContainers(): void
101  {
102  $config = new Config(
104  true,
105  ['one', 'two']
106  );
107 
108  $services = new Services($config);
109 
110  $one = $this->getDummyRequest('one');
111  $two = $this->getDummyRequest('two');
112  $three = $this->getDummyRequest('three');
113 
114  $this->assertEquals('one', $one->getContainerKey());
115  $this->assertEquals('two', $two->getContainerKey());
116  $this->assertEquals('three', $three->getContainerKey());
117 
118  $container_one = $services->get($one);
119  $container_two = $services->get($two);
120  $container_three = $services->get($three);
121 
122  $this->assertInstanceOf(ActiveContainer::class, $container_one);
123  $this->assertInstanceOf(ActiveContainer::class, $container_two);
124  $this->assertInstanceOf(VoidContainer::class, $container_three);
125 
126  $container_one->set('test', 'test_value');
127  $this->assertTrue($container_one->has('test'));
128  $this->assertEquals('test_value', $container_one->get('test', $this->refinery->to()->string()));
129 
130  $container_two->set('test', 'test_value');
131  $this->assertTrue($container_two->has('test'));
132  $this->assertEquals('test_value', $container_two->get('test', $this->refinery->to()->string()));
133 
134  $container_three->set('test', 'test_value');
135  $this->assertFalse($container_three->has('test'));
136  $this->assertNull($container_three->get('test', $this->refinery->to()->string()));
137  }
138 
139  public function testLock(): void
140  {
141  $services = new Services($this->getConfig());
142  $container = $services->get($this->getDummyRequest(self::TEST_CONTAINER));
143  $container->unlock();
144 
145  $container->set('test', 'test_value');
146  $this->assertTrue($container->has('test'));
147  $this->assertEquals('test_value', $container->get('test', $this->refinery->to()->string()));
148 
149  $container->lock(1 / 1000);
150  $this->assertTrue($container->isLocked());
151  $this->assertFalse($container->has('test'));
152  $this->assertNull($container->get('test', $this->refinery->to()->string()));
153  usleep(1000);
154  $this->assertFalse($container->isLocked());
155  $this->assertTrue($container->has('test'));
156  $this->assertEquals('test_value', $container->get('test', $this->refinery->to()->string()));
157 
158  // Second Run
159  $container->lock(1 / 1000);
160  $this->assertTrue($container->isLocked());
161  $this->assertFalse($container->has('test'));
162  $this->assertNull($container->get('test', $this->refinery->to()->string()));
163  usleep(100);
164  $this->assertTrue($container->isLocked());
165  $this->assertFalse($container->has('test'));
166  $this->assertNull($container->get('test', $this->refinery->to()->string()));
167 
168  usleep(5000); // to avoid problems with the next test
169  $this->assertFalse($container->isLocked());
170  }
171 
172  public static function getInvalidLockTimes(): array
173  {
174  return [
175  [-10],
176  [-1],
177  [301],
178  [300.1],
179  ];
180  }
181 
185  public function testInvalidLockTimes(float|int $time): void
186  {
187  $services = new Services($this->getConfig());
188  $container = $services->get($this->getDummyRequest(self::TEST_CONTAINER));
189  $this->expectException(\InvalidArgumentException::class);
190  $container->lock($time);
191  }
192 
193  public function testDelete(): void
194  {
195  $services = new Services($this->getConfig());
196  $container = $services->get($this->getDummyRequest(self::TEST_CONTAINER));
197 
198  $container->set('test', 'test_value');
199  $this->assertTrue($container->has('test'));
200  $this->assertEquals('test_value', $container->get('test', $this->refinery->to()->string()));
201 
202  $container->delete('test');
203  $this->assertFalse($container->has('test'));
204  $this->assertNull($container->get('test', $this->refinery->to()->string()));
205  }
206 
207  public function testDefaultAdaptor(): void
208  {
209  $request = $this->getDummyRequest(self::TEST_CONTAINER);
210 
211  $to_string = $this->refinery->kindlyTo()->string();
212 
213  $config = $this->getConfig();
214 
215  $static = new PHPStatic($config);
216  $this->assertTrue($static->isAvailable());
217 
218  $services = new Services($config);
219  $container = $services->get($request);
220  $this->assertInstanceOf(ActiveContainer::class, $container);
221  $this->assertEquals(Config::PHPSTATIC, $container->getAdaptorName());
222  $this->assertEquals(self::TEST_CONTAINER, $container->getContainerName());
223 
224  $this->assertFalse($container->has('test'));
225  $container->set('test', 'test_value');
226  $this->assertTrue($container->has('test'));
227  $this->assertEquals('test_value', $container->get('test', $to_string));
228  $container->delete('test');
229  $this->assertFalse($container->has('test'));
230 
231  $container->set('test2', 'test_value2');
232  $container->set('test3', 'test_value3');
233  $this->assertTrue($container->has('test2'));
234  $this->assertTrue($container->has('test3'));
235 
236  $container->flush();
237 
238  $this->assertFalse($container->has('test2'));
239  $this->assertFalse($container->has('test3'));
240  }
241 
242  public function testFlush(): void
243  {
244  $services = new Services($this->getConfig());
245  $container = $services->get($this->getDummyRequest(self::TEST_CONTAINER));
246 
247  $container->set('test', 'test_value');
248  $this->assertTrue($container->has('test'));
249  $this->assertEquals('test_value', $container->get('test', $this->refinery->to()->string()));
250 
251  $container->flush();
252  $this->assertFalse($container->has('test'));
253  $this->assertNull($container->get('test', $this->refinery->to()->string()));
254 
255  $container->set('test', 'test_value');
256  $this->assertTrue($container->has('test'));
257  $this->assertEquals('test_value', $container->get('test', $this->refinery->to()->string()));
258 
259  $services->flushAdapter();
260  $this->assertFalse($container->has('test'));
261  $this->assertNull($container->get('test', $this->refinery->to()->string()));
262  }
263 
264  public function testAPCAdapter(): void
265  {
266  $config = $this->getConfig(Config::APCU);
267  $services = new Services($config);
268  $container = $services->get($this->getDummyRequest(self::TEST_CONTAINER));
269  $this->assertEquals(Config::APCU, $container->getAdaptorName());
270  $this->assertEquals(self::TEST_CONTAINER, $container->getContainerName());
271 
272  $apcu = new APCu($config);
273  if (!$apcu->isAvailable() || !(bool) ini_get('apc.enable_cli')) {
274  $this->markTestSkipped('APCu is not available or not enabled for CLI');
275  }
276 
277  $to_string = $this->refinery->kindlyTo()->string();
278 
279  $this->assertFalse($container->has('test'));
280  $container->set('test', 'test_value');
281  $this->assertTrue($container->has('test'));
282  $this->assertEquals('test_value', $container->get('test', $to_string));
283  $container->delete('test');
284  $this->assertFalse($container->has('test'));
285 
286  $container->set('test2', 'test_value2');
287  $container->set('test3', 'test_value3');
288  $this->assertTrue($container->has('test2'));
289  $this->assertTrue($container->has('test3'));
290 
291  $container->flush();
292 
293  $this->assertFalse($container->has('test2'));
294  $this->assertFalse($container->has('test3'));
295  }
296 
301  private function deactivatedTestMemcachedAdapter(): void
302  {
303  $config = $this->getConfig(Config::MEMCACHED);
304  $services = new Services($config);
305  $container = $services->get($this->getDummyRequest(self::TEST_CONTAINER));
306  $this->assertEquals(Config::MEMCACHED, $container->getAdaptorName());
307  $this->assertEquals(self::TEST_CONTAINER, $container->getContainerName());
308 
309  $apcu = new Memcached($config);
310  if (!$apcu->isAvailable()) {
311  $this->markTestSkipped('Memcached is not available');
312  }
313 
314  $this->assertFalse($container->has('test'));
315  $container->set('test', 'test_value');
316  $this->assertTrue($container->has('test'));
317  $this->assertEquals('test_value', $container->get('test'));
318  $container->delete('test');
319  $this->assertFalse($container->has('test'));
320 
321  $container->set('test2', 'test_value2');
322  $container->set('test3', 'test_value3');
323  $this->assertTrue($container->has('test2'));
324  $this->assertTrue($container->has('test3'));
325 
326  $container->flush();
327 
328  $this->assertFalse($container->has('test2'));
329  $this->assertFalse($container->has('test3'));
330  }
331 
332  public function testObjectStorage(): void
333  {
334  $services = new Services($this->getConfig());
335  $container = $services->get($this->getDummyRequest(self::TEST_CONTAINER));
336 
337  $first_object = new \stdClass();
338  $first_object->test = 'test';
339  $container->set('first_object', serialize($first_object));
340 
341  $this->assertTrue($container->has('first_object'));
342 
343  $to_string = $this->refinery->kindlyTo()->string();
344 
345  $data = $container->get('first_object', $to_string);
346  $first_object_from_cache = unserialize($data, ['allowed_classes' => [\stdClass::class]]);
347  $this->assertInstanceOf(\stdClass::class, $first_object_from_cache);
348  $this->assertEquals($first_object, $first_object_from_cache);
349  }
350 
351  public function testTypes(): void
352  {
353  $services = new Services($this->getConfig());
354  $container = $services->get($this->getDummyRequest(self::TEST_CONTAINER));
355 
356  // TRANSFORMATION
357  $to_string = $this->refinery->to()->string();
358  $to_int = $this->refinery->to()->int();
359  $to_array = $this->refinery->to()->listOf($to_string);
360  $to_bool = $this->refinery->to()->bool();
361 
362  // STRING
363  $string = 'test';
364  $container->set('string', $string);
365  $this->assertTrue($container->has('string'));
366  $string_from_cache = $container->get('string', $to_string);
367  $this->assertEquals($string, $string_from_cache);
368  $this->assertEquals(null, $container->get('string', $to_int));
369  $this->assertEquals(null, $container->get('string', $to_bool));
370  $this->assertEquals(null, $container->get('string', $to_array));
371  $this->assertIsString($string_from_cache);
372 
373  // ARRAY
374  $array = ['test', 'test2', 'test3'];
375  $container->set('array', $array);
376  $this->assertTrue($container->has('array'));
377  $array_from_cache = $container->get('array', $to_array);
378  $this->assertEquals($array, $array_from_cache);
379  $this->assertEquals(null, $container->get('array', $to_int));
380  $this->assertEquals(null, $container->get('array', $to_bool));
381  $this->assertEquals(null, $container->get('array', $to_string));
382  $this->assertIsArray($array_from_cache);
383 
384  // BOOL
385  $bool = true;
386  $container->set('bool', $bool);
387  $this->assertTrue($container->has('bool'));
388  $bool_from_cache = $container->get('bool', $to_bool);
389  $this->assertEquals($bool, $bool_from_cache);
390  $this->assertEquals(null, $container->get('bool', $to_int));
391  $this->assertEquals(null, $container->get('bool', $to_array));
392  $this->assertEquals(null, $container->get('bool', $to_string));
393  $this->assertIsBool($bool_from_cache);
394 
395  // ARRAY Different values
396  $array_with_different_values = ['test' => true, 'test2' => 123, 'test3' => ['test' => 'test'], 'test4' => null];
397  $container->set('array_with_different_values', $array_with_different_values);
398  $this->assertTrue($container->has('array_with_different_values'));
399 
400  $trafo = $this->refinery->to()->dictOf(
401  $this->refinery->custom()->transformation(
402  function ($value) {
403  return $value;
404  },
405  function ($value) {
406  return $value;
407  }
408  )
409  );
410 
411  $array_with_different_values_from_cache = $container->get('array_with_different_values', $trafo);
412  $this->assertEquals($array_with_different_values, $array_with_different_values_from_cache);
413  $this->assertIsArray($array_with_different_values_from_cache);
414  $this->assertIsBool($array_with_different_values_from_cache['test']);
415  $this->assertIsInt($array_with_different_values_from_cache['test2']);
416  $this->assertIsArray($array_with_different_values_from_cache['test3']);
417  $this->assertNull($array_with_different_values_from_cache['test4']);
418 
419  $this->assertEquals(null, $container->get('array_with_different_values', $to_int));
420  $this->assertEquals(null, $container->get('array_with_different_values', $to_bool));
421  $this->assertEquals(null, $container->get('array_with_different_values', $to_string));
422 
423  // INT
424  $int = 123;
425  $container->set('int', $int);
426  $this->assertTrue($container->has('int'));
427  $int_from_cache = $container->get('int', $to_int);
428  $this->assertEquals($int, $int_from_cache);
429  $this->assertEquals(null, $container->get('int', $to_string));
430  $this->assertEquals(null, $container->get('int', $to_bool));
431  $this->assertEquals(null, $container->get('int', $to_array));
432  $this->assertIsInt($int_from_cache);
433  }
434 
435  public function testIncomatibleType(): void
436  {
437  $services = new Services($this->getConfig());
438  $container = $services->get(
439  $this->getDummyRequest(self::TEST_CONTAINER)
440  );
441 
442  $this->expectException(\TypeError::class);
443  $container->set('test', new \stdClass());
444  }
445 
446  public function testIncomatibleNestedType(): void
447  {
448  $services = new Services($this->getConfig());
449  $container = $services->get(
450  $this->getDummyRequest(self::TEST_CONTAINER)
451  );
452  $array_with_incompatible_type_in_it = [
453  'test' => 'test',
454  'test2' => 'test2',
455  'test3' => ['test' => new \stdClass()]
456  ];
457  $this->expectException(\InvalidArgumentException::class);
458  $container->set(
459  'array_with_incompatible_type_in_it',
460  $array_with_incompatible_type_in_it
461  );
462  }
463 
464  public function testIncomatibleTypeNested(): void
465  {
466  $services = new Services($this->getConfig());
467  $container = $services->get($this->getDummyRequest(self::TEST_CONTAINER));
468 
469  $array_with_incompatible_type_in_it = [
470  'test' => 'test',
471  'test2' => 'test2',
472  'test3' => ['test' => new \stdClass()]
473  ];
474  $this->expectException(\InvalidArgumentException::class);
475  $container->set('array_with_incompatible_type_in_it', $array_with_incompatible_type_in_it);
476  }
477 
478  protected function getDummyRequest(string $container_key): Request
479  {
480  return new BaseRequest($container_key);
481  }
482 }
deactivatedTestMemcachedAdapter()
this test cannot be executed in the CI on github sincewe do not have a memcached server there...
Definition: CacheTest.php:301
Interface Observer Contains several chained tasks and infos about them.
ILIAS Refinery Factory $refinery
Definition: CacheTest.php:44
$container
Definition: wac.php:13
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getConfig(string $adaptor_name=Config::PHPSTATIC)
Definition: CacheTest.php:61
ilLanguage $language_mock
Definition: CacheTest.php:43
testInvalidLockTimes(float|int $time)
getInvalidLockTimes
Definition: CacheTest.php:185
getDummyRequest(string $container_key)
Definition: CacheTest.php:478
static getInvalidLockTimes()
Definition: CacheTest.php:172