ApcuAdapterTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Tests\Adapter;
  11. use Psr\Log\NullLogger;
  12. use Symfony\Component\Cache\Adapter\ApcuAdapter;
  13. class ApcuAdapterTest extends AdapterTestCase
  14. {
  15. protected $skippedTests = [
  16. 'testExpiration' => 'Testing expiration slows down the test suite',
  17. 'testHasItemReturnsFalseWhenDeferredItemIsExpired' => 'Testing expiration slows down the test suite',
  18. 'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
  19. ];
  20. public function createCachePool($defaultLifetime = 0)
  21. {
  22. if (!\function_exists('apcu_fetch') || !filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN)) {
  23. $this->markTestSkipped('APCu extension is required.');
  24. }
  25. if ('cli' === \PHP_SAPI && !filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN)) {
  26. if ('testWithCliSapi' !== $this->getName()) {
  27. $this->markTestSkipped('apc.enable_cli=1 is required.');
  28. }
  29. }
  30. if ('\\' === \DIRECTORY_SEPARATOR) {
  31. $this->markTestSkipped('Fails transiently on Windows.');
  32. }
  33. return new ApcuAdapter(str_replace('\\', '.', __CLASS__), $defaultLifetime);
  34. }
  35. public function testUnserializable()
  36. {
  37. $pool = $this->createCachePool();
  38. $item = $pool->getItem('foo');
  39. $item->set(function () {});
  40. $this->assertFalse($pool->save($item));
  41. $item = $pool->getItem('foo');
  42. $this->assertFalse($item->isHit());
  43. }
  44. public function testVersion()
  45. {
  46. $namespace = str_replace('\\', '.', \get_class($this));
  47. $pool1 = new ApcuAdapter($namespace, 0, 'p1');
  48. $item = $pool1->getItem('foo');
  49. $this->assertFalse($item->isHit());
  50. $this->assertTrue($pool1->save($item->set('bar')));
  51. $item = $pool1->getItem('foo');
  52. $this->assertTrue($item->isHit());
  53. $this->assertSame('bar', $item->get());
  54. $pool2 = new ApcuAdapter($namespace, 0, 'p2');
  55. $item = $pool2->getItem('foo');
  56. $this->assertFalse($item->isHit());
  57. $this->assertNull($item->get());
  58. $item = $pool1->getItem('foo');
  59. $this->assertFalse($item->isHit());
  60. $this->assertNull($item->get());
  61. }
  62. public function testNamespace()
  63. {
  64. $namespace = str_replace('\\', '.', \get_class($this));
  65. $pool1 = new ApcuAdapter($namespace.'_1', 0, 'p1');
  66. $item = $pool1->getItem('foo');
  67. $this->assertFalse($item->isHit());
  68. $this->assertTrue($pool1->save($item->set('bar')));
  69. $item = $pool1->getItem('foo');
  70. $this->assertTrue($item->isHit());
  71. $this->assertSame('bar', $item->get());
  72. $pool2 = new ApcuAdapter($namespace.'_2', 0, 'p1');
  73. $item = $pool2->getItem('foo');
  74. $this->assertFalse($item->isHit());
  75. $this->assertNull($item->get());
  76. $item = $pool1->getItem('foo');
  77. $this->assertTrue($item->isHit());
  78. $this->assertSame('bar', $item->get());
  79. }
  80. public function testWithCliSapi()
  81. {
  82. try {
  83. // disable PHPUnit error handler to mimic a production environment
  84. $isCalled = false;
  85. set_error_handler(function () use (&$isCalled) {
  86. $isCalled = true;
  87. });
  88. $pool = new ApcuAdapter(str_replace('\\', '.', __CLASS__));
  89. $pool->setLogger(new NullLogger());
  90. $item = $pool->getItem('foo');
  91. $item->isHit();
  92. $pool->save($item->set('bar'));
  93. $this->assertFalse($isCalled);
  94. } finally {
  95. restore_error_handler();
  96. }
  97. }
  98. }