From 3e59f950440d2af9ea0b783bd05d7d5eae1d0557 Mon Sep 17 00:00:00 2001 From: Bernd Stellwag Date: Wed, 3 Oct 2018 13:43:14 +0200 Subject: [PATCH 1/8] Asserts: add expectThrowable() With this method you can not only test Exceptions, like with expectException, but also Errors. Signed-off-by: Bernd Stellwag --- src/Codeception/Module/Asserts.php | 77 ++++++++++++++----- .../_support/_generated/DataTesterActions.php | 38 ++++++++- tests/unit/Codeception/Module/AssertsTest.php | 58 ++++++++++++++ 3 files changed, 154 insertions(+), 19 deletions(-) diff --git a/src/Codeception/Module/Asserts.php b/src/Codeception/Module/Asserts.php index 6e77930fdf..a650818d30 100644 --- a/src/Codeception/Module/Asserts.php +++ b/src/Codeception/Module/Asserts.php @@ -455,40 +455,81 @@ public function fail($message) * * @param $exception string or \Exception * @param $callback + * + * @deprecated Use expectThrowable instead */ public function expectException($exception, $callback) { - $code = null; - $msg = null; - if (is_object($exception)) { - /** @var $exception \Exception **/ - $class = get_class($exception); - $msg = $exception->getMessage(); - $code = $exception->getCode(); + $this->expectThrowable($exception, $callback); + } + + /** + * Handles and checks throwables (Exceptions/Errors) called inside the callback function. + * Either throwable class name or throwable instance should be provided. + * + * ```php + * expectThrowable(MyThrowable::class, function() { + * $this->doSomethingBad(); + * }); + * + * $I->expectThrowable(new MyException(), function() { + * $this->doSomethingBad(); + * }); + * ``` + * If you want to check message or throwable code, you can pass them with throwable instance: + * ```php + * expectThrowable(new MyError("Don't do bad things"), function() { + * $this->doSomethingBad(); + * }); + * ``` + * + * @param $throwable string or \Throwable + * @param $callback + */ + public function expectThrowable($throwable, $callback) + { + if (is_object($throwable)) { + /** @var $throwable \Throwable */ + $class = get_class($throwable); + $msg = $throwable->getMessage(); + $code = $throwable->getCode(); } else { - $class = $exception; + $class= $throwable; + $msg = null; + $code = null; } + try { $callback(); - } catch (\Exception $e) { - if (!$e instanceof $class) { - $this->fail(sprintf("Exception of class $class expected to be thrown, but %s caught", get_class($e))); + } catch (\Throwable $t) { + if (!($t instanceof $class)) { + $this->fail(sprintf( + "Throwable of class '$class' expected to be thrown, but class '%s' was caught", + get_class($t) + )); } - if (null !== $msg and $e->getMessage() !== $msg) { + + if (null !== $msg && $t->getMessage() !== $msg) { $this->fail(sprintf( - "Exception of $class expected to be '$msg', but actual message was '%s'", - $e->getMessage() + "Throwable of class '$class' expected to have message '$msg', but actual message was '%s'", + $t->getMessage() )); } - if (null !== $code and $e->getCode() !== $code) { + + if (null !== $code && $t->getCode() !== $code) { $this->fail(sprintf( - "Exception of $class expected to have code $code, but actual code was %s", - $e->getCode() + "Throwable of class '$class' expected to have code '$code', but actual code was '%s'", + $t->getCode() )); } + $this->assertTrue(true); // increment assertion counter + return; } - $this->fail("Expected exception of $class to be thrown, but nothing was caught"); + $this->fail("Expected throwable of class '$class'' to be thrown, but nothing was caught"); } } diff --git a/tests/data/snapshots/tests/_support/_generated/DataTesterActions.php b/tests/data/snapshots/tests/_support/_generated/DataTesterActions.php index 2bb00d11cd..87ab83f863 100644 --- a/tests/data/snapshots/tests/_support/_generated/DataTesterActions.php +++ b/tests/data/snapshots/tests/_support/_generated/DataTesterActions.php @@ -1,4 +1,4 @@ -expectThrowable(MyThrowable::class, function() { + * $this->doSomethingBad(); + * }); + * + * $I->expectThrowable(new MyException(), function() { + * $this->doSomethingBad(); + * }); + * ``` + * If you want to check message or throwable code, you can pass them with throwable instance: + * ```php + * expectThrowable(new MyError("Don't do bad things"), function() { + * $this->doSomethingBad(); + * }); + * ``` + * + * @param $throwable string of \Throwable + * @param $callback + * @see \Codeception\Module\Asserts::expectThrowable() + */ + public function expectThrowable($throwable, $callback) { + return $this->getScenario()->runStep(new \Codeception\Step\Action('expectThrowable', func_get_args())); + } + + /** * [!] Method is generated. Documentation taken from corresponding module. * diff --git a/tests/unit/Codeception/Module/AssertsTest.php b/tests/unit/Codeception/Module/AssertsTest.php index e237175049..e4a8b3d89e 100644 --- a/tests/unit/Codeception/Module/AssertsTest.php +++ b/tests/unit/Codeception/Module/AssertsTest.php @@ -70,4 +70,62 @@ public function testOutputExceptionTimeWhenNothingCaught() $module->expectException(RuntimeException::class, function () { }); } + + public function testExceptThrowable() + { + $module = new \Codeception\Module\Asserts(make_container()); + $module->expectThrowable('Exception', function () { + throw new Exception(); + }); + $module->expectThrowable(new Error('here'), function () { + throw new Error('here'); + }); + $module->expectThrowable(new Exception('here', 200), function () { + throw new Exception('here', 200); + }); + } + + /** + * @expectedException PHPUnit\Framework\AssertionFailedError + */ + public function testExpectThrowableFailOnDifferentClass() + { + $module = new \Codeception\Module\Asserts(make_container()); + $module->expectThrowable(new Exception(), function () { + throw new Error(); + }); + } + + /** + * @expectedException PHPUnit\Framework\AssertionFailedError + */ + public function testExceptThrowableFailOnDifferentMessage() + { + $module = new \Codeception\Module\Asserts(make_container()); + $module->expectThrowable(new Exception('foo', 200), function () { + throw new Exception('bar', 200); + }); + } + + /** + * @expectedException PHPUnit\Framework\AssertionFailedError + */ + public function testExceptThrowableFailOnDifferentCode() + { + $module = new \Codeception\Module\Asserts(make_container()); + $module->expectThrowable(new Exception('foobar', 200), function () { + throw new Exception('foobar', 2); + }); + } + + /** + * @expectedException PHPUnit\Framework\AssertionFailedError + * @expectedExceptionMessageRegExp /RuntimeException/ + */ + public function testExpectThrowableFailOnNothingCaught() + { + $module = new \Codeception\Module\Asserts(make_container()); + $module->expectThrowable(RuntimeException::class, function () { + }); + } } From 2e75f92b5843447a5cf4e0b2d31658fcce65fe67 Mon Sep 17 00:00:00 2001 From: Bernd Stellwag Date: Wed, 3 Oct 2018 13:45:57 +0200 Subject: [PATCH 2/8] Asserts tests: set up test class in setUp() Signed-off-by: Bernd Stellwag --- tests/unit/Codeception/Module/AssertsTest.php | 98 +++++++++---------- 1 file changed, 48 insertions(+), 50 deletions(-) diff --git a/tests/unit/Codeception/Module/AssertsTest.php b/tests/unit/Codeception/Module/AssertsTest.php index e4a8b3d89e..266bdadd23 100644 --- a/tests/unit/Codeception/Module/AssertsTest.php +++ b/tests/unit/Codeception/Module/AssertsTest.php @@ -1,50 +1,55 @@ module = new \Codeception\Module\Asserts(make_container()); + } + public function testAsserts() { - $module = new \Codeception\Module\Asserts(make_container()); - $module->assertEquals(1, 1); - $module->assertContains(1, [1, 2]); - $module->assertSame(1, 1); - $module->assertNotSame(1, '1'); - $module->assertRegExp('/^[\d]$/', '1'); - $module->assertNotRegExp('/^[a-z]$/', '1'); - $module->assertStringStartsWith('fo', 'foo'); - $module->assertStringStartsNotWith('ba', 'foo'); - $module->assertEmpty([]); - $module->assertNotEmpty([1]); - $module->assertNull(null); - $module->assertNotNull(''); - $module->assertNotNull(false); - $module->assertNotNull(0); - $module->assertTrue(true); - $module->assertNotTrue(false); - $module->assertNotTrue(null); - $module->assertNotTrue('foo'); - $module->assertFalse(false); - $module->assertNotFalse(true); - $module->assertNotFalse(null); - $module->assertNotFalse('foo'); - $module->assertFileExists(__FILE__); - $module->assertFileNotExists(__FILE__ . '.notExist'); - $module->assertInstanceOf('Exception', new Exception()); - $module->assertInternalType('integer', 5); - $module->assertArrayHasKey('one', ['one' => 1, 'two' => 2]); - $module->assertArraySubset(['foo' => [1]], ['foo' => [1, 2]]); - $module->assertCount(3, [1, 2, 3]); + $this->module->assertEquals(1, 1); + $this->module->assertContains(1, [1, 2]); + $this->module->assertSame(1, 1); + $this->module->assertNotSame(1, '1'); + $this->module->assertRegExp('/^[\d]$/', '1'); + $this->module->assertNotRegExp('/^[a-z]$/', '1'); + $this->module->assertStringStartsWith('fo', 'foo'); + $this->module->assertStringStartsNotWith('ba', 'foo'); + $this->module->assertEmpty([]); + $this->module->assertNotEmpty([1]); + $this->module->assertNull(null); + $this->module->assertNotNull(''); + $this->module->assertNotNull(false); + $this->module->assertNotNull(0); + $this->module->assertTrue(true); + $this->module->assertNotTrue(false); + $this->module->assertNotTrue(null); + $this->module->assertNotTrue('foo'); + $this->module->assertFalse(false); + $this->module->assertNotFalse(true); + $this->module->assertNotFalse(null); + $this->module->assertNotFalse('foo'); + $this->module->assertFileExists(__FILE__); + $this->module->assertFileNotExists(__FILE__ . '.notExist'); + $this->module->assertInstanceOf('Exception', new Exception()); + $this->module->assertInternalType('integer', 5); + $this->module->assertArrayHasKey('one', ['one' => 1, 'two' => 2]); + $this->module->assertArraySubset(['foo' => [1]], ['foo' => [1, 2]]); + $this->module->assertCount(3, [1, 2, 3]); } public function testExceptions() { - $module = new \Codeception\Module\Asserts(make_container()); - $module->expectException('Exception', function () { + $this->module->expectException('Exception', function () { throw new Exception; }); - $module->expectException(new Exception('here'), function () { + $this->module->expectException(new Exception('here'), function () { throw new Exception('here'); }); - $module->expectException(new Exception('here', 200), function () { + $this->module->expectException(new Exception('here', 200), function () { throw new Exception('here', 200); }); } @@ -54,8 +59,7 @@ public function testExceptions() */ public function testExceptionFails() { - $module = new \Codeception\Module\Asserts(make_container()); - $module->expectException(new Exception('here', 200), function () { + $this->module->expectException(new Exception('here', 200), function () { throw new Exception('here', 2); }); } @@ -66,21 +70,19 @@ public function testExceptionFails() */ public function testOutputExceptionTimeWhenNothingCaught() { - $module = new \Codeception\Module\Asserts(make_container()); - $module->expectException(RuntimeException::class, function () { + $this->module->expectException(RuntimeException::class, function () { }); } public function testExceptThrowable() { - $module = new \Codeception\Module\Asserts(make_container()); - $module->expectThrowable('Exception', function () { + $this->module->expectThrowable('Exception', function () { throw new Exception(); }); - $module->expectThrowable(new Error('here'), function () { + $this->module->expectThrowable(new Error('here'), function () { throw new Error('here'); }); - $module->expectThrowable(new Exception('here', 200), function () { + $this->module->expectThrowable(new Exception('here', 200), function () { throw new Exception('here', 200); }); } @@ -90,8 +92,7 @@ public function testExceptThrowable() */ public function testExpectThrowableFailOnDifferentClass() { - $module = new \Codeception\Module\Asserts(make_container()); - $module->expectThrowable(new Exception(), function () { + $this->module->expectThrowable(new Exception(), function () { throw new Error(); }); } @@ -101,8 +102,7 @@ public function testExpectThrowableFailOnDifferentClass() */ public function testExceptThrowableFailOnDifferentMessage() { - $module = new \Codeception\Module\Asserts(make_container()); - $module->expectThrowable(new Exception('foo', 200), function () { + $this->module->expectThrowable(new Exception('foo', 200), function () { throw new Exception('bar', 200); }); } @@ -112,8 +112,7 @@ public function testExceptThrowableFailOnDifferentMessage() */ public function testExceptThrowableFailOnDifferentCode() { - $module = new \Codeception\Module\Asserts(make_container()); - $module->expectThrowable(new Exception('foobar', 200), function () { + $this->module->expectThrowable(new Exception('foobar', 200), function () { throw new Exception('foobar', 2); }); } @@ -124,8 +123,7 @@ public function testExceptThrowableFailOnDifferentCode() */ public function testExpectThrowableFailOnNothingCaught() { - $module = new \Codeception\Module\Asserts(make_container()); - $module->expectThrowable(RuntimeException::class, function () { + $this->module->expectThrowable(RuntimeException::class, function () { }); } } From 4ece70e8586b40ef4cf46790632c626781bf10af Mon Sep 17 00:00:00 2001 From: Bernd Stellwag Date: Wed, 3 Oct 2018 14:47:04 +0200 Subject: [PATCH 3/8] Asserts: mention expectThrowable in the documentation Signed-off-by: Bernd Stellwag --- docs/modules/Asserts.md | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/docs/modules/Asserts.md b/docs/modules/Asserts.md index 7ae38dc8ab..2d5234d71a 100644 --- a/docs/modules/Asserts.md +++ b/docs/modules/Asserts.md @@ -314,30 +314,39 @@ Checks that condition is positive. ### expectException + +@see expectThrowable +@deprecated Use expectThrowable instead. + + * `param` $exception string or \Exception + * `param` $callback + + +### expectThrowable -Handles and checks exception called inside callback function. -Either exception class name or exception instance should be provided. +Handles and checks throwables (Exceptions/Errors) called inside the callback function. +Either throwable class name or throwable instance should be provided. ```php expectException(MyException::class, function() { +$I->expectThrowable(MyThrowable::class, function() { $this->doSomethingBad(); }); -$I->expectException(new MyException(), function() { +$I->expectThrowable(new MyException(), function() { $this->doSomethingBad(); }); ``` -If you want to check message or exception code, you can pass them with exception instance: +If you want to check message or throwable code, you can pass them with throwable instance: ```php expectException(new MyException("Don't do bad things"), function() { +// will check that throwable MyError is thrown with "Don't do bad things" message +$I->expectThrowable(new MyError("Don't do bad things"), function() { $this->doSomethingBad(); }); ``` - * `param` $exception string or \Exception + * `param` $throwable string or \Throwable * `param` $callback From 7fc3ed86e2413eeb9641dd2cf9990a7c3abb9701 Mon Sep 17 00:00:00 2001 From: Bernd Stellwag Date: Wed, 3 Oct 2018 14:47:17 +0200 Subject: [PATCH 4/8] Asserts: add type hint Signed-off-by: Bernd Stellwag --- tests/unit/Codeception/Module/AssertsTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/Codeception/Module/AssertsTest.php b/tests/unit/Codeception/Module/AssertsTest.php index 266bdadd23..e529417970 100644 --- a/tests/unit/Codeception/Module/AssertsTest.php +++ b/tests/unit/Codeception/Module/AssertsTest.php @@ -1,6 +1,7 @@ Date: Wed, 3 Oct 2018 14:51:54 +0200 Subject: [PATCH 5/8] Asserts: since we marked expectedException as deprecated, use expectThrowable instead Signed-off-by: Bernd Stellwag --- tests/data/claypit/tests/powers/PowerUpCest.php | 4 ++-- tests/data/snapshots/tests/SnapshotDataCest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/data/claypit/tests/powers/PowerUpCest.php b/tests/data/claypit/tests/powers/PowerUpCest.php index 13c29eac3e..6c6f96bbb7 100644 --- a/tests/data/claypit/tests/powers/PowerUpCest.php +++ b/tests/data/claypit/tests/powers/PowerUpCest.php @@ -4,7 +4,7 @@ class PowerUpCest { public function iHaveNoPower(PowerGuy $I) { - $I->expectException('Exception', function() use ($I) { + $I->expectThrowable('Exception', function() use ($I) { $I->gotThePower(); }); } @@ -22,4 +22,4 @@ protected function drinkBluePotion(\Codeception\Module\PowerHelper $helper) { $helper->_reconfigure(['has_power' => true]); } -} \ No newline at end of file +} diff --git a/tests/data/snapshots/tests/SnapshotDataCest.php b/tests/data/snapshots/tests/SnapshotDataCest.php index ed99898562..a98ae74058 100644 --- a/tests/data/snapshots/tests/SnapshotDataCest.php +++ b/tests/data/snapshots/tests/SnapshotDataCest.php @@ -18,7 +18,7 @@ public function loadSnapshotAndSkipRefresh(DataTester $I, UserSnapshot $snapshot ]); $snapshot->shouldRefreshSnapshot(false); - $I->expectException(\PHPUnit\Framework\AssertionFailedError::class, function() use ($snapshot) { + $I->expectThrowable(\PHPUnit\Framework\AssertionFailedError::class, function() use ($snapshot) { $snapshot->assert(); }); } From 0d36a734be56fe370ae65c62b5f64145ae5cda6a Mon Sep 17 00:00:00 2001 From: Bernd Stellwag Date: Wed, 3 Oct 2018 16:13:10 +0200 Subject: [PATCH 6/8] fix typos Signed-off-by: Bernd Stellwag --- tests/unit/Codeception/Module/AssertsTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/Codeception/Module/AssertsTest.php b/tests/unit/Codeception/Module/AssertsTest.php index e529417970..38b411ffdb 100644 --- a/tests/unit/Codeception/Module/AssertsTest.php +++ b/tests/unit/Codeception/Module/AssertsTest.php @@ -75,7 +75,7 @@ public function testOutputExceptionTimeWhenNothingCaught() }); } - public function testExceptThrowable() + public function testExpectThrowable() { $this->module->expectThrowable('Exception', function () { throw new Exception(); @@ -101,7 +101,7 @@ public function testExpectThrowableFailOnDifferentClass() /** * @expectedException PHPUnit\Framework\AssertionFailedError */ - public function testExceptThrowableFailOnDifferentMessage() + public function testExpectThrowableFailOnDifferentMessage() { $this->module->expectThrowable(new Exception('foo', 200), function () { throw new Exception('bar', 200); @@ -111,7 +111,7 @@ public function testExceptThrowableFailOnDifferentMessage() /** * @expectedException PHPUnit\Framework\AssertionFailedError */ - public function testExceptThrowableFailOnDifferentCode() + public function testExpectThrowableFailOnDifferentCode() { $this->module->expectThrowable(new Exception('foobar', 200), function () { throw new Exception('foobar', 2); From 67731656b6142f1d6e4e7c6ca33d5532072be0f0 Mon Sep 17 00:00:00 2001 From: Bernd Stellwag Date: Wed, 3 Oct 2018 16:18:40 +0200 Subject: [PATCH 7/8] Asserts: try to make expectThrowable compatible with PHP 5.6 again Signed-off-by: Bernd Stellwag --- src/Codeception/Module/Asserts.php | 65 ++++++++++++------- tests/unit/Codeception/Module/AssertsTest.php | 8 +-- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/Codeception/Module/Asserts.php b/src/Codeception/Module/Asserts.php index a650818d30..46874597d4 100644 --- a/src/Codeception/Module/Asserts.php +++ b/src/Codeception/Module/Asserts.php @@ -504,32 +504,51 @@ public function expectThrowable($throwable, $callback) try { $callback(); + } catch (\Exception $t) { + $this->checkThrowable($t, $class, $msg, $code); + + return; } catch (\Throwable $t) { - if (!($t instanceof $class)) { - $this->fail(sprintf( - "Throwable of class '$class' expected to be thrown, but class '%s' was caught", - get_class($t) - )); - } - - if (null !== $msg && $t->getMessage() !== $msg) { - $this->fail(sprintf( - "Throwable of class '$class' expected to have message '$msg', but actual message was '%s'", - $t->getMessage() - )); - } - - if (null !== $code && $t->getCode() !== $code) { - $this->fail(sprintf( - "Throwable of class '$class' expected to have code '$code', but actual code was '%s'", - $t->getCode() - )); - } - - $this->assertTrue(true); // increment assertion counter + $this->checkThrowable($t, $class, $msg, $code); return; } - $this->fail("Expected throwable of class '$class'' to be thrown, but nothing was caught"); + + $this->fail("Expected throwable of class '$class' to be thrown, but nothing was caught"); + } + + /** + * Check if the given throwable matches the expected data, + * fail (throws an exception) if it does not. + * + * @param \Throwable $throwable + * @param string $expectedClass + * @param string $expectedMsg + * @param int $expectedCode + */ + protected function checkThrowable($throwable, $expectedClass, $expectedMsg, $expectedCode) + { + if (!($throwable instanceof $expectedClass)) { + $this->fail(sprintf( + "Exception of class '$expectedClass' expected to be thrown, but class '%s' was caught", + get_class($throwable) + )); + } + + if (null !== $expectedMsg && $throwable->getMessage() !== $expectedMsg) { + $this->fail(sprintf( + "Exception of class '$expectedClass' expected to have message '$expectedMsg', but actual message was '%s'", + $throwable->getMessage() + )); + } + + if (null !== $expectedCode && $throwable->getCode() !== $expectedCode) { + $this->fail(sprintf( + "Exception of class '$expectedClass' expected to have code '$expectedCode', but actual code was '%s'", + $throwable->getCode() + )); + } + + $this->assertTrue(true); // increment assertion counter } } diff --git a/tests/unit/Codeception/Module/AssertsTest.php b/tests/unit/Codeception/Module/AssertsTest.php index 38b411ffdb..195cea1f63 100644 --- a/tests/unit/Codeception/Module/AssertsTest.php +++ b/tests/unit/Codeception/Module/AssertsTest.php @@ -80,8 +80,8 @@ public function testExpectThrowable() $this->module->expectThrowable('Exception', function () { throw new Exception(); }); - $this->module->expectThrowable(new Error('here'), function () { - throw new Error('here'); + $this->module->expectThrowable(new Exception('here'), function () { + throw new Exception('here'); }); $this->module->expectThrowable(new Exception('here', 200), function () { throw new Exception('here', 200); @@ -93,8 +93,8 @@ public function testExpectThrowable() */ public function testExpectThrowableFailOnDifferentClass() { - $this->module->expectThrowable(new Exception(), function () { - throw new Error(); + $this->module->expectThrowable(new RuntimeException(), function () { + throw new Exception(); }); } From f8a2b5289cc907bb095ee0be889d1fa86ef086af Mon Sep 17 00:00:00 2001 From: Bernd Stellwag Date: Wed, 17 Oct 2018 18:17:15 +0200 Subject: [PATCH 8/8] don't use @expectedException annotation as it will get deprecated see https://github.com/sebastianbergmann/phpunit/issues/3332 --- tests/unit/Codeception/Module/AssertsTest.php | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/tests/unit/Codeception/Module/AssertsTest.php b/tests/unit/Codeception/Module/AssertsTest.php index 195cea1f63..3c4e20ba9d 100644 --- a/tests/unit/Codeception/Module/AssertsTest.php +++ b/tests/unit/Codeception/Module/AssertsTest.php @@ -55,22 +55,20 @@ public function testExceptions() }); } - /** - * @expectedException PHPUnit\Framework\AssertionFailedError - */ public function testExceptionFails() { + $this->expectException(PHPUnit\Framework\AssertionFailedError::class); + $this->module->expectException(new Exception('here', 200), function () { throw new Exception('here', 2); }); } - /** - * @expectedException PHPUnit\Framework\AssertionFailedError - * @expectedExceptionMessageRegExp /RuntimeException/ - */ public function testOutputExceptionTimeWhenNothingCaught() { + $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); + $this->expectExceptionMessageRegExp('/RuntimeException/'); + $this->module->expectException(RuntimeException::class, function () { }); } @@ -88,42 +86,38 @@ public function testExpectThrowable() }); } - /** - * @expectedException PHPUnit\Framework\AssertionFailedError - */ public function testExpectThrowableFailOnDifferentClass() { + $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); + $this->module->expectThrowable(new RuntimeException(), function () { throw new Exception(); }); } - /** - * @expectedException PHPUnit\Framework\AssertionFailedError - */ public function testExpectThrowableFailOnDifferentMessage() { + $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); + $this->module->expectThrowable(new Exception('foo', 200), function () { throw new Exception('bar', 200); }); } - /** - * @expectedException PHPUnit\Framework\AssertionFailedError - */ public function testExpectThrowableFailOnDifferentCode() { + $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); + $this->module->expectThrowable(new Exception('foobar', 200), function () { throw new Exception('foobar', 2); }); } - /** - * @expectedException PHPUnit\Framework\AssertionFailedError - * @expectedExceptionMessageRegExp /RuntimeException/ - */ public function testExpectThrowableFailOnNothingCaught() { + $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); + $this->expectExceptionMessageRegExp('/RuntimeException/'); + $this->module->expectThrowable(RuntimeException::class, function () { }); }