-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAsserts.php
More file actions
100 lines (89 loc) · 2.95 KB
/
Asserts.php
File metadata and controls
100 lines (89 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types=1);
namespace Codeception\Module;
use Throwable;
use function get_debug_type;
/**
* Special module for using asserts in your tests.
*/
class Asserts extends AbstractAsserts
{
/**
* Handles and checks throwables (Exceptions/Errors) called inside the callback function.
* Either throwable class name or throwable instance should be provided.
*
* ```php
* <?php
* $I->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
* <?php
* // 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();
* });
* ```
*/
public function expectThrowable(string|Throwable $throwable, callable $callback): void
{
if (is_object($throwable)) {
$class = get_class($throwable);
$msg = $throwable->getMessage();
$code = (int) $throwable->getCode();
} else {
$class = $throwable;
$msg = null;
$code = null;
}
try {
$callback();
} catch (Throwable $t) {
$this->checkThrowable($t, $class, $msg, $code);
return;
}
$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.
*/
protected function checkThrowable(
Throwable $throwable,
string $expectedClass,
?string $expectedMsg,
int|null $expectedCode = null
): void {
if (!($throwable instanceof $expectedClass)) {
$this->fail(sprintf(
"Exception of class '%s' expected to be thrown, but class '%s' was caught",
$expectedClass,
get_debug_type($throwable)
));
}
if (null !== $expectedMsg && $throwable->getMessage() !== $expectedMsg) {
$this->fail(sprintf(
"Exception of class '%s' expected to have message '%s', but actual message was '%s'",
$expectedClass,
$expectedMsg,
$throwable->getMessage()
));
}
if (null !== $expectedCode && $throwable->getCode() !== $expectedCode) {
$this->fail(sprintf(
"Exception of class '%s' expected to have code '%s', but actual code was '%s'",
$expectedClass,
$expectedCode,
$throwable->getCode()
));
}
$this->assertTrue(true); // increment assertion counter
}
}