Skip to content

Commit

Permalink
Merge pull request #11 from elliotchance/adapter-bug
Browse files Browse the repository at this point in the history
Fix another bug
  • Loading branch information
elliotchance committed Apr 29, 2014
2 parents 232ce01 + 0492f22 commit 785af6b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Hoard/AbstractPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ abstract class AbstractPool implements PoolInterface
protected $logger = null;

/**
* Create a pool with an adapter.
* Set the adapter.
* @param \Hoard\AdapterInterface $adapter The adapter.
*/
public function __construct(\Hoard\AdapterInterface $adapter)
public function setAdapter(\Hoard\AdapterInterface $adapter)
{
$this->adapter = $adapter;
}
Expand Down
15 changes: 8 additions & 7 deletions src/Hoard/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,18 @@ public static function getPool($poolName, array $config = array())
throw new NoSuchPoolException($poolName);
}

// create pool
$pool = new $className();
$adapterOptions = $pool->getAdapterOptions();

// create adapter
$adapterClass = '\Hoard\Adapter\Memcached';
if(array_key_exists('adapter', $config)) {
$adapterClass = $config['adapter'];
if(array_key_exists('adapter', $adapterOptions)) {
$adapterClass = $adapterOptions['adapter'];
}
$adapter = new $adapterClass($config);

// create pool
$pool = new $className($adapter);
$adapter = new $adapterClass($adapterOptions);
$adapter->setPool($pool);
$adapter->setAdapterOptions($pool->getAdapterOptions());
$pool->setAdapter($adapter);

// attach the default logger
$pool->setLogger(self::getDefaultLogger());
Expand Down
7 changes: 2 additions & 5 deletions test/Hoard/CacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,14 @@ public function testWillUseMemcacheAdapterIfNotProvided()

public function testWillUseAdapterIfProvided()
{
$config = array(
'adapter' => '\Hoard\DummyAdapter'
);
$pool = CacheManager::getPool('test.simple', $config);
$pool = CacheManager::getPool('test.custom_adapter');
$this->assertInstanceOf('\Hoard\DummyAdapter', $pool->getAdapter());
}

public function testWillSetAdapterOptionsWhenCreatingThePool()
{
$pool = CacheManager::getPool('test.simple');
$this->assertEquals($pool->getAdapter()->getAdapterOptions(), array('adapter' => 'options'));
$this->assertEquals($pool->getAdapter()->getAdapterOptions(), array('foo' => 'bar'));
}

public function testDefaultLoggerIsASingleton()
Expand Down
3 changes: 2 additions & 1 deletion test/Hoard/PoolTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function getMockedPool(array $items = array(), $poolName = '')
}

// mock the original business logic
$mock = $this->getMock($className, null, array($adapter));
$mock = $this->getMock($className, null);
$mock->setAdapter($adapter);
$adapter->setPool($mock);

// load in the items
Expand Down
16 changes: 16 additions & 0 deletions test/HoardPool/Test/CustomAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace HoardPool\Test;

class CustomAdapter extends Simple
{

public function getAdapterOptions()
{
return array(
'adapter' => '\Hoard\DummyAdapter'
);
}

}

2 changes: 1 addition & 1 deletion test/HoardPool/Test/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static function getAdapterName()
public function getAdapterOptions()
{
return array(
'adapter' => 'options'
'foo' => 'bar'
);
}

Expand Down

0 comments on commit 785af6b

Please sign in to comment.