banker/tests/PoolTest.php

364 lines
8.7 KiB
PHP
Raw Normal View History

2016-10-19 09:57:06 -04:00
<?php declare(strict_types=1);
/**
* Banker
*
* A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
2016-10-19 09:57:06 -04:00
*
2021-11-30 11:56:15 -05:00
* PHP version 8+
2016-10-19 09:57:06 -04:00
*
* @package Banker
* @author Timothy J. Warren <tim@timshomepage.net>
2021-02-05 17:05:56 -05:00
* @copyright 2016 - 2021 Timothy J. Warren
2016-10-19 09:57:06 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2021-11-30 11:56:15 -05:00
* @version 5.0.0
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};
use TypeError;
2016-09-06 17:03:43 -04:00
class PoolTest extends TestCase {
2016-09-06 17:03:43 -04:00
protected $pool;
2019-12-10 11:00:46 -05:00
public function setUp(): void
2016-09-06 17:03:43 -04:00
{
$this->pool = new Pool([
'driver' => 'null',
'connection' => []
]);
}
public function testGetDefaultLogger(): void
2016-09-06 17:03:43 -04:00
{
$friend = new Friend($this->pool);
$driverFriend = new Friend($friend->driver);
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");
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));
}
public function testSetLoggerInConstructor(): void
2016-09-06 17:03:43 -04:00
{
$logger = new Logger('test');
2019-12-10 11:00:46 -05:00
$logger->pushHandler(new SyslogHandler('warning', LOG_USER, Logger::WARNING));
2016-09-06 17:03:43 -04:00
$pool = new Pool([
'driver' => 'null',
'connection' => [],
], $logger);
2016-09-06 17:03:43 -04:00
$friend = new Friend($pool);
$driverFriend = new Friend($friend->driver);
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");
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));
}
public function testGetSetLogger(): void
2016-09-06 17:03:43 -04:00
{
$logger = new Logger('test');
2019-12-10 11:00:46 -05:00
$logger->pushHandler(new SyslogHandler('warning2',LOG_USER, Logger::WARNING));
2016-09-06 17:03:43 -04:00
$this->pool->setLogger($logger);
2016-09-06 17:03:43 -04:00
$friend = new Friend($this->pool);
$driverFriend = new Friend($friend->driver);
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");
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));
}
public function testGetItem(): void
2016-09-06 17:03:43 -04:00
{
$item = $this->pool->getItem('foo');
$this->assertInstanceOf(Item::class, $item);
}
public function testItemBadKey(): void
2016-09-06 17:03:43 -04:00
{
$this->expectException(TypeError::class);
2016-09-06 17:03:43 -04:00
$this->pool->getItem([]);
}
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);
2016-09-06 17:03:43 -04:00
foreach($collection as $item)
{
$this->assertInstanceOf(Item::class, $item);
}
}
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
{
$this->pool->clear();
2016-09-06 17:03:43 -04:00
$collection = $this->pool->getItems();
2016-09-06 17:03:43 -04:00
$this->assertInstanceOf(ItemCollection::class, $collection);
$this->assertCount(0, $collection);
2016-09-06 17:03:43 -04:00
}
public function testHasItem(): void
2016-09-06 17:03:43 -04:00
{
$this->pool->clear();
2016-09-06 17:03:43 -04:00
// The key doesn't exist yet
$this->assertFalse($this->pool->hasItem('foobar'));
2016-09-06 17:03:43 -04:00
// Create the item
$item = $this->pool->getItem('foobar')
->set('baz')
->save();
2016-09-06 17:03:43 -04:00
// The item exists now
$this->assertTrue($this->pool->hasItem('foobar'));
}
public function testHasItemBadKey(): void
2016-09-06 17:03:43 -04:00
{
$this->pool->clear();
$this->expectException(TypeError::class);
2016-09-06 17:03:43 -04:00
$this->pool->hasItem(34);
}
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();
2016-09-06 17:03:43 -04:00
$data = [
'foo' => 'bar',
'bar' => 'baz',
'foobar' => 'foobarbaz'
];
2016-09-06 17:03:43 -04:00
// Set up some data
$this->setupDataInCache($data);
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());
}
2016-09-06 17:03:43 -04:00
// Now we clear it all!
$this->pool->clear();
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());
}
}
public function testDeleteItemBadKey(): void
2016-09-06 17:03:43 -04:00
{
$this->expectException(TypeError::class);
// $this->expectExceptionMessage('Cache key must be a string.');
2016-09-06 17:03:43 -04:00
$this->pool->deleteItem(34);
}
2020-05-07 17:17:03 -04:00
public function testDeleteItemsBadKey(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Cache key must be a string.');
$this->pool->deleteItems([34]);
}
public function testDeleteItemThatDoesNotExist(): void
2016-09-06 17:03:43 -04:00
{
$this->pool->clear();
$this->assertFalse($this->pool->deleteItem('foo'));
}
public function testDeleteItem(): void
2016-09-06 17:03:43 -04:00
{
// Start with a clean slate
$this->pool->clear();
2016-09-06 17:03:43 -04:00
$data = [
'foo' => 'bar',
'bar' => 'baz',
'foobar' => 'foobarbaz'
];
2016-09-06 17:03:43 -04:00
$this->setupDataInCache($data);
2016-09-06 17:03:43 -04:00
$this->pool->deleteItem('foo');
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());
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()));
}
}
public function testDeleteItems(): void
2016-09-06 17:03:43 -04:00
{
$this->pool->clear();
2016-09-06 17:03:43 -04:00
$data = [
'foo' => 'bar',
'bar' => 'baz',
'foobar' => 'foobarbaz'
];
2016-09-06 17:03:43 -04:00
$this->setupDataInCache($data);
2016-09-06 17:03:43 -04:00
$this->pool->deleteItems(['foo', 'bar']);
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());
}
}
public function testSaveDeferred(): void
2016-09-06 17:03:43 -04:00
{
$this->pool->clear();
2016-09-06 17:03:43 -04:00
$data = [
'foo' => 'bar',
'bar' => 'baz',
'foobar' => 'foobarbaz'
];
2016-09-06 17:03:43 -04:00
$this->setupDeferredData($data);
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);
// 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
}
}
public function testCommit(): void
2016-09-06 17:03:43 -04:00
{
$this->pool->clear();
2016-09-06 17:03:43 -04:00
// If there are no deferred items, this will return true
$this->assertTrue($this->pool->commit());
2016-09-06 17:03:43 -04:00
$data = [
'foo' => 'bar',
'bar' => 'baz',
'foobar' => 'foobarbaz'
];
2016-09-06 17:03:43 -04:00
$this->setupDeferredData($data);
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));
$this->assertEquals($data[$key], $item->get());
2016-09-06 17:03:43 -04:00
}
2016-09-06 17:03:43 -04:00
$this->pool->commit();
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());
}
}
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);
2016-09-06 17:03:43 -04:00
$this->assertTrue($this->pool->saveDeferred($item));
}
}
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);
2016-09-06 17:03:43 -04:00
$this->pool->save($item);
}
}
}