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-06 17:03:43 -04:00
|
|
|
namespace Aviat\Banker\Tests;
|
|
|
|
|
|
|
|
use Aviat\Banker\Item;
|
|
|
|
use Aviat\Banker\Driver\NullDriver;
|
2016-09-06 20:26:28 -04:00
|
|
|
use Aviat\Banker\Tests\Friend;
|
2016-09-06 17:03:43 -04:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
2016-09-06 20:26:28 -04:00
|
|
|
use DateTime;
|
|
|
|
use DateInterval;
|
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
class ItemTest extends TestCase {
|
2016-09-06 20:26:28 -04:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
protected $key = 'foo';
|
|
|
|
protected $item;
|
|
|
|
protected $driver;
|
2016-09-06 20:26:28 -04:00
|
|
|
|
2019-12-10 11:00:46 -05:00
|
|
|
public function setUp(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
$this->driver = new NullDriver();
|
|
|
|
$this->item = new Item($this->driver, $this->key);
|
|
|
|
}
|
2016-09-06 20:26:28 -04:00
|
|
|
|
2019-12-10 11:00:46 -05:00
|
|
|
public function testGetKey(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
$this->assertEquals($this->key, $this->item->getKey());
|
|
|
|
}
|
2016-09-06 20:26:28 -04:00
|
|
|
|
2019-12-10 11:00:46 -05:00
|
|
|
public function testGet(): void
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
// No value set yet
|
|
|
|
$this->assertNull($this->item->get());
|
2016-09-06 20:26:28 -04:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
// Set a value
|
|
|
|
$this->item->set('bar')
|
|
|
|
->save();
|
2016-09-06 20:26:28 -04:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
$this->assertEquals('bar', $this->item->get());
|
|
|
|
}
|
2016-09-06 20:26:28 -04:00
|
|
|
|
2019-12-10 11:00:46 -05:00
|
|
|
public function testExpiresAt(): void
|
2016-09-06 20:26:28 -04:00
|
|
|
{
|
|
|
|
$time = new DateTime("Next Tuesday");
|
|
|
|
$expected = $time->getTimestamp();
|
|
|
|
$this->item->expiresAt($time);
|
|
|
|
$friend = new Friend($this->item);
|
|
|
|
$this->assertEquals($expected, $friend->expiresAt, "DateTimeInterface");
|
|
|
|
|
|
|
|
$time2 = strtotime("July 16, 2024");
|
|
|
|
$this->item->expiresAt($time2);
|
|
|
|
$friend2 = new Friend($this->item);
|
|
|
|
$this->assertEquals($time2, $friend2->expiresAt, "Unix Timestamp");
|
|
|
|
}
|
|
|
|
|
2019-12-10 11:00:46 -05:00
|
|
|
public function testExpiresAfter(): void
|
2016-09-06 20:26:28 -04:00
|
|
|
{
|
|
|
|
$interval = new DateInterval('P5W');
|
|
|
|
$expected = (int) $interval->format("%s");
|
|
|
|
$this->item->expiresAfter($interval);
|
|
|
|
$friend = new Friend($this->item);
|
|
|
|
$this->assertEquals($expected, $friend->ttl);
|
|
|
|
|
|
|
|
$interval2 = 500;
|
|
|
|
$this->item->expiresAfter($interval2);
|
2018-11-15 16:37:50 -05:00
|
|
|
$this->item->save();
|
2016-09-06 20:26:28 -04:00
|
|
|
$friend2 = new Friend($this->item);
|
|
|
|
$this->assertEquals($interval2, $friend2->ttl);
|
|
|
|
}
|
|
|
|
|
2019-12-10 11:00:46 -05:00
|
|
|
public function testSaveWithExpiresAt(): void
|
2016-09-06 20:26:28 -04:00
|
|
|
{
|
|
|
|
$this->setUp();
|
|
|
|
|
|
|
|
$expires = new DateTime("6 weeks");
|
|
|
|
$this->item->expiresAt($expires);
|
|
|
|
|
|
|
|
$this->item->set('barbazfoo');
|
|
|
|
|
|
|
|
$result = $this->item->save();
|
|
|
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertTrue($this->item->isHit());
|
|
|
|
$this->assertEquals('barbazfoo', $this->item->get());
|
|
|
|
}
|
|
|
|
|
2019-12-10 11:00:46 -05:00
|
|
|
public function testSaveWithExpiresAfter(): void
|
2016-09-06 20:26:28 -04:00
|
|
|
{
|
|
|
|
$this->setUp();
|
|
|
|
|
|
|
|
$interval = new DateInterval('P2D');
|
|
|
|
$expected = $interval->format("%s");
|
|
|
|
$this->item->expiresAfter($interval);
|
|
|
|
$friend = new Friend($this->item);
|
|
|
|
$this->assertEquals($expected, $friend->ttl);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'foo' => [
|
|
|
|
'bar' => []
|
|
|
|
],
|
|
|
|
'baz' => (object) []
|
|
|
|
];
|
|
|
|
$this->item->set($expected);
|
|
|
|
|
|
|
|
$result = $this->item->save();
|
|
|
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertTrue($this->item->isHit());
|
|
|
|
$this->assertEquals($expected, $this->item->get());
|
|
|
|
}
|
2016-09-06 17:03:43 -04:00
|
|
|
}
|