-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathAssert.exception.phpt
85 lines (71 loc) · 1.67 KB
/
Assert.exception.phpt
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
<?php
declare(strict_types=1);
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
$e = Assert::exception(
fn() => throw new Exception,
Exception::class,
);
Assert::true($e instanceof Exception);
Assert::exception(
fn() => throw new Exception('Text 123'),
Exception::class,
'Text %d%',
);
Assert::exception(
fn() => eval('*'),
Error::class,
'syntax error%a?%',
);
Assert::exception(
fn() => Assert::exception(
fn() => null,
Exception::class,
),
Tester\AssertException::class,
'Exception was expected, but none was thrown',
);
$obj = new stdClass;
$e = Assert::exception(
fn() => Assert::exception(
fn() => throw $obj->e = new Exception('message'),
'UnknownException',
),
Tester\AssertException::class,
'UnknownException was expected but got Exception (message)',
);
Assert::same($obj->e, $e->getPrevious());
$obj = new stdClass;
$e = Assert::exception(
fn() => Assert::exception(
fn() => throw $obj->e = new Exception('Text'),
Exception::class,
'Abc',
),
Tester\AssertException::class,
"Exception with a message matching 'Abc' was expected but got 'Text'",
);
Assert::same($obj->e, $e->getPrevious());
Assert::exception(
fn() => throw new Exception('Text', 42),
Exception::class,
null,
42,
);
$obj = new stdClass;
$e = Assert::exception(
fn() => Assert::exception(
fn() => throw $obj->e = new Exception('Text', 1),
Exception::class,
null,
42,
),
Tester\AssertException::class,
'Exception with a code 42 was expected but got 1',
);
Assert::same($obj->e, $e->getPrevious());
$old = Assert::$onFailure;
Assert::$onFailure = function () {};
$e = Assert::exception(function () {}, Exception::class);
Assert::$onFailure = $old;
Assert::null($e);