You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PoolTest.php 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php declare(strict_types=1);
  2. /**
  3. * Banker
  4. *
  5. * A Caching library implementing psr/cache
  6. *
  7. * PHP version 7.1
  8. *
  9. * @package Banker
  10. * @author Timothy J. Warren <tim@timshomepage.net>
  11. * @copyright 2016 - 2018 Timothy J. Warren
  12. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  13. * @version 2.0.0
  14. * @link https://git.timshomepage.net/timw4mail/banker
  15. */
  16. namespace Aviat\Banker\Tests;
  17. use Aviat\Banker\{Item, ItemCollection, Pool};
  18. use Aviat\Banker\Exception\InvalidArgumentException;
  19. use Monolog\Logger;
  20. use Monolog\Handler\SyslogHandler;
  21. use PHPUnit\Framework\TestCase;
  22. use Psr\Log\{LoggerInterface, NullLogger};
  23. class PoolTest extends TestCase {
  24. protected $pool;
  25. public function setUp()
  26. {
  27. $this->pool = new Pool([
  28. 'driver' => 'null',
  29. 'connection' => []
  30. ]);
  31. }
  32. public function testGetDefaultLogger(): void
  33. {
  34. $friend = new Friend($this->pool);
  35. $driverFriend = new Friend($friend->driver);
  36. // Check that a valid logger is set
  37. $this->assertInstanceOf(LoggerInterface::class, $friend->getLogger(), "Logger exists after being set");
  38. $this->assertInstanceOf(LoggerInterface::class, $driverFriend->getLogger(), "Logger exists on driver after being set");
  39. // Make sure we get the default Null logger
  40. $this->assertTrue(is_a($friend->getLogger(), NullLogger::class));
  41. $this->assertTrue(is_a($driverFriend->getLogger(), NullLogger::class));
  42. }
  43. public function testSetLoggerInConstructor(): void
  44. {
  45. $logger = new Logger('test');
  46. $logger->pushHandler(new SyslogHandler(Logger::WARNING));
  47. $pool = new Pool([
  48. 'driver' => 'null',
  49. 'connection' => [],
  50. ], $logger);
  51. $friend = new Friend($pool);
  52. $driverFriend = new Friend($friend->driver);
  53. // Check that a valid logger is set
  54. $this->assertInstanceOf(LoggerInterface::class, $friend->getLogger(), "Logger exists after being set");
  55. $this->assertInstanceOf(LoggerInterface::class, $driverFriend->getLogger(), "Logger exists on driver after being set");
  56. // Make sure we aren't just getting the default Null logger
  57. $this->assertFalse(is_a($friend->getLogger(), NullLogger::class));
  58. $this->assertFalse(is_a($driverFriend->getLogger(), NullLogger::class));
  59. }
  60. public function testGetSetLogger(): void
  61. {
  62. $logger = new Logger('test');
  63. $logger->pushHandler(new SyslogHandler(Logger::WARNING));
  64. $this->pool->setLogger($logger);
  65. $friend = new Friend($this->pool);
  66. $driverFriend = new Friend($friend->driver);
  67. // Check that a valid logger is set
  68. $this->assertInstanceOf(LoggerInterface::class, $friend->getLogger(), "Logger exists after being set");
  69. $this->assertInstanceOf(LoggerInterface::class, $driverFriend->getLogger(), "Logger exists on driver after being set");
  70. // Make sure we aren't just getting the default Null logger
  71. $this->assertFalse(is_a($friend->getLogger(), NullLogger::class));
  72. $this->assertFalse(is_a($driverFriend->getLogger(), NullLogger::class));
  73. }
  74. public function testGetItem(): void
  75. {
  76. $item = $this->pool->getItem('foo');
  77. $this->assertInstanceOf(Item::class, $item);
  78. }
  79. public function testItemBadKey(): void
  80. {
  81. $this->expectException(InvalidArgumentException::class);
  82. $this->expectExceptionMessage('Cache key must be a string.');
  83. $this->pool->getItem([]);
  84. }
  85. public function testGetItemsBadKey(): void
  86. {
  87. $this->expectException(InvalidArgumentException::class);
  88. $this->pool->getItems([1,3,2]);
  89. }
  90. public function testGetItems(): void
  91. {
  92. $collection = $this->pool->getItems(['foo', 'bar', 'baz']);
  93. $this->assertInstanceOf(ItemCollection::class, $collection);
  94. foreach($collection as $item)
  95. {
  96. $this->assertInstanceOf(Item::class, $item);
  97. }
  98. }
  99. public function testGetItemsDeferredItems(): void
  100. {
  101. $this->pool->clear();
  102. $deferredValues = ['x' => 1, 'y' => 2, 'z' => 3];
  103. $keys = ['x', 'y', 'z'];
  104. foreach ($deferredValues as $key => $value)
  105. {
  106. $item = $this->pool->getItem($key)->set($value);
  107. $this->pool->saveDeferred($item);
  108. }
  109. $collection = $this->pool->getItems($keys);
  110. foreach($collection as $key => $item)
  111. {
  112. $this->assertSame($deferredValues[$key], $item->get());
  113. }
  114. $this->assertCount(3, $collection);
  115. }
  116. public function testEmptyGetItems(): void
  117. {
  118. $this->pool->clear();
  119. $collection = $this->pool->getItems();
  120. $this->assertInstanceOf(ItemCollection::class, $collection);
  121. $this->assertCount(0, $collection);
  122. }
  123. public function testHasItem(): void
  124. {
  125. $this->pool->clear();
  126. // The key doesn't exist yet
  127. $this->assertFalse($this->pool->hasItem('foobar'));
  128. // Create the item
  129. $item = $this->pool->getItem('foobar')
  130. ->set('baz')
  131. ->save();
  132. // The item exists now
  133. $this->assertTrue($this->pool->hasItem('foobar'));
  134. }
  135. public function testHasItemBadKey(): void
  136. {
  137. $this->pool->clear();
  138. $this->expectException(InvalidArgumentException::class);
  139. $this->expectExceptionMessage('Cache key must be a string.');
  140. $this->pool->hasItem(34);
  141. }
  142. public function testClear(): void
  143. {
  144. // Call clear to make sure we are working from a clean slate to start
  145. $this->pool->clear();
  146. $data = [
  147. 'foo' => 'bar',
  148. 'bar' => 'baz',
  149. 'foobar' => 'foobarbaz'
  150. ];
  151. // Set up some data
  152. $this->setupDataInCache($data);
  153. foreach($data as $key => $val)
  154. {
  155. $this->assertTrue($this->pool->hasItem($key));
  156. $item = $this->pool->getItem($key);
  157. $this->assertEquals($val, $item->get());
  158. }
  159. // Now we clear it all!
  160. $this->pool->clear();
  161. foreach($data as $key => $val)
  162. {
  163. $this->assertFalse($this->pool->hasItem($key));
  164. $item = $this->pool->getItem($key);
  165. $this->assertNull($item->get());
  166. }
  167. }
  168. public function testDeleteItemBadKey(): void
  169. {
  170. $this->expectException(InvalidArgumentException::class);
  171. $this->expectExceptionMessage("Cache key must be a string.");
  172. $this->pool->deleteItem(34);
  173. }
  174. public function testDeleteItemThatDoesNotExist(): void
  175. {
  176. $this->pool->clear();
  177. $this->assertFalse($this->pool->deleteItem('foo'));
  178. }
  179. public function testDeleteItem(): void
  180. {
  181. // Start with a clean slate
  182. $this->pool->clear();
  183. $data = [
  184. 'foo' => 'bar',
  185. 'bar' => 'baz',
  186. 'foobar' => 'foobarbaz'
  187. ];
  188. $this->setupDataInCache($data);
  189. $this->pool->deleteItem('foo');
  190. // The item no longer exists
  191. $this->assertFalse($this->pool->hasItem('foo'));
  192. $item = $this->pool->getItem('foo');
  193. $this->assertNull($item->get());
  194. // The other items still exist
  195. foreach(['bar', 'foobar'] as $key)
  196. {
  197. $this->assertTrue($this->pool->hasItem($key));
  198. $item = $this->pool->getItem($key);
  199. $this->assertFalse(is_null($item->get()));
  200. }
  201. }
  202. public function testDeleteItems(): void
  203. {
  204. $this->pool->clear();
  205. $data = [
  206. 'foo' => 'bar',
  207. 'bar' => 'baz',
  208. 'foobar' => 'foobarbaz'
  209. ];
  210. $this->setupDataInCache($data);
  211. $this->pool->deleteItems(['foo', 'bar']);
  212. foreach(['foo', 'bar'] as $key)
  213. {
  214. $this->assertFalse($this->pool->hasItem($key));
  215. $item = $this->pool->getItem($key);
  216. $this->assertNull($item->get());
  217. }
  218. }
  219. public function testSaveDeferred(): void
  220. {
  221. $this->pool->clear();
  222. $data = [
  223. 'foo' => 'bar',
  224. 'bar' => 'baz',
  225. 'foobar' => 'foobarbaz'
  226. ];
  227. $this->setupDeferredData($data);
  228. // See that the data is returned by the pool
  229. foreach($data as $key => $val)
  230. {
  231. $this->assertTrue($this->pool->hasItem($key));
  232. $item = $this->pool->getItem($key);
  233. // Since the value has been deferred,
  234. // the pool will return the updated value,
  235. // even though the cache hasn't been updated yet
  236. $this->assertEquals($data[$key], $item->get());
  237. }
  238. }
  239. public function testCommit(): void
  240. {
  241. $this->pool->clear();
  242. // If there are no deferred items, this will return true
  243. $this->assertTrue($this->pool->commit());
  244. $data = [
  245. 'foo' => 'bar',
  246. 'bar' => 'baz',
  247. 'foobar' => 'foobarbaz'
  248. ];
  249. $this->setupDeferredData($data);
  250. // See that the data is returned by the pool
  251. foreach($this->pool->getItems(array_keys($data)) as $key => $item)
  252. {
  253. $this->assertTrue($this->pool->hasItem($key));
  254. $this->assertEquals($data[$key], $item->get());
  255. }
  256. $this->pool->commit();
  257. // See that the data is saved in the cache backend
  258. foreach($this->pool->getItems(array_keys($data)) as $key => $item)
  259. {
  260. $this->assertTrue($this->pool->hasItem($key));
  261. $this->assertEquals($data[$key], $item->get());
  262. }
  263. }
  264. protected function setupDeferredData($data): void
  265. {
  266. foreach($data as $key => $val)
  267. {
  268. $item = $this->pool->getItem($key)
  269. ->set($val);
  270. $this->assertTrue($this->pool->saveDeferred($item));
  271. }
  272. }
  273. protected function setupDataInCache($data): void
  274. {
  275. foreach($data as $key => $val)
  276. {
  277. $item = $this->pool->getItem($key)
  278. ->set($val);
  279. $this->pool->save($item);
  280. }
  281. }
  282. }