CacheTest.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Doctrine\Tests\Common\Cache;
  3. abstract class CacheTest extends \Doctrine\Tests\DoctrineTestCase
  4. {
  5. public function testBasics()
  6. {
  7. $cache = $this->_getCacheDriver();
  8. // Test save
  9. $cache->save('test_key', 'testing this out');
  10. // Test contains to test that save() worked
  11. $this->assertTrue($cache->contains('test_key'));
  12. // Test fetch
  13. $this->assertEquals('testing this out', $cache->fetch('test_key'));
  14. // Test delete
  15. $cache->save('test_key2', 'test2');
  16. $cache->delete('test_key2');
  17. $this->assertFalse($cache->contains('test_key2'));
  18. }
  19. public function testDeleteAll()
  20. {
  21. $cache = $this->_getCacheDriver();
  22. $cache->save('test_key1', '1');
  23. $cache->save('test_key2', '2');
  24. $cache->deleteAll();
  25. $this->assertFalse($cache->contains('test_key1'));
  26. $this->assertFalse($cache->contains('test_key2'));
  27. }
  28. public function testDeleteByRegex()
  29. {
  30. $cache = $this->_getCacheDriver();
  31. $cache->save('test_key1', '1');
  32. $cache->save('test_key2', '2');
  33. $cache->deleteByRegex('/test_key[0-9]/');
  34. $this->assertFalse($cache->contains('test_key1'));
  35. $this->assertFalse($cache->contains('test_key2'));
  36. }
  37. public function testDeleteByPrefix()
  38. {
  39. $cache = $this->_getCacheDriver();
  40. $cache->save('test_key1', '1');
  41. $cache->save('test_key2', '2');
  42. $cache->deleteByPrefix('test_key');
  43. $this->assertFalse($cache->contains('test_key1'));
  44. $this->assertFalse($cache->contains('test_key2'));
  45. }
  46. public function testDeleteBySuffix()
  47. {
  48. $cache = $this->_getCacheDriver();
  49. $cache->save('1test_key', '1');
  50. $cache->save('2test_key', '2');
  51. $cache->deleteBySuffix('test_key');
  52. $this->assertFalse($cache->contains('1test_key'));
  53. $this->assertFalse($cache->contains('2test_key'));
  54. }
  55. public function testDeleteByWildcard()
  56. {
  57. $cache = $this->_getCacheDriver();
  58. $cache->save('test_key1', '1');
  59. $cache->save('test_key2', '2');
  60. $cache->delete('test_key*');
  61. $this->assertFalse($cache->contains('test_key1'));
  62. $this->assertFalse($cache->contains('test_key2'));
  63. }
  64. public function testNamespace()
  65. {
  66. $cache = $this->_getCacheDriver();
  67. $cache->setNamespace('test_');
  68. $cache->save('key1', 'test');
  69. $this->assertTrue($cache->contains('key1'));
  70. $ids = $cache->getIds();
  71. $this->assertTrue(in_array('test_key1', $ids));
  72. }
  73. abstract protected function _getCacheDriver();
  74. }