PageRenderTime 44ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Cache/BaseTest.php

https://bitbucket.org/webvariants/babelcache
PHP | 248 lines | 153 code | 48 blank | 47 comment | 2 complexity | e3f03047e4f499f6640560a02cd13e65 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
  4. *
  5. * This file is released under the terms of the MIT license. You can find the
  6. * complete text in the attached LICENSE file or online at:
  7. *
  8. * http://www.opensource.org/licenses/mit-license.php
  9. */
  10. abstract class Cache_BaseTest extends PHPUnit_Framework_TestCase {
  11. abstract protected function getCache();
  12. public function testGetNonexisting() {
  13. $cache = $this->getCache();
  14. $this->assertSame(12, $cache->get('t.foo', 'key', 12));
  15. $this->assertFalse($cache->get('t.foo', 'key', false));
  16. $this->assertNull($cache->get('t.foo', 'key', null));
  17. }
  18. public function testSetPrefix() {
  19. $cache = $this->getCache();
  20. $cache->setPrefix('foobar');
  21. $cache->set('t.foo', 'key', 'value');
  22. $cache->setPrefix('qux');
  23. $this->assertSame('default', $cache->get('t.foo', 'key', 'default'));
  24. $cache->set('t.foo', 'key', 'mumblefoo');
  25. $this->assertSame('mumblefoo', $cache->get('t.foo', 'key'));
  26. $cache->setPrefix('foobar');
  27. $this->assertSame('value', $cache->get('t.foo', 'key'));
  28. }
  29. /**
  30. * @dataProvider setGetProvider
  31. */
  32. public function testSetGet($namespace, $key, $value) {
  33. $cache = $this->getCache();
  34. $result = $cache->set($namespace, $key, $value);
  35. $this->assertSame($value, $result, 'set should return the set value');
  36. $read = $cache->get($namespace, $key, null, $found);
  37. // test if found was set
  38. $this->assertTrue($found, 'value was not found, but it should be there.');
  39. if (is_object($value)) {
  40. // we do not require the same identity
  41. $this->assertEquals($value, $read, 'value should be an equal object');
  42. }
  43. else {
  44. $this->assertSame($value, $read, 'value should be available immediately');
  45. }
  46. }
  47. public function setGetProvider() {
  48. $foo = new stdClass();
  49. $foo->x = 1;
  50. return array(
  51. array('t.foo.bar', 'blub', 1),
  52. array('t.foo.bar', 'blub', 3.41),
  53. array('t.foo.bar', 'blub', false),
  54. array('t.foo.bar', 'blub', true),
  55. array('t.foo.bar', 'blub', null),
  56. array('t.foo.bar', 'blub', ''),
  57. array('t.foo.bar', 'blub', 'i am a string!'),
  58. array('t.foo.bar', 'blub', array()),
  59. array('t.foo.bar', 'blub', array(null)),
  60. array('t.foo.bar', 'blub', array(1)),
  61. array('t.foo.bar', 'blub', new stdClass()),
  62. array('t.foo.bar', 'blub', $foo)
  63. );
  64. }
  65. /**
  66. * @dataProvider clearProvider
  67. * @depends testSetGet
  68. */
  69. public function testClear($clearLevel, $exists1, $exists2, $exists3) {
  70. $cache = $this->getCache();
  71. $l1 = 't.AA_'.mt_rand().'_AA';
  72. $l2 = $l1.'.BB_'.mt_rand().'_BB';
  73. $l3 = $l2.'.CC_'.mt_rand().'_CC';
  74. $cache->set($l1, 'foo', 'bar');
  75. $cache->set($l2, 'foo', 'bar');
  76. $cache->set($l3, 'foo', null);
  77. $this->assertTrue($cache->clear($$clearLevel, true));
  78. $this->assertSame($exists1, $cache->exists($l1, 'foo'));
  79. $this->assertSame($exists2, $cache->exists($l2, 'foo'));
  80. $this->assertSame($exists3, $cache->exists($l3, 'foo'));
  81. }
  82. public function clearProvider() {
  83. return array(
  84. array('l3', true, true, false),
  85. array('l2', true, false, false),
  86. array('l1', false, false, false)
  87. );
  88. }
  89. /**
  90. * @depends testSetGet
  91. */
  92. public function testNonRecursiveClear() {
  93. $cache = $this->getCache();
  94. $cache->set('t', 'key', 'value');
  95. $cache->set('t.foo', 'key', 'value');
  96. $cache->set('t.foo.foo', 'key', 'value');
  97. $this->assertTrue($cache->clear('t.foo', false));
  98. $this->assertTrue($cache->exists('t', 'key'));
  99. $this->assertFalse($cache->exists('t.foo', 'key'));
  100. // This can be true or false. Both are perfectly fine.
  101. $this->assertInternalType('boolean', $cache->exists('t.foo.foo', 'key'));
  102. }
  103. /**
  104. * @depends testClear
  105. * @expectedException wv\BabelCache\Exception
  106. * @dataProvider emptyValuesProvider
  107. */
  108. public function testClearShouldRequireANamespace($namespace) {
  109. $this->getCache()->clear($namespace);
  110. }
  111. public function emptyValuesProvider() {
  112. return array(
  113. array(null),
  114. array(false),
  115. array('')
  116. );
  117. }
  118. /**
  119. * @depends testSetGet
  120. */
  121. public function testOverwritingValues() {
  122. $cache = $this->getCache();
  123. $cache->set('t.foo', 'key', 'abc');
  124. $cache->set('t.foo', 'key', 'xyz');
  125. $this->assertSame('xyz', $cache->get('t.foo', 'key'));
  126. }
  127. /**
  128. * @depends testSetGet
  129. */
  130. public function testExists() {
  131. $cache = $this->getCache();
  132. $cache->set('t.foo', 'key', 'abc');
  133. $this->assertTrue($cache->exists('t.foo', 'key'));
  134. $this->assertFalse($cache->exists('t.foo', 'KEY'));
  135. $this->assertFalse($cache->exists('t.FOO', 'KEY'));
  136. $this->assertFalse($cache->exists('t', 'foo'));
  137. }
  138. /**
  139. * @depends testExists
  140. */
  141. public function testDelete() {
  142. $cache = $this->getCache();
  143. $cache->set('t.foo', 'key', 'abc');
  144. $this->assertTrue($cache->delete('t.foo', 'key'));
  145. $this->assertFalse($cache->delete('t.foo', 'key'));
  146. $this->assertFalse($cache->exists('t.foo', 'key'));
  147. }
  148. /**
  149. * @depends testSetGet
  150. */
  151. public function testNamespaceAndKeyAreCaseSensitive() {
  152. $cache = $this->getCache();
  153. $cache->set('t.foo', 'key', 'fk content');
  154. $cache->set('t.FOO', 'key', 'Fk content');
  155. $this->assertTrue($cache->exists('t.foo', 'key'));
  156. $this->assertTrue($cache->exists('t.FOO', 'key'));
  157. $this->assertFalse($cache->exists('t.Foo', 'key'));
  158. $this->assertFalse($cache->exists('t.foo', 'KEY'));
  159. $cache->set('t.foo', 'KEY', 'fK content');
  160. $this->assertTrue($cache->exists('t.foo', 'key'));
  161. $this->assertTrue($cache->exists('t.FOO', 'key'));
  162. $this->assertFalse($cache->exists('t.Foo', 'key'));
  163. $this->assertTrue($cache->exists('t.foo', 'KEY'));
  164. $this->assertSame('fk content', $cache->get('t.foo', 'key'));
  165. $this->assertSame('Fk content', $cache->get('t.FOO', 'key'));
  166. $this->assertSame('fK content', $cache->get('t.foo', 'KEY'));
  167. $cache->delete('t.foo', 'key');
  168. $this->assertFalse($cache->exists('t.foo', 'key'));
  169. $this->assertTrue($cache->exists('t.FOO', 'key'));
  170. $this->assertTrue($cache->exists('t.foo', 'KEY'));
  171. }
  172. public function testLocking() {
  173. $cache = $this->getCache();
  174. // there should be no lock already
  175. $this->assertFalse($cache->hasLock('t.locks', 'foo'));
  176. $this->assertFalse($cache->hasLock('t.locks', 'bar'));
  177. // we should only be able to lock once
  178. $this->assertTrue($cache->lock('t.locks', 'foo'));
  179. $this->assertFalse($cache->lock('t.locks', 'foo'));
  180. // but locking other keys should still work
  181. $this->assertTrue($cache->lock('t.locks', 'bar'));
  182. // now we have locks
  183. $this->assertTrue($cache->hasLock('t.locks', 'foo'));
  184. $this->assertTrue($cache->hasLock('t.locks', 'bar'));
  185. // a clear should delete the locks as well
  186. $cache->clear('x');
  187. // locks are gone
  188. $this->assertFalse($cache->hasLock('t.locks', 'foo'));
  189. $this->assertFalse($cache->hasLock('t.locks', 'bar'));
  190. // ... so we can lock again
  191. $this->assertTrue($cache->lock('t.locks', 'foo'));
  192. $this->assertTrue($cache->hasLock('t.locks', 'foo'));
  193. // unlocking should have the same effect
  194. $this->assertTrue($cache->unlock('t.locks', 'foo'));
  195. $this->assertFalse($cache->hasLock('t.locks', 'foo'));
  196. $this->assertTrue($cache->lock('t.locks', 'foo'));
  197. // unlocking a non-existing lock for a key should not work
  198. $this->assertFalse($cache->unlock('t.locks', 'bar'));
  199. }
  200. }