Skip to content

Commit

Permalink
Merge pull request #166 from tedivm/setPool
Browse files Browse the repository at this point in the history
Changed setDriver to setPool in Item class
  • Loading branch information
tedivm committed May 15, 2014
2 parents 174cba8 + 20e561e commit 881d530
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 67 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -28,6 +28,8 @@

* Removed constructor requirements from DriverInterface and added setOptions to replace it. Please note this means *all* drivers no longer take their options through the constructor, but expect them through the setOptions method.

* Replaced "Item->setDriver" with "Item->setPool" function. This should have no effect on typical consumers of this library but may effect those extending the Item or Pool class.

* Added Drivers::getAllDrivers which returns an unfiltered list of registered drivers.

* Added Drivers::getAvailableDrivers to replace the existing Drivers::getDrivers, which is now deprecated.
Expand Down
8 changes: 4 additions & 4 deletions src/Stash/Interfaces/ItemInterface.php
Expand Up @@ -15,13 +15,13 @@ interface ItemInterface
{

/**
* Sets the driver for the Item class to use.
* Sets the Parent Pool for the Item class to use.
*
* Typically called by Pool directly, and *must* be called before running caching functions.
*
* @param DriverInterface $driver
* @param PoolInterface $driver
*/
public function setDriver(DriverInterface $driver);
public function setPool(PoolInterface $driver);

/**
* Takes and sets the key and namespace.
Expand Down Expand Up @@ -117,7 +117,7 @@ public function isDisabled();
/**
* Return true if caching is disabled
*
* @param \PSR\Logger $logger
* @param \PSR\Log\LoggerInterface $logger
* @return bool
*/
public function setLogger($logger);
Expand Down
2 changes: 1 addition & 1 deletion src/Stash/Interfaces/PoolInterface.php
Expand Up @@ -123,7 +123,7 @@ public function getNamespace();
/**
* Sets a PSR\Logger style logging client to enable the tracking of errors.
*
* @param \PSR\Logger $logger
* @param \PSR\Log\LoggerInterface $logger
* @return bool
*/
public function setLogger($logger);
Expand Down
23 changes: 21 additions & 2 deletions src/Stash/Item.php
Expand Up @@ -14,6 +14,7 @@
use Stash\Exception\Exception;
use Stash\Interfaces\DriverInterface;
use Stash\Interfaces\ItemInterface;
use Stash\Interfaces\PoolInterface;

/**
* Stash caches data that has a high generation cost, such as template preprocessing or code that requires a database
Expand Down Expand Up @@ -105,6 +106,13 @@ class Item implements ItemInterface
*/
protected $stampedeRunning = false;

/**
* The Pool that spawned this instance of the Item..
*
* @var \Stash\Interfaces\PoolInterface
*/
protected $pool;

/**
* The cacheDriver being used by the system. While this class handles all of the higher functions, it's the cache
* driver here that handles all of the storage/retrieval functionality. This value is set by the constructor.
Expand Down Expand Up @@ -140,9 +148,10 @@ class Item implements ItemInterface
/**
* {@inheritdoc}
*/
public function setDriver(DriverInterface $driver)
public function setPool(PoolInterface $pool)
{
$this->driver = $driver;
$this->pool = $pool;
$this->setDriver($pool->getDriver());
}

/**
Expand Down Expand Up @@ -374,6 +383,16 @@ public function setLogger($logger)
$this->logger = $logger;
}

/**
* Sets the driver this object uses to interact with the caching system.
*
* @param DriverInterface $driver
*/
protected function setDriver(DriverInterface $driver)
{
$this->driver = $driver;
}

/**
* Logs an exception with the Logger class, if it exists.
*
Expand Down
14 changes: 7 additions & 7 deletions src/Stash/Pool.php
Expand Up @@ -133,18 +133,18 @@ public function getItem()
}
}

/** @var ItemInterface $cache */
$cache = new $this->itemClass();
$cache->setDriver($this->driver);
$cache->setKey($key, $namespace);
/** @var ItemInterface $item */
$item = new $this->itemClass();
$item->setPool($this);
$item->setKey($key, $namespace);

if($this->isDisabled)
$cache->disable();
$item->disable();

if(isset($this->logger))
$cache->setLogger($this->logger);
$item->setLogger($this->logger);

return $cache;
return $item;
}

/**
Expand Down
54 changes: 37 additions & 17 deletions tests/Stash/Test/AbstractItemTest.php
Expand Up @@ -15,6 +15,8 @@
use Stash\Utilities;
use Stash\Driver\Ephemeral;

use Stash\Test\Stubs\PoolGetDriverStub;

/**
* @package Stash
* @author Robert Hafner <tedivm@tedivm.com>
Expand Down Expand Up @@ -80,7 +82,10 @@ public function testConstruct($key = array())
$item = $this->getItem();
$this->assertTrue(is_a($item, 'Stash\Item'), 'Test object is an instance of Stash');

$item->setDriver($this->driver);
$poolStub = new PoolGetDriverStub();
$poolStub->setDriver($this->driver);
$item->setPool($poolStub);

$item->setKey($key);

return $item;
Expand Down Expand Up @@ -112,7 +117,9 @@ public function testSet()
}

$item = $this->getItem();
$item->setDriver(new Ephemeral(array()));
$poolStub = new PoolGetDriverStub();
$poolStub->setDriver(new Ephemeral(array()));
$item->setPool($poolStub);
$this->assertFalse($item->set($this->data), 'Item without key returns false for set.');
}

Expand All @@ -133,11 +140,15 @@ public function testGet()
}

if (!isset($this->driver)) {
$this->driver = new Ephemeral(array());
$this->driver = new Ephemeral();
}

$item = $this->getItem();
$item->setDriver(new Ephemeral(array()));

$poolStub = new PoolGetDriverStub();
$poolStub->setDriver(new Ephemeral());
$item->setPool($poolStub);

$this->assertEquals(null, $item->get(), 'Item without key returns null for get.');

}
Expand All @@ -149,14 +160,18 @@ public function testGet()
public function testGetItemInvalidKey()
{
$item = $this->getItem();
$item->setDriver(new Ephemeral(array()));
$poolStub = new PoolGetDriverStub();
$poolStub->setDriver(new Ephemeral(array()));
$item->setPool($poolStub);
$item->setKey('This is not an array');
}

public function testLock()
{
$item = $this->getItem();
$item->setDriver(new Ephemeral(array()));
$poolStub = new PoolGetDriverStub();
$poolStub->setDriver(new Ephemeral());
$item->setPool($poolStub);
$this->assertFalse($item->lock(), 'Item without key returns false for lock.');
}

Expand Down Expand Up @@ -415,25 +430,30 @@ public function testDisable()

public function testDisableCacheWillNeverCallDriver()
{
$stash = $this->getItem();
$stash->setDriver($this->getMockedDriver());
$stash->setKey(array('test', 'key'));
$stash->disable();
$this->assertTrue($stash->isDisabled());
$this->assertDisabledStash($stash);
$item = $this->getItem();
$poolStub = new PoolGetDriverStub();
$poolStub->setDriver($this->getMockedDriver());
$item->setPool($poolStub);
$item->setKey(array('test', 'key'));
$item->disable();

$this->assertTrue($item->isDisabled());
$this->assertDisabledStash($item);
}

public function testDisableCacheGlobally()
{
Item::$runtimeDisable = true;
$testDriver = $this->getMockedDriver();

$stash = $this->getItem();
$stash->setDriver($testDriver);
$stash->setKey(array('test', 'key'));
$item = $this->getItem();
$poolStub = new PoolGetDriverStub();
$poolStub->setDriver($this->getMockedDriver());
$item->setPool($poolStub);
$item->setKey(array('test', 'key'));

$this->assertDisabledStash($stash);
$this->assertTrue($stash->isDisabled());
$this->assertDisabledStash($item);
$this->assertTrue($item->isDisabled());
$this->assertFalse($testDriver->wasCalled(), 'Driver was not called after Item was disabled.');
Item::$runtimeDisable = false;
}
Expand Down
38 changes: 24 additions & 14 deletions tests/Stash/Test/CacheExceptionTest.php
Expand Up @@ -12,6 +12,7 @@
namespace Stash\Test;

use Stash\Test\Stubs\DriverExceptionStub;
use Stash\Test\Stubs\PoolGetDriverStub;
use Stash\Pool;
use Stash\Item;

Expand All @@ -24,7 +25,9 @@ class CacheExceptionTest extends \PHPUnit_Framework_TestCase
public function testSet()
{
$item = new Item();
$item->setDriver(new DriverExceptionStub());
$poolStub = new PoolGetDriverStub();
$poolStub->setDriver(new DriverExceptionStub());
$item->setPool($poolStub);
$item->setKey(array('path', 'to', 'store'));

$this->assertFalse($item->isDisabled());
Expand All @@ -35,7 +38,9 @@ public function testSet()
public function testGet()
{
$item = new Item();
$item->setDriver(new DriverExceptionStub());
$poolStub = new PoolGetDriverStub();
$poolStub->setDriver(new DriverExceptionStub());
$item->setPool($poolStub);
$item->setKey(array('path', 'to', 'get'));

$this->assertFalse($item->isDisabled());
Expand All @@ -46,8 +51,9 @@ public function testGet()
public function testClear()
{
$item = new Item();
$item->setDriver(new DriverExceptionStub());
$item->setKey(array('path', 'to', 'clear'));
$poolStub = new PoolGetDriverStub();
$poolStub->setDriver(new DriverExceptionStub());
$item->setPool($poolStub); $item->setKey(array('path', 'to', 'clear'));

$this->assertFalse($item->isDisabled());
$this->assertFalse($item->clear());
Expand All @@ -56,25 +62,29 @@ public function testClear()

public function testPurge()
{
$pool = new Pool(new DriverExceptionStub());
$stash = $pool->getItem('test');
$this->assertFalse($stash->isDisabled());
$pool = new Pool();
$pool->setDriver(new DriverExceptionStub());

$item = $pool->getItem('test');
$this->assertFalse($item->isDisabled());
$this->assertFalse($pool->purge());

$stash = $pool->getItem('test');
$this->assertTrue($stash->isDisabled(), 'Is disabled after exception is thrown in driver');
$item = $pool->getItem('test');
$this->assertTrue($item->isDisabled(), 'Is disabled after exception is thrown in driver');
$this->assertFalse($pool->purge());
}

public function testFlush()
{
$pool = new Pool(new DriverExceptionStub());
$stash = $pool->getItem('test');
$this->assertFalse($stash->isDisabled());
$pool = new Pool();
$pool->setDriver(new DriverExceptionStub());

$item = $pool->getItem('test');
$this->assertFalse($item->isDisabled());
$this->assertFalse($pool->flush());

$stash = $pool->getItem('test');
$this->assertTrue($stash->isDisabled(), 'Is disabled after exception is thrown in driver');
$item = $pool->getItem('test');
$this->assertTrue($item->isDisabled(), 'Is disabled after exception is thrown in driver');
$this->assertFalse($pool->flush());
}

Expand Down
13 changes: 9 additions & 4 deletions tests/Stash/Test/Driver/EphemeralTest.php
Expand Up @@ -12,6 +12,7 @@
namespace Stash\Test\Driver;

use Stash\Item;
use Stash\Test\Stubs\PoolGetDriverStub;

/**
* @package Stash
Expand All @@ -24,14 +25,16 @@ class EphemeralTest extends AbstractDriverTest
public function testKeyCollisions1()
{
$driver = new $this->driverClass;
$poolStub = new PoolGetDriverStub();
$poolStub->setDriver($driver);

$item1 = new Item();
$item1->setDriver(($driver));
$item1->setPool($poolStub);
$item1->setKey(array('##' , '#'));
$item1->set('X');

$item2 = new Item();
$item2->setDriver(($driver));
$item2->setPool($poolStub);
$item2->setKey(array('#' , '##'));
$item2->set('Y');

Expand All @@ -41,14 +44,16 @@ public function testKeyCollisions1()
public function testKeyCollisions2()
{
$driver = new $this->driverClass;
$poolStub = new PoolGetDriverStub();
$poolStub->setDriver($driver);

$item1 = new Item();
$item1->setDriver(($driver));
$item1->setPool($poolStub);
$item1->setKey(array('#'));
$item1->set('X');

$item2 = new Item();
$item2->setDriver(($driver));
$item2->setPool($poolStub);
$item2->setKey(array(':'));
$item2->set('Y');

Expand Down
6 changes: 5 additions & 1 deletion tests/Stash/Test/Driver/FileSystemTest.php
Expand Up @@ -11,6 +11,7 @@

namespace Stash\Test\Driver;

use Stash\Test\Stubs\PoolGetDriverStub;
use Stash\Driver\FileSystem;
use Stash\Item;

Expand Down Expand Up @@ -69,7 +70,10 @@ public function testOptionKeyHashFunctionDirs()
$rand = str_repeat(uniqid(), 32);

$item = new Item();
$item->setDriver($driver);

$poolStub = new PoolGetDriverStub();
$poolStub->setDriver($driver);
$item->setPool($poolStub);
$item->setKey($paths);
$item->set($rand);

Expand Down

0 comments on commit 881d530

Please sign in to comment.