PageRenderTime 109ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/CacheDbaTest.php

https://bitbucket.org/gjerokrsteski/php-dba-cache
PHP | 182 lines | 126 code | 32 blank | 24 comment | 0 complexity | c1a6046f9d52c1a5c37998339018593c MD5 | raw file
  1. <?php
  2. require_once dirname(dirname(__FILE__)) . '/src/CacheDba.php';
  3. require_once dirname(dirname(__FILE__)) . '/src/CacheSerializer.php';
  4. require_once dirname(__FILE__) .'/DummyFixtures.php';
  5. class CacheDbaTest extends PHPUnit_Framework_TestCase
  6. {
  7. /**
  8. * @var CacheDba
  9. */
  10. private $_cache;
  11. /**
  12. * Prepares the environment before running a test.
  13. */
  14. protected function setUp()
  15. {
  16. parent::setUp();
  17. try {
  18. $this->_cache = new CacheDba(dirname(dirname(__FILE__)) . '/tests/_drafts/cache.flat');
  19. } catch(RuntimeException $e) {
  20. $this->markTestSkipped($e->getMessage());
  21. }
  22. }
  23. /**
  24. * Cleans up the environment after running a test.
  25. */
  26. protected function tearDown()
  27. {
  28. unset($this->_cache);
  29. parent::tearDown();
  30. }
  31. public function objectsProvider()
  32. {
  33. $stdClass = new stdClass();
  34. $stdClass->title = 'Zweiundvierz';
  35. $stdClass->from = 'Joe';
  36. $stdClass->to = 'Jane';
  37. $stdClass->body = 'Ich kenne die Antwort -- aber was ist die Frage?';
  38. return array(
  39. array(
  40. md5('stdClass'),
  41. $stdClass
  42. ),
  43. array(
  44. md5('ZipArchive'),
  45. new ZipArchive()
  46. ),
  47. array(
  48. md5('XMLReader'),
  49. new XMLReader()
  50. ),
  51. array(
  52. md5('Dummy'),
  53. new Dummy()
  54. )
  55. );
  56. }
  57. /**
  58. * @depends CacheDbaTest::testCreateNewCacheObjectNoException
  59. * @dataProvider objectsProvider
  60. */
  61. public function testPutSomeObjectsIntoTheCache($identifier, $object)
  62. {
  63. try {
  64. $this->_cache->put($identifier, $object);
  65. } catch (Exception $e) {
  66. $this->fail(
  67. $e->getMessage()
  68. );
  69. }
  70. }
  71. /**
  72. * @depends CacheDbaTest::testPutSomeObjectsIntoTheCache
  73. * @dataProvider objectsProvider
  74. */
  75. public function testGetSomeObjectsFromTheCacheAndCompareEachother($identifier, $expectedObject)
  76. {
  77. try {
  78. $this->assertTrue($this->_cache->has($identifier));
  79. $getObject = $this->_cache->get($identifier);
  80. $this->assertEquals($expectedObject, $getObject);
  81. } catch (Exception $e) {
  82. $this->fail(
  83. $e->getMessage()
  84. );
  85. }
  86. }
  87. public function testReadAllIdsInCache()
  88. {
  89. $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache-get-all-ids.flat';
  90. try {
  91. $cache = new CacheDba(
  92. $path, 'flatfile', 'c', false
  93. );
  94. } catch(RuntimeException $e) {
  95. $this->markTestSkipped($e->getMessage());
  96. }
  97. $cache->put('array-1', array( 1 ));
  98. $cache->put('string-2', 'some big string');
  99. $cache->put('float-3', 1234.87987698);
  100. $ids = $cache->getIds();
  101. $this->assertInstanceOf('ArrayObject', $ids);
  102. $this->assertEquals($ids[0], 'array-1');
  103. $this->assertEquals($ids[1], 'string-2');
  104. $this->assertEquals($ids[2], 'float-3');
  105. unlink($path);
  106. }
  107. public function testPutTheSameIdentifierTwiceToFlatfileHandler()
  108. {
  109. $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache-insert.flat';
  110. try {
  111. $cache = new CacheDba($path, 'flatfile', 'c', false);
  112. } catch(RuntimeException $e) {
  113. $this->markTestSkipped($e->getMessage());
  114. }
  115. // first insert.
  116. $this->assertTrue($cache->put('key', 'data'));
  117. // replace instead of insert.
  118. $this->assertTrue($cache->put('key', 'data-2'));
  119. //check if data replaced.
  120. $this->assertEquals('data-2', $cache->get('key'));
  121. }
  122. public function testPutTheSameIdentifierTwiceToDb4Handler()
  123. {
  124. $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache.db4';
  125. try {
  126. $cache = new CacheDba($path, 'db4');
  127. } catch(RuntimeException $e) {
  128. $this->markTestSkipped($e->getMessage());
  129. }
  130. // first insert.
  131. $this->assertTrue($cache->put('key', 'data'));
  132. // replace instead of insert.
  133. $this->assertTrue($cache->put('key', 'data-2'));
  134. //check if data replaced.
  135. $this->assertEquals('data-2', $cache->get('key'));
  136. }
  137. public function testLoadingMetadata()
  138. {
  139. $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache.db4';
  140. try {
  141. $cache = new CacheDba($path, 'db4');
  142. } catch(RuntimeException $e) {
  143. $this->markTestSkipped($e->getMessage());
  144. }
  145. // first insert.
  146. $this->assertTrue($cache->put('key', 'data'));
  147. $this->assertInternalType('array', $cache->getMetaData('key'));
  148. $this->assertNotEmpty($cache->getMetaData('key'));
  149. }
  150. }