Skip to content

Commit

Permalink
Separate exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
sapsan4eg committed Apr 2, 2016
1 parent e38a6e9 commit 57fa37f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/Exceptions/InjectMustImplementException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Sixx\DependencyInjection\Exceptions;

class InjectMustImplementException extends InjectException
{

}
8 changes: 8 additions & 0 deletions src/Exceptions/InjectNotInjectedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Sixx\DependencyInjection\Exceptions;

class InjectNotInjectedException extends InjectException
{

}
8 changes: 8 additions & 0 deletions src/Exceptions/InjectRequiredParameterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Sixx\DependencyInjection\Exceptions;

class InjectRequiredParameterException extends InjectException
{

}
8 changes: 5 additions & 3 deletions src/Inject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Sixx\DependencyInjection;

use Sixx\DependencyInjection\Exceptions\InjectException;
use Sixx\DependencyInjection\Exceptions\InjectRequiredParameterException;
use Sixx\DependencyInjection\Exceptions\InjectNotInjectedException;

class Inject
{
Expand All @@ -28,7 +30,7 @@ public static function instantiation($className, array $parameters = null)
if (self::container()->isInjected($className))
return self::instantiation(self::container()->getServiceName($className), $parameters);

throw new InjectException("Inject error: interface " . $className . " exist but not injected yet.");
throw new InjectNotInjectedException("Inject error: interface " . $className . " exist but not injected yet.");
}

throw new InjectException("Inject error: class " . $className . " not exist.");
Expand Down Expand Up @@ -62,7 +64,7 @@ public static function method($className, $methodName, array $parameters = null)
if (self::container()->isInjected($className))
return self::method(self::container()->getServiceName($className), $methodName, $parameters);

throw new InjectException("Inject error: interface " . $className . " exist but not injected yet.");
throw new InjectNotInjectedException("Inject error: interface " . $className . " exist but not injected yet.");
}

throw new InjectException("Inject error: class " . $className . " not exist.");
Expand Down Expand Up @@ -96,7 +98,7 @@ protected static function getParameters(\ReflectionMethod $method, array $parame
elseif (self::container()->isInstantiate($parameter->getClass()))
$arguments[$parameter->getName()] = self::instantiation($parameter->getClass()->name);
elseif (true != $parameter->isOptional())
throw new InjectException("Required parameter [" . $parameter->getName() . "] in " . $method->getDeclaringClass()->name . "::" . $method->getName() . " is not specified.");
throw new InjectRequiredParameterException("Inject error: required parameter [" . $parameter->getName() . "] in " . $method->getDeclaringClass()->name . "::" . $method->getName() . " is not specified.");
}
} catch (\ReflectionException $exception) {
throw new InjectException("Inject error: " . $exception->getMessage());
Expand Down
4 changes: 2 additions & 2 deletions src/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sixx\DependencyInjection;

use Sixx\DependencyInjection\Exceptions\InjectException;
use Sixx\DependencyInjection\Exceptions\InjectMustImplementException;

class ServiceContainer
{
Expand Down Expand Up @@ -85,7 +85,7 @@ public function isInjected($name = null, $annotation = null)
return false;

if (false == $this->isImplement($this->getServiceName($name, $annotation), $name))
throw new InjectException("Inject error: class " . $this->getServiceName($name, $annotation) . " must implement " . $name);
throw new InjectMustImplementException("Inject error: class " . $this->getServiceName($name, $annotation) . " must implement " . $name);

return true;
}
Expand Down
22 changes: 16 additions & 6 deletions tests/InjectExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testExceptionNotPublicMethod()
}

/**
* @expectedException \Sixx\DependencyInjection\Exceptions\InjectException
* @expectedException \Sixx\DependencyInjection\Exceptions\InjectMustImplementException
* @expectedExceptionMessage Inject error: class Start must implement INext
*/
public function testExceptionClassMustImplement()
Expand All @@ -48,8 +48,8 @@ public function testExceptionClassMustImplement()
}

/**
* @expectedException \Sixx\DependencyInjection\Exceptions\InjectException
* @expectedExceptionMessage Required parameter [start] in ChildClass::hello is not specified.
* @expectedException \Sixx\DependencyInjection\Exceptions\InjectRequiredParameterException
* @expectedExceptionMessage Inject error: required parameter [start] in ChildClass::hello is not specified.
*/
public function testExceptionRequiredParameter()
{
Expand All @@ -58,7 +58,7 @@ public function testExceptionRequiredParameter()
}

/**
* @expectedException \Sixx\DependencyInjection\Exceptions\InjectException
* @expectedException \Sixx\DependencyInjection\Exceptions\InjectMustImplementException
* @expectedExceptionMessage Inject error: class Start must implement INext
*/
public function testExceptionClassMustImplements()
Expand All @@ -70,12 +70,22 @@ public function testExceptionClassMustImplements()
}

/**
* @expectedException \Sixx\DependencyInjection\Exceptions\InjectException
* @expectedExceptionMessage Required parameter [c] in SimpleParameter::__construct is not specified.
* @expectedException \Sixx\DependencyInjection\Exceptions\InjectRequiredParameterException
* @expectedExceptionMessage Inject error: required parameter [c] in SimpleParameter::__construct is not specified.
*/
public function testExceptionRequiredParameterDifferent()
{
Inject::bind("INext", "Next");
Inject::instantiation("SimpleParameter", ["c" => new Next()]);
}

/**
* @expectedException \Sixx\DependencyInjection\Exceptions\InjectNotInjectedException
* @expectedExceptionMessage Inject error: interface INext exist but not injected yet.
*/
public function testExceptionNotInjected()
{
Inject::flushServices();
Inject::instantiation("INext");
}
}

0 comments on commit 57fa37f

Please sign in to comment.