Skip to content

Commit

Permalink
Expanded DriverFactory exception messages and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tedivm committed May 9, 2014
1 parent a122cf2 commit a6cb5ff
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
13 changes: 13 additions & 0 deletions Factory/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class DriverFactory
*
* @param $types
* @param $options
* @throws \RuntimeException
* @return DriverInterface
*/
public static function createDriver($types, $options)
Expand All @@ -20,6 +21,18 @@ public static function createDriver($types, $options)
$h = array();

foreach ($types as $type) {

if (!isset($drivers[$type])) {
$allDrivers = Drivers::getAllDrivers();

if(isset($allDrivers[$type])) {
throw new \RuntimeException('Driver currently unavailable.');
}else{
throw new \RuntimeException('Driver does not exist.');
}
}


$class = $drivers[$type];
if ($type === 'Memcache' && isset($options[$type])) {
// Fix servers spec since underlying drivers expect plain arrays, not hashes.
Expand Down
69 changes: 56 additions & 13 deletions Tests/Factory/DriverFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ class DriverFactoryTest extends \PHPUnit_Framework_TestCase
'ttl' => 300,
'namespace' => null,
),
'Memcache' => array(
'servers' => array(
array(
'server' => '127.0.0.1',
'port' => '11211'
),
array(
'server' => '127.0.0.1',
'port' => '11212',
'weight' => '30'
),
array(
'server' => '127.0.0.1',
'port' => '11211',
'weight' => '30'
),
)
)
);

public function setUp()
Expand All @@ -43,32 +61,57 @@ public function testManufactureDrivers($types, $options)
$driver = DriverFactory::createDriver($types, $options);

if (count($types) > 1) {
$driverclass = $this->drivers['Composite'];
$driverClass = $this->drivers['Composite'];
$h = $this->getObjectAttribute($driver, 'drivers');
$drivers = array_combine($types, $h);
} else {
$driverclass = $this->drivers[$types[0]];
$driverClass = $this->drivers[$types[0]];
$drivers = array($types[0] => $driver);
}

$this->assertInstanceOf($driverclass, $driver);
$this->assertInstanceOf('Stash\Interfaces\DriverInterface', $driver);
$this->assertInstanceOf($driverClass, $driver);

foreach ($drivers as $subtype => $subdriver) {
$subdriverclass = $this->drivers[$subtype];
$this->assertInstanceOf($subdriverclass, $subdriver);
foreach ($drivers as $subtype => $subDriver) {
$subDriverClass = $this->drivers[$subtype];
$this->assertInstanceOf($subDriverClass, $subDriver);
}

foreach ($types as $type) {
$defaults = isset($this->defaultSettings[$type]) ? $this->defaultSettings[$type] : array();
$options = array_merge($defaults, $options);
}

/* foreach ($options as $optname => $optvalue) {
$this->assertAttributeEquals($optvalue, $optname, $drivers[$type]);
}
*/
public function testMemcacheSetup()
{
if(!isset($this->drivers['Memcache'])) {
$this->markTestSkipped('Memcache extension required for this test.');
}

$driver = DriverFactory::createDriver(array('Memcache'), $this->defaultSettings['Memcache']);
$this->assertInstanceOf('Stash\Interfaces\DriverInterface', $driver);
}


/**
*
* @expectedException \RuntimeException
* @expectedExceptionMessage Driver does not exist.
*/
public function testFakeDriverException()
{
DriverFactory::createDriver(array('FakeDriver'), array());
}

/**
*
* @expectedException \RuntimeException
* @expectedExceptionMessage Driver currently unavailable.
*/
public function testUnavailableDriverException()
{
Drivers::registerDriver('FakeDriver', 'Stash\Test\Stubs\DriverUnavailableStub');
DriverFactory::createDriver(array('FakeDriver'), array());
}


public function driverProvider()
{
return array(
Expand Down

0 comments on commit a6cb5ff

Please sign in to comment.