2016-10-19 09:57:06 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Banker
|
|
|
|
*
|
2020-05-07 19:00:38 -04:00
|
|
|
* 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-05 16:43:37 -04:00
|
|
|
namespace Aviat\Banker\Tests\Driver;
|
|
|
|
|
2020-05-07 17:17:03 -04:00
|
|
|
use Aviat\Banker\Driver\DriverInterface;
|
2021-02-05 13:26:01 -05:00
|
|
|
use Aviat\Banker\Exception\InvalidArgumentException;
|
2016-09-05 16:43:37 -04:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-02-18 19:23:20 -05:00
|
|
|
use TypeError;
|
2016-09-05 16:43:37 -04:00
|
|
|
|
|
|
|
class DriverTestBase extends TestCase {
|
|
|
|
|
2020-05-07 17:17:03 -04:00
|
|
|
protected DriverInterface $driver;
|
|
|
|
|
2018-11-15 16:37:50 -05:00
|
|
|
public function testGetSet(): void
|
2016-09-05 16:43:37 -04:00
|
|
|
{
|
|
|
|
$this->driver->set('foo', 'bar');
|
2018-11-15 16:37:50 -05:00
|
|
|
$this->assertTrue($this->driver->exists('foo'));
|
2016-09-05 16:43:37 -04:00
|
|
|
$this->assertEquals('bar', $this->driver->get('foo'));
|
|
|
|
|
|
|
|
$bar = [
|
|
|
|
'foo' => [
|
|
|
|
'apple' => 'orange'
|
|
|
|
],
|
|
|
|
'bar' => 'baz'
|
|
|
|
];
|
|
|
|
|
2020-05-08 18:58:25 -04:00
|
|
|
$this->assertTrue($this->driver->set('bar', $bar));
|
2016-09-05 16:43:37 -04:00
|
|
|
$this->assertEquals($bar, $this->driver->get('bar'));
|
|
|
|
}
|
2016-09-06 20:26:28 -04:00
|
|
|
|
2018-11-15 16:37:50 -05:00
|
|
|
public function testGetMultipleOnBadKey(): void
|
|
|
|
{
|
|
|
|
$actual = $this->driver->getMultiple(['x','y']);
|
|
|
|
$this->assertEquals([], $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMultiple(): void
|
2016-09-06 20:26:28 -04:00
|
|
|
{
|
|
|
|
$this->driver->set('foo', ['bar']);
|
|
|
|
$this->driver->set('bar', (object) [
|
|
|
|
'foo' => [
|
|
|
|
'bar' => 'baz'
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Intentionally set the same key with different values
|
|
|
|
$this->driver->set('baz', 34);
|
|
|
|
$this->driver->set('baz', 42);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'foo' => ['bar'],
|
|
|
|
'bar' => (object) [
|
|
|
|
'foo' => [
|
|
|
|
'bar' => 'baz'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$actual = $this->driver->getMultiple(['foo', 'bar']);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
2020-05-08 18:58:25 -04:00
|
|
|
public function testSetMultiple(): void
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'foo' => [
|
|
|
|
'apple' => 'orange'
|
|
|
|
],
|
|
|
|
'bar' => 'baz',
|
|
|
|
'baz' => 123456,
|
|
|
|
'a' => [1, 2, 3],
|
|
|
|
'b' => false,
|
|
|
|
'c' => true,
|
|
|
|
'd' => null,
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertTrue($this->driver->setMultiple($data));
|
|
|
|
$this->assertEquals($data, $this->driver->getMultiple(array_keys($data)));
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:26:01 -05:00
|
|
|
public function testSetMultipleInvalidKey(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$data = [
|
|
|
|
128 => 0,
|
|
|
|
0x123 => 1,
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->driver->setMultiple($data);
|
|
|
|
}
|
|
|
|
|
2020-05-08 18:58:25 -04:00
|
|
|
public function testSetMultipleExpires(): void
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'foo' => [
|
|
|
|
'apple' => 'orange'
|
|
|
|
],
|
|
|
|
'bar' => 'baz',
|
|
|
|
'baz' => 123456,
|
|
|
|
'a' => [1, 2, 3],
|
|
|
|
'b' => false,
|
|
|
|
'c' => true,
|
|
|
|
'd' => null,
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertTrue($this->driver->setMultiple($data, 30));
|
|
|
|
$this->assertEquals($data, $this->driver->getMultiple(array_keys($data)));
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:37:50 -05:00
|
|
|
public function testSetWithExpires(): void
|
2016-09-06 20:26:28 -04:00
|
|
|
{
|
|
|
|
$this->driver->set('foo', 'bar', 30);
|
|
|
|
$this->assertEquals('bar', $this->driver->get('foo'));
|
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2021-02-05 13:26:01 -05:00
|
|
|
public function testSetInvalidKey(): void
|
|
|
|
{
|
|
|
|
$this->expectException(\TypeError::class);
|
|
|
|
$this->driver->set(0x12, 'foo');
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:37:50 -05:00
|
|
|
public function testDelete(): void
|
|
|
|
{
|
|
|
|
$this->driver->set('a1', 'b2');
|
|
|
|
$this->assertTrue($this->driver->exists('a1'));
|
|
|
|
|
|
|
|
$this->assertTrue($this->driver->delete('a1'));
|
|
|
|
|
|
|
|
$this->assertFalse($this->driver->exists('a1'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteMultiple(): void
|
|
|
|
{
|
|
|
|
$this->driver->set('a', 1);
|
|
|
|
$this->driver->set('b', 2);
|
|
|
|
|
|
|
|
$this->assertTrue($this->driver->exists('a'));
|
|
|
|
$this->assertTrue($this->driver->exists('b'));
|
|
|
|
|
2020-05-08 18:58:25 -04:00
|
|
|
$this->assertTrue($this->driver->deleteMultiple(['a', 'b']));
|
2018-11-15 16:37:50 -05:00
|
|
|
|
|
|
|
$this->assertFalse($this->driver->exists('a'));
|
|
|
|
$this->assertFalse($this->driver->exists('b'));
|
|
|
|
}
|
|
|
|
|
2020-05-08 18:58:25 -04:00
|
|
|
public function testDeleteMultipleBadKey(): void
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->driver->exists('foo'));
|
|
|
|
$this->assertFalse($this->driver->exists('bar'));
|
|
|
|
|
|
|
|
$this->assertFalse($this->driver->deleteMultiple(['foo', 'bar']));
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:37:50 -05:00
|
|
|
public function testExpiresAt(): void
|
|
|
|
{
|
|
|
|
$this->driver->set('abc', 'def');
|
|
|
|
$result = $this->driver->expiresAt('abc', 30);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExpiresAtBadKey(): void
|
|
|
|
{
|
|
|
|
$result = $this->driver->expiresAt('q', 30);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2016-09-05 16:43:37 -04:00
|
|
|
}
|