CacheTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Doctrine\Tests\Common\Cache;
  3. use Doctrine\Common\Cache\Cache;
  4. abstract class CacheTest extends \Doctrine\Tests\DoctrineTestCase
  5. {
  6. public function testBasics()
  7. {
  8. $cache = $this->_getCacheDriver();
  9. // Test save
  10. $cache->save('test_key', 'testing this out');
  11. // Test contains to test that save() worked
  12. $this->assertTrue($cache->contains('test_key'));
  13. // Test fetch
  14. $this->assertEquals('testing this out', $cache->fetch('test_key'));
  15. // Test delete
  16. $cache->save('test_key2', 'test2');
  17. $cache->delete('test_key2');
  18. $this->assertFalse($cache->contains('test_key2'));
  19. }
  20. public function testObjects()
  21. {
  22. $cache = $this->_getCacheDriver();
  23. // Fetch/save test with objects (Is cache driver serializes/unserializes objects correctly ?)
  24. $cache->save('test_object_key', new \ArrayObject());
  25. $this->assertTrue($cache->fetch('test_object_key') instanceof \ArrayObject);
  26. }
  27. public function testDeleteAll()
  28. {
  29. $cache = $this->_getCacheDriver();
  30. $cache->save('test_key1', '1');
  31. $cache->save('test_key2', '2');
  32. $cache->deleteAll();
  33. $this->assertFalse($cache->contains('test_key1'));
  34. $this->assertFalse($cache->contains('test_key2'));
  35. }
  36. public function testFlushAll()
  37. {
  38. $cache = $this->_getCacheDriver();
  39. $cache->save('test_key1', '1');
  40. $cache->save('test_key2', '2');
  41. $cache->flushAll();
  42. $this->assertFalse($cache->contains('test_key1'));
  43. $this->assertFalse($cache->contains('test_key2'));
  44. }
  45. public function testNamespace()
  46. {
  47. $cache = $this->_getCacheDriver();
  48. $cache->setNamespace('test_');
  49. $cache->save('key1', 'test');
  50. $this->assertTrue($cache->contains('key1'));
  51. $cache->setNamespace('test2_');
  52. $this->assertFalse($cache->contains('key1'));
  53. }
  54. /**
  55. * @group DCOM-43
  56. */
  57. public function testGetStats()
  58. {
  59. $cache = $this->_getCacheDriver();
  60. $stats = $cache->getStats();
  61. $this->assertArrayHasKey(Cache::STATS_HITS, $stats);
  62. $this->assertArrayHasKey(Cache::STATS_MISSES, $stats);
  63. $this->assertArrayHasKey(Cache::STATS_UPTIME, $stats);
  64. $this->assertArrayHasKey(Cache::STATS_MEMORY_USAGE, $stats);
  65. $this->assertArrayHasKey(Cache::STATS_MEMORY_AVAILABLE, $stats);
  66. }
  67. /**
  68. * Make sure that all supported caches return "false" instead of "null" to be compatible
  69. * with ORM integration.
  70. */
  71. public function testFalseOnFailedFetch()
  72. {
  73. $cache = $this->_getCacheDriver();
  74. $result = $cache->fetch('nonexistent_key');
  75. $this->assertFalse($result);
  76. $this->assertNotNull($result);
  77. }
  78. /**
  79. * @return \Doctrine\Common\Cache\CacheProvider
  80. */
  81. abstract protected function _getCacheDriver();
  82. }