44
55namespace Codeception \Module ;
66
7+ use Throwable ;
8+
9+ use function get_debug_type ;
10+
711/**
812 * Special module for using asserts in your tests.
913 */
@@ -23,6 +27,7 @@ class Asserts extends AbstractAsserts
2327 * $this->doSomethingBad();
2428 * });
2529 * ```
30+ *
2631 * If you want to check message or throwable code, you can pass them with throwable instance:
2732 * ```php
2833 * <?php
@@ -31,15 +36,13 @@ class Asserts extends AbstractAsserts
3136 * $this->doSomethingBad();
3237 * });
3338 * ```
34- *
35- * @param \Throwable|string $throwable
3639 */
37- public function expectThrowable ($ throwable , callable $ callback ): void
40+ public function expectThrowable (string | Throwable $ throwable , callable $ callback ): void
3841 {
3942 if (is_object ($ throwable )) {
4043 $ class = get_class ($ throwable );
4144 $ msg = $ throwable ->getMessage ();
42- $ code = $ throwable ->getCode ();
45+ $ code = ( int ) $ throwable ->getCode ();
4346 } else {
4447 $ class = $ throwable ;
4548 $ msg = null ;
@@ -48,7 +51,7 @@ public function expectThrowable($throwable, callable $callback): void
4851
4952 try {
5053 $ callback ();
51- } catch (\ Throwable $ t ) {
54+ } catch (Throwable $ t ) {
5255 $ this ->checkThrowable ($ t , $ class , $ msg , $ code );
5356 return ;
5457 }
@@ -60,13 +63,17 @@ public function expectThrowable($throwable, callable $callback): void
6063 * Check if the given throwable matches the expected data,
6164 * fail (throws an exception) if it does not.
6265 */
63- protected function checkThrowable (\Throwable $ throwable , string $ expectedClass , ?string $ expectedMsg , $ expectedCode = null ): void
64- {
66+ protected function checkThrowable (
67+ Throwable $ throwable ,
68+ string $ expectedClass ,
69+ ?string $ expectedMsg ,
70+ int |null $ expectedCode = null
71+ ): void {
6572 if (!($ throwable instanceof $ expectedClass )) {
6673 $ this ->fail (sprintf (
6774 "Exception of class '%s' expected to be thrown, but class '%s' was caught " ,
6875 $ expectedClass ,
69- get_class ($ throwable )
76+ get_debug_type ($ throwable )
7077 ));
7178 }
7279
0 commit comments