Files
cache/test/Unit/Adapter/FilesystemAdapterTest.php

141 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace IQParts\CacheTest\Unit\Adapter;
use IQParts\Cache\Adapter\FilesystemAdapter;
use IQParts\Cache\Serializer\JsonSerializer;
use IQParts\CacheTest\AbstractTestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
final class FilesystemAdapterTest extends AbstractTestCase
{
public function testGetSet()
{
$location = $this->getTmpDirectory();
$adapter = new FilesystemAdapter(
$location,
new JsonSerializer(),
0777
);
$adapter->set('a', 'b');
$this->assertEquals('b', $adapter->get('a'));
$adapter->delete('a');
$this->assertEquals(null, $adapter->get('a'));
$adapter->set('ttl', 'b', 200);
$this->assertTrue($adapter->ttl('ttl') > 0);
$this->assertSame($adapter->ttl('b'), $adapter::NO_TTL);
$adapter->set('a-keys', 'a');
$adapter->set('b-keys', 'a');
$this->assertEquals(['a-keys'], $adapter->keys('a*'));
$adapter->delete('b');
$adapter->delete('a-keys');
$adapter->delete('b-keys');
}
public function testTtl()
{
$location = $this->getTmpDirectory();
$adapter = new FilesystemAdapter(
$location,
new JsonSerializer(),
0777
);
$adapter->set('a', 'b', 10);
$this->assertEquals('b', $adapter->get('a'));
$this->assertTrue($adapter->ttl('a') > 0);
$adapter->set('b', 'a', -10);
$this->assertNull($adapter->get('b'));
$adapter->delete('b');
$adapter->delete('a');
}
public function testDefaultTtl()
{
$location = $this->getTmpDirectory();
$adapter = new FilesystemAdapter(
$location,
new JsonSerializer(),
0777,
10
);
$adapter->set('a', 'b');
$this->assertTrue($adapter->ttl('a') > 0);
$adapter->delete('a');
}
public function testDelete()
{
$location = $this->getTmpDirectory();
$adapter = new FilesystemAdapter(
$location,
new JsonSerializer(),
0777
);
$adapter->set('del-a', 'a');
$adapter->set('del-b', 'b');
$adapter->delete('del*');
$this->assertNull($adapter->get('del-a'));
$this->assertNull($adapter->get('del-b'));
}
public function testSubFolder()
{
$location = $this->getTmpDirectory();
$adapter = new FilesystemAdapter(
$location,
new JsonSerializer(),
0777
);
$adapter->set('a/b', 'a');
$this->assertEquals('a', $adapter->get('a/b'));
$adapter->delete('a/b');
}
public function testSubFolderWithSeperator()
{
$location = $this->getTmpDirectory();
$adapter = new FilesystemAdapter(
$location,
new JsonSerializer(),
0777,
null,
'/'
);
$adapter->set('a/b', 'a');
$this->assertEquals('a', $adapter->get('a/b'));
$adapter->delete('a/b');
}
protected function tearDown()
{
parent::tearDown();
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->getTmpDirectory(), RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $file) {
if ($file->isDir()) {
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
}
}