Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Provider update and unit test coverage 100%

  • Loading branch information...
commit f26b89cc077349311c11f67e8180716c9c6c367a 1 parent 4bdeee3
@Evoke-PHP authored
View
20 php/src/Evoke/Service/Provider.php
@@ -153,11 +153,19 @@ protected function getDependency(ReflectionParameter $reflectionParam,
return $passedParameters[$reflectionParam->name];
}
+ /* As far as I can tell the difference between isDefaultValueAvailable
+ * and isOptional is that:
+ * isDefaultValueAvailable is for classes in PHP
+ * isOptional is for builtin objects from the PHP engine
+ */
if ($reflectionParam->isDefaultValueAvailable())
{
- // Use a default value.
return $reflectionParam->getDefaultValue();
}
+ elseif ($reflectionParam->isOptional())
+ {
+ return null;
+ }
$depClass = $reflectionParam->getClass();
@@ -165,7 +173,7 @@ protected function getDependency(ReflectionParameter $reflectionParam,
{
$message = 'Missing ';
$message .= $reflectionParam->isArray() ? 'Array' : 'Scalar';
- $message .= ' dependency.';
+ $message .= ' Dependency';
// It must be an unset Scalar or Array. Bail hard and early.
throw new InvalidArgumentException($message);
@@ -177,12 +185,8 @@ protected function getDependency(ReflectionParameter $reflectionParam,
return $this->make($depClass->name);
}
- if ($reflectionParam->isOptional())
- {
- return null;
- }
-
- throw new InvalidArgumentException('Missing Interface Dependency');
+ throw new InvalidArgumentException(
+ 'Missing Abstract/Interface Dependency');
}
/**
View
167 php/test/unit/Evoke/Service/ProviderTest.php
@@ -5,33 +5,6 @@
Evoke\Service\Provider,
PHPUnit_Framework_TestCase;
-// Interface for the test.
-interface PI
-{
-
-}
-
-// Classes for the test.
-class PA
-{
- // No constructor means no dependencies.
-}
-
-class PB
-{
- public function __construct(PI $paramOne, Array $arr) {}
-}
-
-class PC implements PI
-{
- public function __construct($int, $strDefault = 'Paul') {}
-}
-
-class PD
-{
- public function __construct(PA $concrete, PI $interface) {}
-}
-
/**
* Provider Test.
*/
@@ -64,6 +37,113 @@ public function testMake($object, $classname, $params)
$this->assertInstanceOf($classname, $object->make($classname, $params));
}
+ /**
+ * Trying to make an object with a missing Abstract/Interface throws IAE.
+ *
+ * @covers Evoke\Service\Provider::getDependency
+ * @depends Evoke_Test\Service\ProviderTest::test__construct
+ * @expectedException InvalidArgumentException
+ * @expectedExceptionMessage Missing Abstract/Interface Dependency
+ */
+ public function testMissingDependencyAbstractInterface()
+ {
+ $object = new Provider($this->getMock('Evoke\Service\CacheIface'));
+ $object->make('Evoke_Test\Service\PD');
+ }
+
+ /**
+ * Trying to make an object with a missing Array throws IAE.
+ *
+ * @covers Evoke\Service\Provider::getDependency
+ * @depends Evoke_Test\Service\ProviderTest::test__construct
+ * @expectedException InvalidArgumentException
+ * @expectedExceptionMessage Missing Array Dependency
+ */
+ public function testMissingDependencyArray()
+ {
+ $object = new Provider($this->getMock('Evoke\Service\CacheIface'));
+ $object->make('Evoke_Test\Service\PB',
+ array('Param_One' => $this->getMock(
+ 'Evoke_Test\Service\PC', NULL, array(1))));
+ }
+
+ /**
+ * Trying to make an object with a missing scalar throws IAE.
+ *
+ * @covers Evoke\Service\Provider::getDependency
+ * @depends Evoke_Test\Service\ProviderTest::test__construct
+ * @expectedException InvalidArgumentException
+ * @expectedExceptionMessage Missing Scalar Dependency
+ */
+ public function testMissingDependencyScalar()
+ {
+ $object = new Provider($this->getMock('Evoke\Service\CacheIface'));
+ $object->make('Evoke_Test\Service\PC');
+ }
+
+ /**
+ * Test that the reflection cache is set when it is fresh.
+ *
+ * @covers Evoke\Service\Provider::getReflection
+ * @depends Evoke_Test\Service\ProviderTest::test__construct
+ */
+ public function testReflectionCacheFresh()
+ {
+ $cache = $this->getMock('Evoke\Service\CacheIface');
+
+ $i = 0;
+ $cache->expects($this->at($i++))
+ ->method('exists')
+ ->with($this->equalTo('Evoke_Test\Service\PD'))
+ ->will($this->returnValue(false));
+
+ $cache->expects($this->at($i++))
+ ->method('set')
+ ->with($this->equalTo('Evoke_Test\Service\PD'),
+ $this->arrayHasKey('Class'));
+
+
+ $object = new Provider($cache);
+ $object->make('Evoke_Test\Service\PD',
+ array('Interface' => $this->getMock(
+ 'Evoke_Test\Service\PC', NULL, array(1))));
+ }
+
+ /**
+ * Test that the reflection cache is used when it has information.
+ *
+ * @covers Evoke\Service\Provider::getReflection
+ * @depends Evoke_Test\Service\ProviderTest::test__construct
+ */
+ public function testReflectionCacheUsed()
+ {
+ // Now simulate the cache being used.
+ $usedCache = $this->getMock('Evoke\Service\CacheIface');
+
+ $i = 0;
+ $usedCache->expects($this->at($i++))
+ ->method('exists')
+ ->with($this->equalTo('Evoke_Test\Service\PD'))
+ ->will($this->returnValue(true));
+
+ $pdClass = new \ReflectionClass('Evoke_Test\Service\PD');
+ $pdParams = $pdClass->getConstructor()->getParameters();
+
+ $usedCache->expects($this->at($i++))
+ ->method('get')
+ ->with($this->equalTo('Evoke_Test\Service\PD'))
+ ->will($this->returnValue(
+ array('Class' => $pdClass,
+ 'Params' => $pdParams)));
+
+ $object = new Provider($usedCache);
+ $this->assertInstanceOf(
+ 'Evoke_Test\Service\PD',
+ $object->make('Evoke_Test\Service\PD',
+ array('Interface' => $this->getMock(
+ 'Evoke_Test\Service\PC', NULL, array(1)))));
+ }
+
/******************/
/* Data Providers */
/******************/
@@ -99,9 +179,42 @@ public function providerMake()
'Classname' => 'Evoke_Test\Service\PD',
'Params' => array('Interface' => $this->getMock(
'Evoke_Test\Service\PC', NULL, array(1))));
+
+ $tests['Built_In_Class'] = array(
+ 'Object' => new Provider($this->getMock('Evoke\Service\Cache')),
+ 'Classname' => 'DateTime',
+ 'Params' => array());
return $tests;
}
}
+// Interface for the test.
+interface PI
+{
+
+}
+
+// Classes for the test.
+class PA
+{
+ // No constructor means no dependencies.
+}
+
+class PB
+{
+ public function __construct(PI $paramOne, Array $arr) {}
+}
+
+class PC implements PI
+{
+ public function __construct($int, $strDefault = 'Paul') {}
+}
+
+class PD
+{
+ public function __construct(PA $concrete, PI $interface) {}
+}
+
+
// EOF
Please sign in to comment.
Something went wrong with that request. Please try again.