Permalink
Browse files

Added option in CatchException to catch exceptions in constructor

  • Loading branch information...
dominik59 committed Feb 8, 2018
1 parent 350042b commit a8f34096328a10c10c88dc663aa9a053e27266af
Showing with 42 additions and 0 deletions.
  1. +30 −0 docs/documentation/tests.rst
  2. +12 −0 src/Ouzo/Goodies/Tests/CatchException.php
@@ -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
----------
@@ -6,6 +6,8 @@
namespace Ouzo\Tests;
use Exception;
use ReflectionClass;
use Throwable;
/**
@@ -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.