AbstractPoolTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /*
  3. * This file is part of the Stash package.
  4. *
  5. * (c) Robert Hafner <tedivm@tedivm.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Stash\Test;
  11. use Stash\Exception\InvalidArgumentException;
  12. use Stash\Pool;
  13. use Stash\Driver\Ephemeral;
  14. use Stash\Test\Stubs\LoggerStub;
  15. use Stash\Test\Stubs\DriverExceptionStub;
  16. /**
  17. * @package Stash
  18. * @author Robert Hafner <tedivm@tedivm.com>
  19. */
  20. class AbstractPoolTest extends \PHPUnit_Framework_TestCase
  21. {
  22. protected $data = array(array('test', 'test'));
  23. protected $multiData = array('key' => 'value',
  24. 'key1' => 'value1',
  25. 'key2' => 'value2',
  26. 'key3' => 'value3');
  27. protected $poolClass = '\Stash\Pool';
  28. public function testSetDriver()
  29. {
  30. $driver = new Ephemeral();
  31. $pool = new $this->poolClass($driver);
  32. $this->assertAttributeEquals($driver, 'driver', $pool);
  33. }
  34. public function testSetItemDriver()
  35. {
  36. $pool = $this->getTestPool();
  37. $stash = $pool->getItem('test');
  38. $this->assertAttributeInstanceOf('Stash\Driver\Ephemeral', 'driver', $stash, 'set driver is pushed to new stash objects');
  39. }
  40. public function testSetItemClass()
  41. {
  42. $mockItem = $this->getMock('Stash\Interfaces\ItemInterface');
  43. $mockClassName = get_class($mockItem);
  44. $pool = $this->getTestPool();
  45. $this->assertTrue($pool->setItemClass($mockClassName));
  46. $this->assertAttributeEquals($mockClassName, 'itemClass', $pool);
  47. }
  48. public function testSetItemClassFakeClassException()
  49. {
  50. try {
  51. $pool = $this->getTestPool();
  52. $pool->setItemClass('FakeClassName');
  53. } catch (\Exception $expected) {
  54. return;
  55. }
  56. $this->fail('An expected exception has not been raised.');
  57. }
  58. public function testSetItemClassImproperClassException()
  59. {
  60. try {
  61. $pool = $this->getTestPool();
  62. $pool->setItemClass('\stdClass');
  63. } catch (\Exception $expected) {
  64. return;
  65. }
  66. $this->fail('An expected exception has not been raised.');
  67. }
  68. public function testGetItem()
  69. {
  70. $pool = $this->getTestPool();
  71. $stash = $pool->getItem('base/one');
  72. $this->assertInstanceOf('Stash\Item', $stash, 'getItem returns a Stash\Item object');
  73. $stash->set($this->data)->save();
  74. $storedData = $stash->get();
  75. $this->assertEquals($this->data, $storedData, 'getItem returns working Stash\Item object');
  76. $key = $stash->getKey();
  77. $this->assertEquals('base/one', $key, 'Pool sets proper Item key.');
  78. $pool->setNamespace('TestNamespace');
  79. $item = $pool->getItem('test/item');
  80. $this->assertAttributeEquals('TestNamespace', 'namespace', $item, 'Pool sets Item namespace.');
  81. }
  82. public function testSaveItem()
  83. {
  84. $pool = $this->getTestPool();
  85. $this->assertFalse($pool->hasItem('base/one'), 'Pool->hasItem() returns false for item without stored data.');
  86. $item = $pool->getItem('base/one');
  87. $this->assertInstanceOf('Stash\Item', $item, 'getItem returns a Stash\Item object');
  88. $key = $item->getKey();
  89. $this->assertEquals('base/one', $key, 'Pool sets proper Item key.');
  90. $item->set($this->data);
  91. $this->assertTrue($pool->save($item), 'Pool->save() returns true.');
  92. $storedData = $item->get();
  93. $this->assertEquals($this->data, $storedData, 'Pool->save() returns proper data on passed Item.');
  94. $item = $pool->getItem('base/one');
  95. $storedData = $item->get();
  96. $this->assertEquals($this->data, $storedData, 'Pool->save() returns proper data on new Item instance.');
  97. $this->assertTrue($pool->hasItem('base/one'), 'Pool->hasItem() returns true for item with stored data.');
  98. $pool->setNamespace('TestNamespace');
  99. $item = $pool->getItem('test/item');
  100. $this->assertAttributeEquals('TestNamespace', 'namespace', $item, 'Pool sets Item namespace.');
  101. }
  102. public function testSaveDeferredItem()
  103. {
  104. $pool = $this->getTestPool();
  105. $this->assertFalse($pool->hasItem('base/one'), 'Pool->hasItem() returns false for item without stored data.');
  106. $item = $pool->getItem('base/one');
  107. $this->assertInstanceOf('Stash\Item', $item, 'getItem returns a Stash\Item object');
  108. $key = $item->getKey();
  109. $this->assertEquals('base/one', $key, 'Pool sets proper Item key.');
  110. $item->set($this->data);
  111. $this->assertTrue($pool->saveDeferred($item), 'Pool->save() returns true.');
  112. $storedData = $item->get();
  113. $this->assertEquals($this->data, $storedData, 'Pool->save() returns proper data on passed Item.');
  114. $item = $pool->getItem('base/one');
  115. $storedData = $item->get();
  116. $this->assertEquals($this->data, $storedData, 'Pool->save() returns proper data on new Item instance.');
  117. $this->assertTrue($pool->hasItem('base/one'), 'Pool->hasItem() returns true for item with stored data.');
  118. $pool->setNamespace('TestNamespace');
  119. $item = $pool->getItem('test/item');
  120. $this->assertAttributeEquals('TestNamespace', 'namespace', $item, 'Pool sets Item namespace.');
  121. }
  122. public function testHasItem()
  123. {
  124. $pool = $this->getTestPool();
  125. $this->assertFalse($pool->hasItem('base/one'), 'Pool->hasItem() returns false for item without stored data.');
  126. $item = $pool->getItem('base/one');
  127. $item->set($this->data);
  128. $pool->save($item);
  129. $this->assertTrue($pool->hasItem('base/one'), 'Pool->hasItem() returns true for item with stored data.');
  130. }
  131. public function testCommit()
  132. {
  133. $pool = $this->getTestPool();
  134. $this->assertTrue($pool->commit());
  135. }
  136. /**
  137. * @expectedException InvalidArgumentException
  138. * @expectedExceptionMessage Invalid or Empty Node passed to getItem constructor.
  139. */
  140. public function testGetItemInvalidKeyMissingNode()
  141. {
  142. $pool = $this->getTestPool();
  143. $item = $pool->getItem('This/Test//Fail');
  144. }
  145. public function testGetItems()
  146. {
  147. $pool = $this->getTestPool();
  148. $keys = array_keys($this->multiData);
  149. $cacheIterator = $pool->getItems($keys);
  150. $keyData = $this->multiData;
  151. foreach ($cacheIterator as $key => $stash) {
  152. $this->assertTrue($stash->isMiss(), 'new Cache in iterator is empty');
  153. $stash->set($keyData[$key])->save();
  154. unset($keyData[$key]);
  155. }
  156. $this->assertCount(0, $keyData, 'all keys are accounted for the in cache iterator');
  157. $cacheIterator = $pool->getItems($keys);
  158. foreach ($cacheIterator as $key => $stash) {
  159. $this->assertEquals($key, $stash->getKey(), 'Item key is not equals key in iterator');
  160. $data = $stash->get($key);
  161. $this->assertEquals($this->multiData[$key], $data, 'data put into the pool comes back the same through iterators.');
  162. }
  163. }
  164. public function testDeleteItems()
  165. {
  166. $pool = $this->getTestPool();
  167. $keys = array_keys($this->multiData);
  168. $cacheIterator = $pool->getItems($keys);
  169. $keyData = $this->multiData;
  170. foreach ($cacheIterator as $stash) {
  171. $key = $stash->getKey();
  172. $this->assertTrue($stash->isMiss(), 'new Cache in iterator is empty');
  173. $stash->set($keyData[$key])->save();
  174. unset($keyData[$key]);
  175. }
  176. $this->assertCount(0, $keyData, 'all keys are accounted for the in cache iterator');
  177. $cacheIterator = $pool->getItems($keys);
  178. foreach ($cacheIterator as $item) {
  179. $key = $item->getKey();
  180. $data = $item->get($key);
  181. $this->assertEquals($this->multiData[$key], $data, 'data put into the pool comes back the same through iterators.');
  182. }
  183. $this->assertTrue($pool->deleteItems($keys), 'deleteItems returns true.');
  184. $cacheIterator = $pool->getItems($keys);
  185. foreach ($cacheIterator as $item) {
  186. $this->assertTrue($item->isMiss(), 'data cleared using deleteItems is removed from the cache.');
  187. }
  188. }
  189. public function testClearCache()
  190. {
  191. $pool = $this->getTestPool();
  192. $stash = $pool->getItem('base/one');
  193. $stash->set($this->data)->save();
  194. $this->assertTrue($pool->clear(), 'clear returns true');
  195. $stash = $pool->getItem('base/one');
  196. $this->assertNull($stash->get(), 'clear removes item');
  197. $this->assertTrue($stash->isMiss(), 'clear causes cache miss');
  198. }
  199. public function testPurgeCache()
  200. {
  201. $pool = $this->getTestPool();
  202. $stash = $pool->getItem('base/one');
  203. $stash->set($this->data)->expiresAfter(-600)->save();
  204. $this->assertTrue($pool->purge(), 'purge returns true');
  205. $stash = $pool->getItem('base/one');
  206. $this->assertNull($stash->get(), 'purge removes item');
  207. $this->assertTrue($stash->isMiss(), 'purge causes cache miss');
  208. }
  209. public function testNamespacing()
  210. {
  211. $pool = $this->getTestPool();
  212. $this->assertAttributeEquals(null, 'namespace', $pool, 'Namespace starts empty.');
  213. $this->assertTrue($pool->setNamespace('TestSpace'), 'setNamespace returns true.');
  214. $this->assertAttributeEquals('TestSpace', 'namespace', $pool, 'setNamespace sets the namespace.');
  215. $this->assertEquals('TestSpace', $pool->getNamespace(), 'getNamespace returns current namespace.');
  216. $this->assertTrue($pool->setNamespace(), 'setNamespace returns true when setting null.');
  217. $this->assertAttributeEquals(null, 'namespace', $pool, 'setNamespace() empties namespace.');
  218. $this->assertFalse($pool->getNamespace(), 'getNamespace returns false when no namespace is set.');
  219. }
  220. /**
  221. * @expectedException InvalidArgumentException
  222. * @expectedExceptionMessage Namespace must be alphanumeric.
  223. */
  224. public function testInvalidNamespace()
  225. {
  226. $pool = $this->getTestPool();
  227. $pool->setNamespace('!@#$%^&*(');
  228. }
  229. public function testSetLogger()
  230. {
  231. $pool = $this->getTestPool();
  232. $driver = new DriverExceptionStub();
  233. $pool->setDriver($driver);
  234. $logger = new LoggerStub();
  235. $pool->setLogger($logger);
  236. $this->assertAttributeInstanceOf('Stash\Test\Stubs\LoggerStub', 'logger', $pool, 'setLogger injects logger into Pool.');
  237. $item = $pool->getItem('testItem');
  238. $this->assertAttributeInstanceOf('Stash\Test\Stubs\LoggerStub', 'logger', $item, 'setLogger injects logger into Pool.');
  239. }
  240. public function testLoggerClear()
  241. {
  242. $pool = $this->getTestPool();
  243. $driver = new DriverExceptionStub();
  244. $pool->setDriver($driver);
  245. $logger = new LoggerStub();
  246. $pool->setLogger($logger);
  247. // triggerlogging
  248. $pool->clear();
  249. $this->assertInstanceOf('Stash\Test\Exception\TestException',
  250. $logger->lastContext['exception'], 'Logger was passed exception in event context.');
  251. $this->assertTrue(strlen($logger->lastMessage) > 0, 'Logger message set after "get" exception.');
  252. $this->assertEquals('critical', $logger->lastLevel, 'Exceptions logged as critical.');
  253. }
  254. public function testLoggerPurge()
  255. {
  256. $pool = $this->getTestPool();
  257. $driver = new DriverExceptionStub();
  258. $pool->setDriver($driver);
  259. $logger = new LoggerStub();
  260. $pool->setLogger($logger);
  261. // triggerlogging
  262. $pool->purge();
  263. $this->assertInstanceOf('Stash\Test\Exception\TestException',
  264. $logger->lastContext['exception'], 'Logger was passed exception in event context.');
  265. $this->assertTrue(strlen($logger->lastMessage) > 0, 'Logger message set after "set" exception.');
  266. $this->assertEquals('critical', $logger->lastLevel, 'Exceptions logged as critical.');
  267. }
  268. /**
  269. * @return \Stash\Pool
  270. */
  271. protected function getTestPool()
  272. {
  273. return new $this->poolClass();
  274. }
  275. }