From a6cb5ff827064af0eccd76fba30b8d443d0315d4 Mon Sep 17 00:00:00 2001 From: Robert Hafner Date: Fri, 9 May 2014 00:51:20 -0700 Subject: [PATCH] Expanded DriverFactory exception messages and tests --- Factory/DriverFactory.php | 13 ++++++ Tests/Factory/DriverFactoryTest.php | 69 +++++++++++++++++++++++------ 2 files changed, 69 insertions(+), 13 deletions(-) diff --git a/Factory/DriverFactory.php b/Factory/DriverFactory.php index 1a7bbf5..9e503f7 100644 --- a/Factory/DriverFactory.php +++ b/Factory/DriverFactory.php @@ -11,6 +11,7 @@ class DriverFactory * * @param $types * @param $options + * @throws \RuntimeException * @return DriverInterface */ public static function createDriver($types, $options) @@ -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. diff --git a/Tests/Factory/DriverFactoryTest.php b/Tests/Factory/DriverFactoryTest.php index 7f79f76..4338854 100644 --- a/Tests/Factory/DriverFactoryTest.php +++ b/Tests/Factory/DriverFactoryTest.php @@ -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() @@ -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(