PhpFileCacheTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Doctrine\Tests\Common\Cache;
  3. use Doctrine\Common\Cache\Cache;
  4. use Doctrine\Common\Cache\PhpFileCache;
  5. /**
  6. * @group DCOM-101
  7. */
  8. class PhpFileCacheTest extends BaseFileCacheTest
  9. {
  10. public function provideDataToCache()
  11. {
  12. $data = parent::provideDataToCache();
  13. unset($data['object'], $data['object_recursive']); // PhpFileCache only allows objects that implement __set_state() and fully support var_export()
  14. if (PHP_VERSION_ID < 70002) {
  15. unset($data['float_zero']); // var_export exports float(0) as int(0): https://bugs.php.net/bug.php?id=66179
  16. }
  17. return $data;
  18. }
  19. public function testImplementsSetState()
  20. {
  21. $cache = $this->_getCacheDriver();
  22. // Test save
  23. $cache->save('test_set_state', new SetStateClass(array(1,2,3)));
  24. //Test __set_state call
  25. $this->assertCount(0, SetStateClass::$values);
  26. // Test fetch
  27. $value = $cache->fetch('test_set_state');
  28. $this->assertInstanceOf('Doctrine\Tests\Common\Cache\SetStateClass', $value);
  29. $this->assertEquals(array(1,2,3), $value->getValue());
  30. //Test __set_state call
  31. $this->assertCount(1, SetStateClass::$values);
  32. // Test contains
  33. $this->assertTrue($cache->contains('test_set_state'));
  34. }
  35. public function testNotImplementsSetState()
  36. {
  37. $cache = $this->_getCacheDriver();
  38. $this->setExpectedException('InvalidArgumentException');
  39. $cache->save('test_not_set_state', new NotSetStateClass(array(1,2,3)));
  40. }
  41. public function testGetStats()
  42. {
  43. $cache = $this->_getCacheDriver();
  44. $stats = $cache->getStats();
  45. $this->assertNull($stats[Cache::STATS_HITS]);
  46. $this->assertNull($stats[Cache::STATS_MISSES]);
  47. $this->assertNull($stats[Cache::STATS_UPTIME]);
  48. $this->assertEquals(0, $stats[Cache::STATS_MEMORY_USAGE]);
  49. $this->assertGreaterThan(0, $stats[Cache::STATS_MEMORY_AVAILABLE]);
  50. }
  51. protected function _getCacheDriver()
  52. {
  53. return new PhpFileCache($this->directory);
  54. }
  55. }
  56. class NotSetStateClass
  57. {
  58. private $value;
  59. public function __construct($value)
  60. {
  61. $this->value = $value;
  62. }
  63. public function getValue()
  64. {
  65. return $this->value;
  66. }
  67. }
  68. class SetStateClass extends NotSetStateClass
  69. {
  70. public static $values = array();
  71. public static function __set_state($data)
  72. {
  73. self::$values = $data;
  74. return new self($data['value']);
  75. }
  76. }