Skip to content

Commit

Permalink
testing, logging, and other stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes committed Sep 18, 2024
1 parent 3344c2b commit 70c200a
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/SonsOfPHP/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function deleteItems(array $keys): bool
$isOk = true;
foreach ($keys as $key) {
if (!$this->deleteItem($key)) {
$this->logger?->debug(sprintf('Unable to delete key "%s".', $key));
$isOk = false;
}
}
Expand All @@ -77,6 +78,7 @@ public function commit(): bool

foreach ($this->deferred as $item) {
if (!$this->save($item)) {
$this->logger?->debug(sprintf('Unable to save key "%s".', $item->getKey()));
$isOk = false;
}
}
Expand Down
44 changes: 22 additions & 22 deletions src/SonsOfPHP/Component/Cache/Adapter/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,18 @@ public function hasItem(string $key): bool
*/
public function clear(): bool
{
if (is_dir($this->directory)) {
// Prune all files and directories
$it = new \RecursiveDirectoryIterator($this->directory, \RecursiveDirectoryIterator::SKIP_DOTS);
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($files as $file) {
if ($file->isDir()) {
rmdir($file->getPathname());
} else {
unlink($file->getPathname());
}
$it = new \RecursiveDirectoryIterator($this->directory, \RecursiveDirectoryIterator::SKIP_DOTS);
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);

foreach ($files as $file) {
if ($file->isDir()) {
rmdir($file->getPathname());
} else {
unlink($file->getPathname());
}

return rmdir($this->directory);
}

return true;
return rmdir($this->directory);
}

/**
Expand All @@ -95,27 +91,30 @@ public function deleteItem(string $key): bool
{
CacheItem::validateKey($key);

if (file_exists($this->getFile($key))) {
return unlink($this->getFile($key));
$filename = $this->getFile($key);

if (!file_exists($filename)) {
$this->logger?->debug(sprintf('Cache file "%s" does not exist', $filename));
return false;
}

return true;
return unlink($filename);
}

/**
* {@inheritdoc}
*/
public function save(CacheItemInterface $item): bool
{
if (!is_dir($this->directory)) {
@mkdir($this->directory, $this->defaultPermission, true);
}
$filename = $this->getFile($item->getKey());

if (false === file_put_contents($this->getFile($item->getKey()), $this->marshaller->marshall($item->get()))) {
if (false === file_put_contents($filename, $this->marshaller->marshall($item->get()))) {
$this->logger?->debug(sprintf('Could not write "%s"', $filename));
return false;
}

if (false === chmod($this->getFile($item->getKey()), $this->defaultPermission)) {
if (false === chmod($filename, $this->defaultPermission)) {
$this->logger?->debug(sprintf('Could not chmod "%s"', $filename));
return false;
}

Expand All @@ -124,7 +123,8 @@ public function save(CacheItemInterface $item): bool
}

$mtime = (int) round($item->expiry());
return touch($this->getFile($item->getKey()), $mtime);

return touch($filename, $mtime);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\Cache\Tests\Adapter;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use SonsOfPHP\Component\Cache\Adapter\AbstractAdapter;
use SonsOfPHP\Component\Cache\Adapter\AdapterInterface;
use SonsOfPHP\Component\Cache\CacheItem;

#[CoversClass(AbstractAdapter::class)]
#[UsesClass(CacheItem::class)]
//#[UsesClass(SerializableMarshaller::class)]
final class AbstractAdapterTest extends TestCase
{
private AbstractAdapter $adapter;

protected function setUp(): void
{
//$this->adapter = $this->createStub(AbstractAdapter::class);
$this->adapter = new class extends AbstractAdapter {
public function getItem(string $key): CacheItemInterface
{
return new CacheItem($key, false);
}

public function hasItem(string $key): bool
{
return $this->getItem($key)->isHit();
}

public function clear(): bool
{
return true;
}

public function deleteItem(string $key): bool
{
return true;
}

public function save(CacheItemInterface $item): bool
{
return true;
}
};
}

public function testItHasTheCorrectInterface(): void
{
$this->assertInstanceOf(AdapterInterface::class, $this->adapter);
$this->assertInstanceOf(CacheItemPoolInterface::class, $this->adapter);
}

public function testItWillExecuteGetItemWhenGetItemsIsExecuted(): void
{
$items = iterator_to_array($this->adapter->getItems(['test']));
$this->assertArrayHasKey('test', $items);
}

public function testItWillDeleteItems(): void
{
$this->assertTrue($this->adapter->deleteItems(['test']));
}

public function testItWillSaveDeferred(): void
{
$cacheItem = $this->adapter->getItem('test');

$this->assertTrue($this->adapter->saveDeferred($cacheItem));
}

public function testItWillCommit(): void
{
$cacheItem = $this->adapter->getItem('test');
$this->adapter->saveDeferred($cacheItem);

$this->assertTrue($this->adapter->commit());
}

public function testItWillDestruct(): void
{
$cacheItem = $this->adapter->getItem('test');
$this->adapter->saveDeferred($cacheItem);

$this->assertTrue($this->adapter->commit());
unset($this->adapter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ public function testItCanDeleteCacheItem(): void
$this->assertTrue($this->adapter->deleteItem('test'));
$this->assertFalse($this->adapter->hasItem('test'));
}

public function testItWillReturnFalseWhenDeletingKeyThatDoesNotExist(): void
{
$this->assertFalse($this->adapter->deleteItem('test'));
}
}

0 comments on commit 70c200a

Please sign in to comment.