2016-10-19 09:57:06 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Banker
|
|
|
|
*
|
|
|
|
* A Caching library implementing psr/cache
|
|
|
|
*
|
|
|
|
* PHP version 7.0
|
|
|
|
*
|
|
|
|
* @package Banker
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2017-03-01 12:33:12 -05:00
|
|
|
* @copyright 2016 - 2017 Timothy J. Warren
|
2016-10-19 09:57:06 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2017-03-01 12:33:12 -05:00
|
|
|
* @version 1.0.1
|
2016-10-19 09:57:06 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/banker
|
|
|
|
*/
|
2016-09-06 17:03:43 -04:00
|
|
|
namespace Aviat\Banker\Tests;
|
|
|
|
|
2017-03-01 12:04:01 -05:00
|
|
|
use Aviat\Banker\{Item, ItemCollection, Pool};
|
2016-09-06 17:03:43 -04:00
|
|
|
use Aviat\Banker\Exception\InvalidArgumentException;
|
|
|
|
use Monolog\Logger;
|
|
|
|
use Monolog\Handler\SyslogHandler;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-03-01 12:04:01 -05:00
|
|
|
use Psr\Log\{LoggerInterface, NullLogger};
|
2016-09-06 17:03:43 -04:00
|
|
|
|
|
|
|
class PoolTest extends TestCase {
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
protected $pool;
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->pool = new Pool([
|
|
|
|
'driver' => 'null',
|
|
|
|
'connection' => []
|
|
|
|
]);
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testGetDefaultLogger(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
$friend = new Friend($this->pool);
|
|
|
|
$driverFriend = new Friend($friend->driver);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// Check that a valid logger is set
|
|
|
|
$this->assertInstanceOf(LoggerInterface::class, $friend->getLogger(), "Logger exists after being set");
|
|
|
|
$this->assertInstanceOf(LoggerInterface::class, $driverFriend->getLogger(), "Logger exists on driver after being set");
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// Make sure we get the default Null logger
|
|
|
|
$this->assertTrue(is_a($friend->getLogger(), NullLogger::class));
|
|
|
|
$this->assertTrue(is_a($driverFriend->getLogger(), NullLogger::class));
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testSetLoggerInConstructor(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
$logger = new Logger('test');
|
|
|
|
$logger->pushHandler(new SyslogHandler(Logger::WARNING));
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$pool = new Pool([
|
|
|
|
'driver' => 'null',
|
|
|
|
'connection' => [],
|
|
|
|
], $logger);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$friend = new Friend($pool);
|
|
|
|
$driverFriend = new Friend($friend->driver);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// Check that a valid logger is set
|
|
|
|
$this->assertInstanceOf(LoggerInterface::class, $friend->getLogger(), "Logger exists after being set");
|
|
|
|
$this->assertInstanceOf(LoggerInterface::class, $driverFriend->getLogger(), "Logger exists on driver after being set");
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// Make sure we aren't just getting the default Null logger
|
|
|
|
$this->assertFalse(is_a($friend->getLogger(), NullLogger::class));
|
|
|
|
$this->assertFalse(is_a($driverFriend->getLogger(), NullLogger::class));
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testGetSetLogger(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
$logger = new Logger('test');
|
|
|
|
$logger->pushHandler(new SyslogHandler(Logger::WARNING));
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->pool->setLogger($logger);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$friend = new Friend($this->pool);
|
|
|
|
$driverFriend = new Friend($friend->driver);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// Check that a valid logger is set
|
|
|
|
$this->assertInstanceOf(LoggerInterface::class, $friend->getLogger(), "Logger exists after being set");
|
|
|
|
$this->assertInstanceOf(LoggerInterface::class, $driverFriend->getLogger(), "Logger exists on driver after being set");
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// Make sure we aren't just getting the default Null logger
|
|
|
|
$this->assertFalse(is_a($friend->getLogger(), NullLogger::class));
|
|
|
|
$this->assertFalse(is_a($driverFriend->getLogger(), NullLogger::class));
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:37:50 -05:00
|
|
|
public function testGetItem(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
$item = $this->pool->getItem('foo');
|
|
|
|
$this->assertInstanceOf(Item::class, $item);
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testItemBadKey(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2018-11-15 16:37:50 -05:00
|
|
|
$this->expectExceptionMessage('Cache key must be a string.');
|
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->pool->getItem([]);
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testGetItemsBadKey(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->pool->getItems([1,3,2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetItems(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
$collection = $this->pool->getItems(['foo', 'bar', 'baz']);
|
|
|
|
$this->assertInstanceOf(ItemCollection::class, $collection);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
foreach($collection as $item)
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf(Item::class, $item);
|
|
|
|
}
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testGetItemsDeferredItems(): void
|
|
|
|
{
|
|
|
|
$this->pool->clear();
|
|
|
|
|
|
|
|
$deferredValues = ['x' => 1, 'y' => 2, 'z' => 3];
|
|
|
|
$keys = ['x', 'y', 'z'];
|
|
|
|
|
|
|
|
foreach ($deferredValues as $key => $value)
|
|
|
|
{
|
|
|
|
$item = $this->pool->getItem($key)->set($value);
|
|
|
|
$this->pool->saveDeferred($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
$collection = $this->pool->getItems($keys);
|
|
|
|
|
|
|
|
foreach($collection as $key => $item)
|
|
|
|
{
|
|
|
|
$this->assertSame($deferredValues[$key], $item->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertCount(3, $collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmptyGetItems(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
2018-11-15 16:37:50 -05:00
|
|
|
$this->pool->clear();
|
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$collection = $this->pool->getItems();
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->assertInstanceOf(ItemCollection::class, $collection);
|
2018-11-15 16:37:50 -05:00
|
|
|
$this->assertCount(0, $collection);
|
2016-09-06 17:03:43 -04:00
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testHasItem(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
2018-11-15 16:37:50 -05:00
|
|
|
$this->pool->clear();
|
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// The key doesn't exist yet
|
|
|
|
$this->assertFalse($this->pool->hasItem('foobar'));
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// Create the item
|
|
|
|
$item = $this->pool->getItem('foobar')
|
|
|
|
->set('baz')
|
|
|
|
->save();
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// The item exists now
|
|
|
|
$this->assertTrue($this->pool->hasItem('foobar'));
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testHasItemBadKey(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
2018-11-15 16:37:50 -05:00
|
|
|
$this->pool->clear();
|
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2018-11-15 16:37:50 -05:00
|
|
|
$this->expectExceptionMessage('Cache key must be a string.');
|
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->pool->hasItem(34);
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testClear(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
// Call clear to make sure we are working from a clean slate to start
|
|
|
|
$this->pool->clear();
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$data = [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'bar' => 'baz',
|
|
|
|
'foobar' => 'foobarbaz'
|
|
|
|
];
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// Set up some data
|
|
|
|
$this->setupDataInCache($data);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
foreach($data as $key => $val)
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->pool->hasItem($key));
|
|
|
|
$item = $this->pool->getItem($key);
|
|
|
|
$this->assertEquals($val, $item->get());
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// Now we clear it all!
|
|
|
|
$this->pool->clear();
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
foreach($data as $key => $val)
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->pool->hasItem($key));
|
|
|
|
$item = $this->pool->getItem($key);
|
|
|
|
$this->assertNull($item->get());
|
|
|
|
}
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testDeleteItemBadKey(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage("Cache key must be a string.");
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->pool->deleteItem(34);
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testDeleteItemThatDoesNotExist(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
$this->pool->clear();
|
|
|
|
$this->assertFalse($this->pool->deleteItem('foo'));
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testDeleteItem(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
// Start with a clean slate
|
|
|
|
$this->pool->clear();
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$data = [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'bar' => 'baz',
|
|
|
|
'foobar' => 'foobarbaz'
|
|
|
|
];
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->setupDataInCache($data);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->pool->deleteItem('foo');
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// The item no longer exists
|
|
|
|
$this->assertFalse($this->pool->hasItem('foo'));
|
|
|
|
$item = $this->pool->getItem('foo');
|
|
|
|
$this->assertNull($item->get());
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// The other items still exist
|
|
|
|
foreach(['bar', 'foobar'] as $key)
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->pool->hasItem($key));
|
|
|
|
$item = $this->pool->getItem($key);
|
|
|
|
$this->assertFalse(is_null($item->get()));
|
|
|
|
}
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testDeleteItems(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
$this->pool->clear();
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$data = [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'bar' => 'baz',
|
|
|
|
'foobar' => 'foobarbaz'
|
|
|
|
];
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->setupDataInCache($data);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->pool->deleteItems(['foo', 'bar']);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
foreach(['foo', 'bar'] as $key)
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->pool->hasItem($key));
|
|
|
|
$item = $this->pool->getItem($key);
|
|
|
|
$this->assertNull($item->get());
|
|
|
|
}
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testSaveDeferred(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
$this->pool->clear();
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$data = [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'bar' => 'baz',
|
|
|
|
'foobar' => 'foobarbaz'
|
|
|
|
];
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->setupDeferredData($data);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// See that the data is returned by the pool
|
|
|
|
foreach($data as $key => $val)
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->pool->hasItem($key));
|
|
|
|
$item = $this->pool->getItem($key);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
// Since the value has been deferred,
|
|
|
|
// the pool will return the updated value,
|
|
|
|
// even though the cache hasn't been updated yet
|
|
|
|
$this->assertEquals($data[$key], $item->get());
|
2016-09-06 17:03:43 -04:00
|
|
|
}
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
public function testCommit(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
$this->pool->clear();
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// If there are no deferred items, this will return true
|
|
|
|
$this->assertTrue($this->pool->commit());
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$data = [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'bar' => 'baz',
|
|
|
|
'foobar' => 'foobarbaz'
|
|
|
|
];
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->setupDeferredData($data);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// See that the data is returned by the pool
|
|
|
|
foreach($this->pool->getItems(array_keys($data)) as $key => $item)
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->pool->hasItem($key));
|
2018-11-15 16:37:50 -05:00
|
|
|
$this->assertEquals($data[$key], $item->get());
|
2016-09-06 17:03:43 -04:00
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->pool->commit();
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// See that the data is saved in the cache backend
|
|
|
|
foreach($this->pool->getItems(array_keys($data)) as $key => $item)
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->pool->hasItem($key));
|
|
|
|
$this->assertEquals($data[$key], $item->get());
|
|
|
|
}
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
protected function setupDeferredData($data): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
foreach($data as $key => $val)
|
|
|
|
{
|
|
|
|
$item = $this->pool->getItem($key)
|
|
|
|
->set($val);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->assertTrue($this->pool->saveDeferred($item));
|
|
|
|
}
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
protected function setupDataInCache($data): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
foreach($data as $key => $val)
|
|
|
|
{
|
|
|
|
$item = $this->pool->getItem($key)
|
|
|
|
->set($val);
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->pool->save($item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|