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

Commit

Permalink
Adds test to check TTL on set/setMultiple of SimpleCacheDecorator
Browse files Browse the repository at this point in the history
We should use TTL from options when it is not provided on set/setMultiple
methods of SimpleCacheDecorator.
  • Loading branch information
michalbundyra committed Aug 14, 2019
1 parent 035b012 commit 96fa750
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
47 changes: 45 additions & 2 deletions test/Psr/SimpleCache/SimpleCacheDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public function setUp()
/**
* @param bool $staticTtl
* @param int $minTtl
* @return ObjectProphecy
*/
public function mockCapabilities(
ObjectProphecy $storage,
private function getMockCapabilities(
array $supportedDataTypes = null,
$staticTtl = true,
$minTtl = 60
Expand All @@ -69,6 +69,22 @@ public function mockCapabilities(
$capabilities->getSupportedDatatypes()->willReturn($supportedDataTypes);
$capabilities->getStaticTtl()->willReturn($staticTtl);
$capabilities->getMinTtl()->willReturn($minTtl);

return $capabilities;
}

/**
* @param bool $staticTtl
* @param int $minTtl
*/
public function mockCapabilities(
ObjectProphecy $storage,
array $supportedDataTypes = null,
$staticTtl = true,
$minTtl = 60
) {
$capabilities = $this->getMockCapabilities($supportedDataTypes, $staticTtl, $minTtl);

$storage->getCapabilities()->will([$capabilities, 'reveal']);
}

Expand Down Expand Up @@ -739,4 +755,31 @@ public function testHasReRaisesExceptionThrownByStorage()
$this->assertSame($exception, $e->getPrevious());
}
}

public function testUseTtlFromOptionsWhenNotProvidedOnSet()
{
$capabilities = $this->getMockCapabilities();

$storage = new TestAsset\TtlStorage(['ttl' => 20]);
$storage->setCapabilities($capabilities->reveal());
$cache = new SimpleCacheDecorator($storage);

$cache->set('foo', 'bar');
self::assertSame(20, $storage->ttl['foo']);
self::assertSame(20, $storage->getOptions()->getTtl());
}

public function testUseTtlFromOptionsWhenNotProvidedOnSetMultiple()
{
$capabilities = $this->getMockCapabilities();

$storage = new TestAsset\TtlStorage(['ttl' => 20]);
$storage->setCapabilities($capabilities->reveal());
$cache = new SimpleCacheDecorator($storage);

$cache->setMultiple(['foo' => 'bar', 'bar' => 'baz']);
self::assertSame(20, $storage->ttl['foo']);
self::assertSame(20, $storage->ttl['bar']);
self::assertSame(20, $storage->getOptions()->getTtl());
}
}
46 changes: 46 additions & 0 deletions test/Psr/SimpleCache/TestAsset/TtlStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @see https://github.com/zendframework/zend-cache for the canonical source repository
* @copyright Copyright (c) 2019 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\Cache\Psr\SimpleCache\TestAsset;

use Zend\Cache\Storage\Adapter;
use Zend\Cache\Storage\Capabilities;

class TtlStorage extends Adapter\AbstractAdapter
{
/** @var array */
private $data = [];

/** @var array */
public $ttl = [];

protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null)
{
$success = isset($this->data[$normalizedKey]);

return $success ? $this->data[$normalizedKey] : null;
}

protected function internalSetItem(& $normalizedKey, & $value)
{
$this->ttl[$normalizedKey] = $this->getOptions()->getTtl();

$this->data[$normalizedKey] = $value;
return true;
}

protected function internalRemoveItem(& $normalizedKey)
{
unset($this->data[$normalizedKey]);
return true;
}

public function setCapabilities(Capabilities $capabilities)
{
$this->capabilities = $capabilities;
}
}

0 comments on commit 96fa750

Please sign in to comment.