Repository navigation
Expand file tree
/
Copy pathLazyFunctionAssertionTest.php
More file actions
153 lines (139 loc) · 5.09 KB
/
Copy pathLazyFunctionAssertionTest.php
File metadata and controls
153 lines (139 loc) · 5.09 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Assert
*
* LICENSE
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@beberlei.de so I can send you a copy immediately.
*/
namespace Assert\Tests;
use Assert\LazyAssertion;
use Assert\LazyAssertionException;
use Assert\Tests\Fixtures\CustomLazyAssertionException;
use PHPUnit\Framework\TestCase;
class LazyFunctionAssertionTest extends TestCase
{
public function testThatLazyAssertionsCollectsAllErrorsUntilAssertAll()
{
$this->expectException('Assert\LazyAssertionException');
$this->expectExceptionMessage('The following 3 assertions failed:');
\Assert\lazy()
->that(10, 'foo')->string()
->that(null, 'bar')->notEmpty()
->that('string', 'baz')->isArray()
->verifyNow();
}
public function testThatLazyAssertionsSkipsAssertionsOfCurrentChainAfterFailure()
{
$this->expectException('Assert\LazyAssertionException');
$this->expectExceptionMessage('The following 1 assertions failed:');
\Assert\lazy()
->that(null, 'foo')->notEmpty()->string()
->verifyNow();
}
public function testLazyAssertionExceptionCanReturnAllErrors()
{
try {
\Assert\lazy()
->that(10, 'foo')->string()
->that(null, 'bar')->notEmpty()
->that('string', 'baz')->isArray()
->verifyNow();
} catch (LazyAssertionException $ex) {
self::assertEquals(
[
'Value "10" expected to be string, type integer given.',
'Value "<NULL>" is empty, but non empty value was expected.',
'Value "string" is not an array.',
],
\array_map(
function (\Exception $ex) {
return $ex->getMessage();
},
$ex->getErrorExceptions()
)
);
}
}
public function testVerifyNowReturnsTrueIfAssertionsPass()
{
$this->assertTrue(
\Assert\lazy()
->that(2, 'Two')->eq(2)
->verifyNow()
);
}
public function testRestOfChainNotSkippedWhenTryAllUsed()
{
try {
\Assert\lazy()
->that(9.9, 'foo')->tryAll()->integer('must be int')->between(10, 20, 'must be between')
->verifyNow();
} catch (LazyAssertionException $ex) {
$this->assertEquals(
[
'must be int',
'must be between',
],
\array_map(
function (\Exception $ex) {
return $ex->getMessage();
},
$ex->getErrorExceptions()
)
);
}
}
public function testCallsToThatFollowingTryAllSkipAssertionsAfterFailure()
{
$this->expectException('Assert\LazyAssertionException');
$this->expectExceptionMessage('The following 1 assertions failed:');
\Assert\lazy()
->that(10, 'foo')->tryAll()->integer()
->that(null, 'foo')->notEmpty()->string()
->verifyNow();
}
public function testCallsToThatWithTryAllWithMultipleAssertionsAllGetReported()
{
$this->expectException('Assert\LazyAssertionException');
$this->expectExceptionMessage('The following 4 assertions failed:');
\Assert\lazy()
->that(10, 'foo')->tryAll()->float()->greaterThan(100)
->that(null, 'foo')->tryAll()->notEmpty()->string()
->verifyNow();
}
public function testCallsToTryAllOnLazyAlwaysReportAllGetReported()
{
$this->expectException('Assert\LazyAssertionException');
$this->expectExceptionMessage('The following 4 assertions failed:');
\Assert\lazy()->tryAll()
->that(10, 'foo')->float()->greaterThan(100)
->that(null, 'foo')->notEmpty()->string()
->verifyNow();
}
public function testThatLazyAssertionThrowsCustomExceptionWhenSet()
{
$this->expectException('Assert\Tests\Fixtures\CustomLazyAssertionException');
$this->expectExceptionMessage('The following 1 assertions failed:');
$lazyAssertion = new LazyAssertion();
$lazyAssertion->setExceptionClass(CustomLazyAssertionException::class);
\var_dump(
$lazyAssertion
->that('foo', 'property')->integer()
->verifyNow()
);
}
public function testLazyAssertionExceptionExtendsInvalidArgumentException()
{
$this->expectException('Assert\InvalidArgumentException');
$this->expectExceptionMessage('The following 4 assertions failed:');
\Assert\lazy()->tryAll()
->that(10, 'foo')->float()->greaterThan(100)
->that(null, 'foo')->notEmpty()->string()
->verifyNow();
}
}