1
1
<?php
2
2
3
+ declare (strict_types=1 );
4
+
3
5
namespace Codeception \Module ;
4
6
5
- use Codeception \ Lib \ Notification ;
7
+ use Throwable ;
6
8
7
9
/**
8
10
* Special module for using asserts in your tests.
9
11
*/
10
12
class Asserts extends AbstractAsserts
11
13
{
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
-
45
14
/**
46
15
* Handles and checks throwables (Exceptions/Errors) called inside the callback function.
47
16
* Either throwable class name or throwable instance should be provided.
@@ -65,10 +34,9 @@ public function expectException($exception, $callback)
65
34
* });
66
35
* ```
67
36
*
68
- * @param \Throwable|string $throwable
69
- * @param callable $callback
37
+ * @param Throwable|string $throwable
70
38
*/
71
- public function expectThrowable ($ throwable , $ callback )
39
+ public function expectThrowable ($ throwable , callable $ callback ): void
72
40
{
73
41
if (is_object ($ throwable )) {
74
42
$ class = get_class ($ throwable );
@@ -82,45 +50,42 @@ public function expectThrowable($throwable, $callback)
82
50
83
51
try {
84
52
$ callback ();
85
- } catch (\Exception $ t ) {
86
- $ this ->checkThrowable ($ t , $ class , $ msg , $ code );
87
- return ;
88
- } catch (\Throwable $ t ) {
53
+ } catch (Throwable $ t ) {
89
54
$ this ->checkThrowable ($ t , $ class , $ msg , $ code );
90
55
return ;
91
56
}
92
57
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 " );
94
59
}
95
60
96
61
/**
97
62
* Check if the given throwable matches the expected data,
98
63
* 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
104
64
*/
105
- protected function checkThrowable ($ throwable , $ expectedClass , $ expectedMsg , $ expectedCode )
65
+ protected function checkThrowable (Throwable $ throwable , string $ expectedClass , ? string $ expectedMsg , ? int $ expectedCode ): void
106
66
{
107
67
if (!($ throwable instanceof $ expectedClass )) {
108
68
$ 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 ,
110
71
get_class ($ throwable )
111
72
));
112
73
}
113
74
114
75
if (null !== $ expectedMsg && $ throwable ->getMessage () !== $ expectedMsg ) {
115
76
$ 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 ,
117
80
$ throwable ->getMessage ()
118
81
));
119
82
}
120
83
121
84
if (null !== $ expectedCode && $ throwable ->getCode () !== $ expectedCode ) {
122
85
$ 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 ,
124
89
$ throwable ->getCode ()
125
90
));
126
91
}
0 commit comments