Permalink
Browse files

Insure an exception is thrown when a class is unable to be resolved.

  • Loading branch information...
1 parent af43c26 commit a185139af6bb2da1c68b8c439220d7fd85e6ff60 @trq trq committed Jan 30, 2013
Showing with 39 additions and 25 deletions.
  1. +31 −25 lib/Proem/Service/AssetManager.php
  2. +8 −0 lib/Proem/Service/Tests/AssetManagerTest.php
@@ -280,37 +280,43 @@ protected function setParam($type, $index, $value, $override = false) {
*/
protected function autoResolve($name, $params)
{
- $reflection = new \ReflectionClass($name);
- if ($reflection->isInstantiable()) {
- $construct = $reflection->getConstructor();
- if ($construct === null) {
- $object = new $name;
- } else {
- $dependencies = $this->getDependencies($construct->getParameters());
- $object = $reflection->newInstanceArgs($dependencies);
- }
+ try {
+ $reflection = new \ReflectionClass($name);
+ if ($reflection->isInstantiable()) {
+ $construct = $reflection->getConstructor();
+ if ($construct === null) {
+ $object = new $name;
+ } else {
+ $dependencies = $this->getDependencies($construct->getParameters());
+ $object = $reflection->newInstanceArgs($dependencies);
+ }
- try {
- $method = $reflection->getMethod('setParams');
- $method->invokeArgs($object, $params);
- } catch (\ReflectionException $e) {}
+ try {
+ $method = $reflection->getMethod('setParams');
+ $method->invokeArgs($object, $params);
- // Allow a list of methods to be executed.
- if (isset($params['methods'])) {
- foreach ($params['methods'] as $method) {
+ // Do nothing. This method may very well not exist.
+ } catch (\ReflectionException $e) {}
+
+ // Allow a list of methods to be executed.
+ if (isset($params['methods'])) {
+ foreach ($params['methods'] as $method) {
+ $method = $reflection->getMethod($method);
+ $method->invokeArgs($object, $this->getDependencies($method->getParameters()));
+ }
+ }
+
+ // If this single method is invoked, its results will be returned.
+ if (isset($params['invoke'])) {
+ $method = $params['invoke'];
$method = $reflection->getMethod($method);
- $method->invokeArgs($object, $this->getDependencies($method->getParameters()));
+ return $method->invokeArgs($object, $this->getDependencies($method->getParameters()));
}
- }
- // If this single method is invoked, its results will be returned.
- if (isset($params['invoke'])) {
- $method = $params['invoke'];
- $method = $reflection->getMethod($method);
- return $method->invokeArgs($object, $this->getDependencies($method->getParameters()));
+ return $object;
}
-
- return $object;
+ } catch (\ReflectionException $e) {
+ throw new \LogicException("Unable to resolve '{$name}'");
}
}
}
@@ -55,6 +55,14 @@ public function testCanAttachAndRetreiveInstance()
$this->assertInstanceOf('\stdClass', $am->resolve('foo'));
}
+ /**
+ * @expectedException LogicException
+ */
+ public function testNonExistent()
+ {
+ (new AssetManager)->resolve('idonotexist');
+ }
+
public function testCanStoreAndRetreiveAssetObject()
{
$asset = m::mock('\Proem\Service\Asset', ['stdClass']);

0 comments on commit a185139

Please sign in to comment.