PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/library/Centurion/Cache/CacheTest.php

http://github.com/centurion-project/Centurion
PHP | 133 lines | 89 code | 32 blank | 12 comment | 4 complexity | bdffc502cc9988b14cde86b6e9baf205 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  3. class Centurion_Cache_CacheTest extends PHPUnit_Framework_TestCase
  4. {
  5. /**
  6. *
  7. * Enter description here ...
  8. * @return Centurion_Cache_Manager
  9. */
  10. public function getCacheManager()
  11. {
  12. global $application;
  13. return $application->getBootstrap()->getResource('cachemanager');
  14. }
  15. protected function setUp()
  16. {
  17. $cacheManager = $this->getCacheManager();
  18. if (is_null($cacheManager)) {
  19. $this->fail('Could not find cache manager in test Centurion_Cache_CacheTest');
  20. }
  21. //TODO: fix this, we don't have to force signal to be connected, it must already been connected. PB came frome Centurion_Test_PHPUnit_ControllerTestCase::tearDown() with Centurion_Signal::unregister(); command
  22. $this->getCacheManager()->connectSignal();
  23. $this->getCacheManager()->cleanCache(Zend_Cache::CLEANING_MODE_ALL);
  24. }
  25. protected function tearDown()
  26. {
  27. $cacheManager = $this->getCacheManager();
  28. if (is_null($cacheManager)) {
  29. $this->fail();
  30. }
  31. $this->getCacheManager()->cleanCache(Zend_Cache::CLEANING_MODE_ALL);
  32. }
  33. public function testFilters()
  34. {
  35. $cacheCore = $this->getCacheManager()->getCache('core');
  36. $str = sha1(uniqid());
  37. $id = sha1(uniqid());
  38. $tag = sha1(uniqid());
  39. $cacheCore->save($str, $id);
  40. $this->assertEquals($str, $cacheCore->load($id));
  41. //Test remove by Id
  42. $cacheCore->remove($id);
  43. $this->assertFalse($cacheCore->load($id));
  44. $cacheCore->save($str, $id, array($tag));
  45. $this->assertEquals($str, $cacheCore->load($id));
  46. //Test remove by tag
  47. $cacheCore->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array($tag));
  48. $this->assertFalse($cacheCore->load($id));
  49. //Test timeout
  50. $cacheCore->save($str, $id, array(), 1);
  51. $this->assertEquals($str, $cacheCore->load($id));
  52. sleep(2);
  53. $cacheCore->clean(Zend_Cache::CLEANING_MODE_OLD);
  54. $this->assertFalse($cacheCore->load($id));
  55. }
  56. public function testCleanCacheSignal()
  57. {
  58. $cacheCore = $this->getCacheManager()->getCache('core');
  59. $str = sha1(uniqid());
  60. $id = sha1(uniqid());
  61. $cacheCore->save($str, $id);
  62. $this->assertEquals($str, $cacheCore->load($id));
  63. Centurion_Signal::factory('clean_cache')->send($this);
  64. $this->assertFalse($cacheCore->load($id));
  65. //TODO: finish
  66. }
  67. public function testCacheStatic()
  68. {
  69. //TODO: implement test testCacheStatic();
  70. }
  71. public function testCacheObject()
  72. {
  73. }
  74. public function testCacheWithRelation()
  75. {
  76. $userTable = Centurion_Db::getSingleton('auth/user');
  77. $userPermissionTable = Centurion_Db::getSingleton('auth/userPermission');
  78. $cacheCore = $this->getCacheManager()->getCache('core');
  79. $userTable->getOrCreate(array('username' => 'userrow for testCacheWithRelation'));
  80. $str = sha1(uniqid());
  81. $id = sha1(uniqid());
  82. $tags = array();
  83. $adminUser = $userTable->fetchRow(array('username=?' => 'userrow for testCacheWithRelation'));
  84. if (null == $adminUser) {
  85. //TODO: We should add row that we need
  86. $this->fail('Admin not found in db');
  87. }
  88. $tags[] = $adminUser->getCacheTag('user_permissions');
  89. $cacheCore->save($str, $id, $tags);
  90. $this->assertEquals($str, $cacheCore->load($id));
  91. $permission = Centurion_Db::getSingleton('auth/permission')->createRow(array('permission' => sha1(uniqid())));
  92. $permission->save();
  93. $row = $userPermissionTable->createRow(array('user_id' => $adminUser->id, 'permission_id' => $permission->id));
  94. $row->save();
  95. $this->assertFalse($cacheCore->load($id));
  96. $cacheCore->save($str, $id, $tags);
  97. $this->assertEquals($str, $cacheCore->load($id));
  98. $row->delete();
  99. $this->assertFalse($cacheCore->load($id));
  100. }
  101. }