PageRenderTime 67ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine/cache/tests/Doctrine/Tests/Common/Cache/PhpFileCacheTest.php

https://github.com/nattaphat/hgis
PHP | 149 lines | 97 code | 35 blank | 17 comment | 3 complexity | e6564a796e8625ecbb71d6eefbe75f2b MD5 | raw file
  1. <?php
  2. namespace Doctrine\Tests\Common\Cache;
  3. use Doctrine\Common\Cache\PhpFileCache;
  4. /**
  5. * @group DCOM-101
  6. */
  7. class PhpFileCacheTest extends CacheTest
  8. {
  9. /**
  10. * @var \Doctrine\Common\Cache\PhpFileCache
  11. */
  12. private $driver;
  13. protected function _getCacheDriver()
  14. {
  15. $dir = sys_get_temp_dir() . "/doctrine_cache_". uniqid();
  16. $this->assertFalse(is_dir($dir));
  17. $this->driver = new PhpFileCache($dir);
  18. $this->assertTrue(is_dir($dir));
  19. return $this->driver;
  20. }
  21. public function testObjects()
  22. {
  23. $this->markTestSkipped('PhpFileCache does not support saving objects that dont implement __set_state()');
  24. }
  25. public function testLifetime()
  26. {
  27. $cache = $this->_getCacheDriver();
  28. // Test save
  29. $cache->save('test_key', 'testing this out', 10);
  30. // Test contains to test that save() worked
  31. $this->assertTrue($cache->contains('test_key'));
  32. // Test fetch
  33. $this->assertEquals('testing this out', $cache->fetch('test_key'));
  34. // access private methods
  35. $getFilename = new \ReflectionMethod($cache, 'getFilename');
  36. $getNamespacedId = new \ReflectionMethod($cache, 'getNamespacedId');
  37. $getFilename->setAccessible(true);
  38. $getNamespacedId->setAccessible(true);
  39. $id = $getNamespacedId->invoke($cache, 'test_key');
  40. $path = $getFilename->invoke($cache, $id);
  41. $value = include $path;
  42. // update lifetime
  43. $value['lifetime'] = $value['lifetime'] - 20;
  44. file_put_contents($path, '<?php return unserialize(' . var_export(serialize($value), true) . ');');
  45. // test expired data
  46. $this->assertFalse($cache->contains('test_key'));
  47. $this->assertFalse($cache->fetch('test_key'));
  48. }
  49. public function testImplementsSetState()
  50. {
  51. $cache = $this->_getCacheDriver();
  52. // Test save
  53. $cache->save('test_set_state', new SetStateClass(array(1,2,3)));
  54. //Test __set_state call
  55. $this->assertCount(0, SetStateClass::$values);
  56. // Test fetch
  57. $value = $cache->fetch('test_set_state');
  58. $this->assertInstanceOf('Doctrine\Tests\Common\Cache\SetStateClass', $value);
  59. $this->assertEquals(array(1,2,3), $value->getValue());
  60. //Test __set_state call
  61. $this->assertCount(1, SetStateClass::$values);
  62. // Test contains
  63. $this->assertTrue($cache->contains('test_set_state'));
  64. }
  65. public function testNotImplementsSetState()
  66. {
  67. $cache = $this->_getCacheDriver();
  68. $this->setExpectedException('InvalidArgumentException');
  69. $cache->save('test_not_set_state', new NotSetStateClass(array(1,2,3)));
  70. }
  71. public function testGetStats()
  72. {
  73. $cache = $this->_getCacheDriver();
  74. $stats = $cache->getStats();
  75. $this->assertNull($stats);
  76. }
  77. public function tearDown()
  78. {
  79. if (!$this->driver) {
  80. return;
  81. }
  82. $dir = $this->driver->getDirectory();
  83. $ext = $this->driver->getExtension();
  84. $iterator = new \RecursiveDirectoryIterator($dir);
  85. foreach (new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST) as $file) {
  86. if ($file->isFile()) {
  87. @unlink($file->getRealPath());
  88. } else {
  89. @rmdir($file->getRealPath());
  90. }
  91. }
  92. }
  93. }
  94. class NotSetStateClass
  95. {
  96. private $value;
  97. public function __construct($value)
  98. {
  99. $this->value = $value;
  100. }
  101. public function getValue()
  102. {
  103. return $this->value;
  104. }
  105. }
  106. class SetStateClass extends NotSetStateClass
  107. {
  108. public static $values = array();
  109. public static function __set_state($data)
  110. {
  111. self::$values = $data;
  112. return new self($data['value']);
  113. }
  114. }