Skip to content

Commit

Permalink
Merge pull request #96 from boekkooi/exceptional-exceptions
Browse files Browse the repository at this point in the history
Exceptional exceptions
  • Loading branch information
rosstuck committed Feb 19, 2016
2 parents 9b0e554 + 6f75e36 commit cda5b1e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php"
>

<listeners>
Expand Down
10 changes: 6 additions & 4 deletions src/Exception/CanNotDetermineCommandNameException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
class CanNotDetermineCommandNameException extends \RuntimeException implements Exception
{
/**
* @var object
* @var mixed
*/
private $command;

/**
* @param object $command
* @param mixed $command
*
* @return static
*/
public static function forCommand($command)
{
$exception = new static('Could not determine command name of ' . get_class($command));
$type = is_object($command) ? get_class($command) : gettype($command);

$exception = new static('Could not determine command name of ' . $type);
$exception->command = $command;

return $exception;
Expand All @@ -28,7 +30,7 @@ public static function forCommand($command)
/**
* Returns the command that could not be invoked
*
* @return object
* @return mixed
*/
public function getCommand()
{
Expand Down
10 changes: 6 additions & 4 deletions src/Exception/CanNotInvokeHandlerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@
class CanNotInvokeHandlerException extends \BadMethodCallException implements Exception
{
/**
* @var object
* @var mixed
*/
private $command;

/**
* @param object $command
* @param mixed $command
* @param string $reason
*
* @return static
*/
public static function forCommand($command, $reason)
{
$type = is_object($command) ? get_class($command) : gettype($command);

$exception = new static(
'Could not invoke handler for command ' . get_class($command) .
'Could not invoke handler for command ' . $type .
' for reason: ' . $reason
);
$exception->command = $command;
Expand All @@ -35,7 +37,7 @@ public static function forCommand($command, $reason)
/**
* Returns the command that could not be invoked
*
* @return object
* @return mixed
*/
public function getCommand()
{
Expand Down
36 changes: 36 additions & 0 deletions tests/Exception/CanNotDetermineCommandNameExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Tactician\CommandBus\Tests\Exception;

use League\Tactician\Exception\CanNotDetermineCommandNameException;

class CanNotDetermineCommandNameExceptionTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideAnyTypeOfCommand
*/
public function testForAnyTypeOfCommand($command)
{
$exception = CanNotDetermineCommandNameException::forCommand($command);
$this->assertSame($command, $exception->getCommand());
}

public function provideAnyTypeOfCommand()
{
return [
[ 1 ],
[ new \stdClass() ],
[ null ],
[ 'a string' ],
[ new \SplFileInfo(__FILE__) ],
[ true ],
[ false ],
[ [] ],
[ [ [ 1 ] ] ],
[
function () {
}
],
];
}
}
28 changes: 28 additions & 0 deletions tests/Exception/CanNotInvokeHandlerExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,32 @@ public function testExceptionContainsDebuggingInfo()
$this->assertSame($command, $exception->getCommand());
$this->assertInstanceOf(Exception::class, $exception);
}

/**
* @dataProvider provideAnyTypeOfCommand
*/
public function testForAnyTypeOfCommand($command)
{
$exception = CanNotInvokeHandlerException::forCommand($command, 'happens');
$this->assertSame($command, $exception->getCommand());
}

public function provideAnyTypeOfCommand()
{
return [
[ 1 ],
[ new \stdClass() ],
[ null ],
[ 'a string' ],
[ new \SplFileInfo(__FILE__) ],
[ true ],
[ false ],
[ [] ],
[ [ [ 1 ] ] ],
[
function () {
}
],
];
}
}

0 comments on commit cda5b1e

Please sign in to comment.