PageRenderTime 66ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/CacheDbaHandlersTest.php

https://bitbucket.org/gjerokrsteski/php-dba-cache
PHP | 164 lines | 100 code | 34 blank | 30 comment | 0 complexity | d2c88d391f431284c1cd32e32a865c5f 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 CacheDbaHandlersTest extends PHPUnit_Framework_TestCase
  6. {
  7. /**
  8. * @var stdClass
  9. */
  10. protected $_object;
  11. /**
  12. * @var string
  13. */
  14. protected $_identifier;
  15. /**
  16. * Prepares the environment before running a test.
  17. */
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $stdClass = new stdClass();
  22. $stdClass->title = 'Zweiundvierz';
  23. $stdClass->from = 'Joe';
  24. $stdClass->to = 'Jane';
  25. $stdClass->body = new Dummy();
  26. $this->_object = $stdClass;
  27. $this->_identifier = md5('stdClass' . time());
  28. }
  29. /**
  30. * Cleans up the environment after running a test.
  31. */
  32. protected function tearDown()
  33. {
  34. unset($this->_object, $this->_identifier);
  35. parent::tearDown();
  36. }
  37. /**
  38. * Tests support for Oracle Berkeley DB 4.
  39. */
  40. public function testOracleBerkeleyDb4HandlerSupportWithoutPersistantConnection()
  41. {
  42. try {
  43. $cache = new CacheDba(
  44. dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache.db4', 'db4', 'c', false
  45. );
  46. } catch(RuntimeException $e) {
  47. $this->markTestSkipped($e->getMessage());
  48. }
  49. $this->assertInstanceOf('CacheDba', $cache);
  50. $cache->put($this->_identifier, $this->_object);
  51. $this->assertInstanceOf('stdClass', $cache->get($this->_identifier));
  52. }
  53. /**
  54. * @depends CacheDbaHandlersTest::testOracleBerkeleyDb4HandlerSupportWithoutPersistantConnection
  55. */
  56. public function testOracleBerkeleyDb4HandlerBeSupportedWithPersistantConnection()
  57. {
  58. try {
  59. $cache = new CacheDba(dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache.db4', 'db4');
  60. } catch(RuntimeException $e) {
  61. $this->markTestSkipped($e->getMessage());
  62. }
  63. $this->assertInstanceOf('CacheDba', $cache);
  64. $cache->put($this->_identifier, $this->_object);
  65. $this->assertInstanceOf('stdClass', $cache->get($this->_identifier));
  66. }
  67. /**
  68. * Tests support for CDB - Tiny Constant Database.
  69. */
  70. public function testCanCdbHandlerOnlyNewAndReadBeSupportedWithPersistantConnection()
  71. {
  72. $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache-cdb2.cdb';
  73. // create handler to write.
  74. try {
  75. $cacheMake = new CacheDba($path, 'cdb_make', 'n');
  76. } catch(RuntimeException $e) {
  77. $this->markTestSkipped($e->getMessage());
  78. }
  79. $this->assertInstanceOf('CacheDba', $cacheMake);
  80. $this->assertTrue($cacheMake->put(md5('test123'), $this->_object));
  81. // for read we close the handler.
  82. $cacheMake->closeDba();
  83. // create handler to read.
  84. try {
  85. $cacheRead = new CacheDba($path, 'cdb', 'r');
  86. } catch(RuntimeException $e) {
  87. $this->markTestSkipped($e->getMessage());
  88. }
  89. $this->assertTrue($cacheRead->has(md5('test123')));
  90. $this->assertInstanceOf('stdClass', $cacheRead->get(md5('test123')));
  91. $cacheRead->closeDba();
  92. }
  93. public function testCreateAnCdbHandler()
  94. {
  95. $path = dirname(dirname(__FILE__)) . '/tests/_drafts/simple-xml-test-cache-on-cdb.db';
  96. try {
  97. new CacheDba(
  98. $path, "cdb", "n"
  99. );
  100. unlink($path);
  101. } catch(RuntimeException $e) {
  102. $this->markTestSkipped($e->getMessage());
  103. }
  104. }
  105. public function testPutTheSameIdentifierTwiceToCdbHandler()
  106. {
  107. $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache-cdb2.cdb';
  108. // CREATE HANDLER TO WRITE.
  109. try {
  110. $cacheMake = new CacheDba($path, 'cdb_make', 'n');
  111. } catch(RuntimeException $e) {
  112. $this->markTestSkipped($e->getMessage());
  113. }
  114. // first insert.
  115. $this->assertTrue($cacheMake->put('key', 'data'));
  116. // replace instead of insert.
  117. $this->assertTrue($cacheMake->put('key', 'data-2'));
  118. // for read we close the handler.
  119. $cacheMake->closeDba();
  120. // CREATE HANDLER TO READ.
  121. try {
  122. $cacheRead = new CacheDba($path, 'cdb', 'r');
  123. } catch(RuntimeException $e) {
  124. $this->markTestSkipped($e->getMessage());
  125. }
  126. //check if data replaced.
  127. $this->assertEquals('data', $cacheRead->get('key'));
  128. $cacheRead->closeDba();
  129. }
  130. }