11<?php
22
3+ declare (strict_types=1 );
4+
35namespace Codeception \Module ;
46
5- use Codeception \ Lib \ Notification ;
7+ use Throwable ;
68
79/**
810 * Special module for using asserts in your tests.
911 */
1012class Asserts extends AbstractAsserts
1113{
12- /**
13- * Handles and checks exception called inside callback function.
14- * Either exception class name or exception instance should be provided.
15- *
16- * ```php
17- * <?php
18- * $I->expectException(MyException::class, function() {
19- * $this->doSomethingBad();
20- * });
21- *
22- * $I->expectException(new MyException(), function() {
23- * $this->doSomethingBad();
24- * });
25- * ```
26- * If you want to check message or exception code, you can pass them with exception instance:
27- * ```php
28- * <?php
29- * // will check that exception MyException is thrown with "Don't do bad things" message
30- * $I->expectException(new MyException("Don't do bad things"), function() {
31- * $this->doSomethingBad();
32- * });
33- * ```
34- *
35- * @deprecated Use expectThrowable() instead
36- * @param \Exception|string $exception
37- * @param callable $callback
38- */
39- public function expectException ($ exception , $ callback )
40- {
41- Notification::deprecate ('Use expectThrowable() instead ' );
42- $ this ->expectThrowable ($ exception , $ callback );
43- }
44-
4514 /**
4615 * Handles and checks throwables (Exceptions/Errors) called inside the callback function.
4716 * Either throwable class name or throwable instance should be provided.
@@ -65,10 +34,9 @@ public function expectException($exception, $callback)
6534 * });
6635 * ```
6736 *
68- * @param \Throwable|string $throwable
69- * @param callable $callback
37+ * @param Throwable|string $throwable
7038 */
71- public function expectThrowable ($ throwable , $ callback )
39+ public function expectThrowable ($ throwable , callable $ callback ): void
7240 {
7341 if (is_object ($ throwable )) {
7442 $ class = get_class ($ throwable );
@@ -82,45 +50,42 @@ public function expectThrowable($throwable, $callback)
8250
8351 try {
8452 $ callback ();
85- } catch (\Exception $ t ) {
86- $ this ->checkThrowable ($ t , $ class , $ msg , $ code );
87- return ;
88- } catch (\Throwable $ t ) {
53+ } catch (Throwable $ t ) {
8954 $ this ->checkThrowable ($ t , $ class , $ msg , $ code );
9055 return ;
9156 }
9257
93- $ this ->fail ("Expected throwable of class ' $ class' to be thrown, but nothing was caught " );
58+ $ this ->fail ("Expected throwable of class ' { $ class} ' to be thrown, but nothing was caught " );
9459 }
9560
9661 /**
9762 * Check if the given throwable matches the expected data,
9863 * fail (throws an exception) if it does not.
99- *
100- * @param \Throwable $throwable
101- * @param string $expectedClass
102- * @param string $expectedMsg
103- * @param int $expectedCode
10464 */
105- protected function checkThrowable ($ throwable , $ expectedClass , $ expectedMsg , $ expectedCode )
65+ protected function checkThrowable (Throwable $ throwable , string $ expectedClass , ? string $ expectedMsg , ? int $ expectedCode ): void
10666 {
10767 if (!($ throwable instanceof $ expectedClass )) {
10868 $ this ->fail (sprintf (
109- "Exception of class ' $ expectedClass' expected to be thrown, but class '%s' was caught " ,
69+ "Exception of class '%s' expected to be thrown, but class '%s' was caught " ,
70+ $ expectedClass ,
11071 get_class ($ throwable )
11172 ));
11273 }
11374
11475 if (null !== $ expectedMsg && $ throwable ->getMessage () !== $ expectedMsg ) {
11576 $ this ->fail (sprintf (
116- "Exception of class ' $ expectedClass' expected to have message ' $ expectedMsg', but actual message was '%s' " ,
77+ "Exception of class '%s' expected to have message '%s', but actual message was '%s' " ,
78+ $ expectedClass ,
79+ $ expectedMsg ,
11780 $ throwable ->getMessage ()
11881 ));
11982 }
12083
12184 if (null !== $ expectedCode && $ throwable ->getCode () !== $ expectedCode ) {
12285 $ this ->fail (sprintf (
123- "Exception of class ' $ expectedClass' expected to have code ' $ expectedCode', but actual code was '%s' " ,
86+ "Exception of class '%s' expected to have code '%s', but actual code was '%s' " ,
87+ $ expectedClass ,
88+ $ expectedCode ,
12489 $ throwable ->getCode ()
12590 ));
12691 }
0 commit comments