Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
- PSR-16 Simple Cache Interface implementation
Browse files Browse the repository at this point in the history
- Codiing standards
- Test coverage
  • Loading branch information
Sam-Burns committed Jan 5, 2017
1 parent 90e7201 commit c1af969
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"php": "^5.5 || ^7.0",
"zendframework/zend-stdlib": "^2.7 || ^3.0",
"zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3",
"zendframework/zend-eventmanager": "^2.6.2 || ^3.0"
"zendframework/zend-eventmanager": "^2.6.2 || ^3.0",
"psr/simple-cache": "^1.0.0"
},
"require-dev": {
"zendframework/zend-serializer": "^2.6",
Expand Down
52 changes: 50 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

namespace Zend\Cache\Exception;

use Psr\SimpleCache\InvalidArgumentException as Psr16InvalidArgumentException;

class InvalidArgumentException extends \InvalidArgumentException implements
ExceptionInterface
ExceptionInterface,
Psr16InvalidArgumentException
{
}
74 changes: 73 additions & 1 deletion src/Storage/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@
namespace Zend\Cache\Storage\Adapter;

use ArrayObject;
use Psr\SimpleCache\CacheInterface as Psr16CacheInterface;
use SplObjectStorage;
use stdClass;
use Traversable;
use Zend\Cache\Exception;
use Zend\Cache\Storage\Capabilities;
use Zend\Cache\Storage\Event;
use Zend\Cache\Storage\ExceptionEvent;
use Zend\Cache\Storage\FlushableInterface;
use Zend\Cache\Storage\Plugin;
use Zend\Cache\Storage\PostEvent;
use Zend\Cache\Storage\StorageInterface;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\EventsCapableInterface;

abstract class AbstractAdapter implements StorageInterface, EventsCapableInterface
abstract class AbstractAdapter implements StorageInterface, EventsCapableInterface, Psr16CacheInterface
{
/**
* The used EventManager if any
Expand Down Expand Up @@ -1577,4 +1579,74 @@ protected function normalizeKeyValuePairs(array & $keyValuePairs)
}
$keyValuePairs = $normalizedKeyValuePairs;
}

// PSR-16 Simple Cache Interface implementation

/**
* @inheritdoc
*/
public function get($key, $default = null)
{
$result = $this->getItem($key);
return $result === null ? $default : $result;
}

/**
* @inheritdoc
*/
public function set($key, $value, $ttl = null)
{
return $this->setItem($key, $value);
}

/**
* @inheritdoc
*/
public function delete($key)
{
return $this->removeItem($key);
}

/**
* @inheritdoc
*/
public function clear()
{
if ($this instanceof FlushableInterface) {
return $this->flush();
}
return false;
}

/**
* @inheritdoc
*/
public function getMultiple($keys, $default = null)
{
return $this->getItems($keys);
}

/**
* @inheritdoc
*/
public function setMultiple($values, $ttl = null)
{
return ! $this->setItems($values);
}

/**
* @inheritdoc
*/
public function deleteMultiple($keys)
{
return ! $this->removeItems($keys);
}

/**
* @inheritdoc
*/
public function has($key)
{
return $this->hasItem($key);
}
}
22 changes: 21 additions & 1 deletion test/Storage/Adapter/CommonAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace ZendTest\Cache\Storage\Adapter;

use Psr\SimpleCache\CacheInterface as Psr16CacheInterface;
use Zend\Cache\Storage\AdapterPluginManager;
use Zend\Cache\Storage\AvailableSpaceCapableInterface;
use Zend\Cache\Storage\IterableInterface;
Expand Down Expand Up @@ -42,7 +43,7 @@ abstract class CommonAdapterTest extends \PHPUnit_Framework_TestCase
/**
* The storage adapter
*
* @var StorageInterface
* @var StorageInterface|Psr16CacheInterface
*/
protected $_storage;

Expand Down Expand Up @@ -1252,4 +1253,23 @@ protected function waitForFullSecond()
$interval = (microtime(true) - time()) * 1000000;
usleep((int) $interval);
}

public function testItIsAPsr16Adapter()
{
$this->assertInstanceOf(Psr16CacheInterface::class, $this->_storage);
}

public function testHasImplementation()
{
$this->_storage->setItem('keyThatExists', 'value');
$this->assertTrue($this->_storage->has('keyThatExists'));
$this->assertFalse($this->_storage->has('keyThatDoesntExist'));
}

public function testGetImplementation()
{
$this->_storage->setItem('key', 'value');
$this->assertEquals('value', $this->_storage->get('key'));
$this->assertEquals('defaultValue', $this->_storage->get('keyThatDoesntExist', 'defaultValue'));
}
}

0 comments on commit c1af969

Please sign in to comment.