Skip to content

Commit

Permalink
Added option in CatchException to catch exceptions in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik59 committed Feb 8, 2018
1 parent 350042b commit a8f3409
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/documentation/tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,36 @@ Sample usage
//then
CatchException::assertThat()->isInstanceOf("FooException");


Another example with catching errors in constructor:

::

//given
class Color {
public function __construct($firstParam, $secondParam, $thirdParam){
throw new Exception();
}
}

class ColorTest extends TestCase{
/**
* @test
*/
public function testConstructor(){
//given
$params =["firstParam", ["secondParam"], 3];

//when
CatchException::inConstructor(Color::class, $params);

//then
CatchException::assertThat()->isInstanceOf(Exception::class);
}
}



Assertions
----------

Expand Down
12 changes: 12 additions & 0 deletions src/Ouzo/Goodies/Tests/CatchException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Ouzo\Tests;

use Exception;
use ReflectionClass;
use Throwable;

/**
Expand Down Expand Up @@ -54,4 +56,14 @@ public static function get()
{
return self::$exception;
}

public static function inConstructor($className, array $params)
{
try {
$class = new ReflectionClass($className);
$class->newInstanceArgs($params);
} catch (Exception $e) {
self::$exception = $e;
}
}
}

0 comments on commit a8f3409

Please sign in to comment.