1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Assert\Tests; |
4
|
|
|
|
5
|
|
|
use Assert\Assert; |
6
|
|
|
use Assert\InvalidArgumentException; |
7
|
|
|
use Assert\LazyAssertion; |
8
|
|
|
use Assert\LazyAssertionException; |
9
|
|
|
|
10
|
|
|
class LazyAssertionTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @test |
14
|
|
|
*/ |
15
|
|
|
public function it_collects_errors_until_assertall() |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
$this->setExpectedException('Assert\LazyAssertionException', <<<EXC |
18
|
|
|
The following 3 assertions failed: |
19
|
|
|
1) foo: Value "10" expected to be string, type integer given. |
20
|
|
|
2) bar: Value "<NULL>" is empty, but non empty value was expected. |
21
|
|
|
3) baz: Value "string" is not an array. |
22
|
|
|
|
23
|
|
|
EXC |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
Assert::lazy() |
27
|
|
|
->that(10, 'foo')->string() |
28
|
|
|
->that(null, 'bar')->notEmpty() |
29
|
|
|
->that('string', 'baz')->isArray() |
30
|
|
|
->verifyNow(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @test |
35
|
|
|
*/ |
36
|
|
|
public function it_skips_assertions_of_current_chain_after_failure() |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$this->setExpectedException('Assert\LazyAssertionException', <<<EXC |
39
|
|
|
The following 1 assertions failed: |
40
|
|
|
1) foo: Value "<NULL>" is empty, but non empty value was expected. |
41
|
|
|
|
42
|
|
|
EXC |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
Assert::lazy() |
46
|
|
|
->that(null, 'foo')->notEmpty()->string() |
47
|
|
|
->verifyNow(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testLazyAssertionExceptionCanReturnAllErrors() |
51
|
|
|
{ |
52
|
|
|
try { |
53
|
|
|
Assert::lazy() |
54
|
|
|
->that(10, 'foo')->string() |
55
|
|
|
->that(null, 'bar')->notEmpty() |
56
|
|
|
->that('string', 'baz')->isArray() |
57
|
|
|
->verifyNow(); |
58
|
|
|
} catch (LazyAssertionException $ex) { |
59
|
|
|
self::assertEquals(array( |
60
|
|
|
'Value "10" expected to be string, type integer given.', |
61
|
|
|
'Value "<NULL>" is empty, but non empty value was expected.', |
62
|
|
|
'Value "string" is not an array.', |
63
|
|
|
), array_map(function (\Exception $ex) { |
64
|
|
|
return $ex->getMessage(); |
65
|
|
|
}, $ex->getErrorExceptions())); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testVerifyNowReturnsTrueIfAssertionsPass() |
70
|
|
|
{ |
71
|
|
|
$this->assertTrue( |
72
|
|
|
Assert::lazy() |
73
|
|
|
->that(2, 'Two')->eq(2) |
74
|
|
|
->verifyNow() |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testRestOfChainNotSkippedWhenTryAllUsed() |
79
|
|
|
{ |
80
|
|
|
try { |
81
|
|
|
Assert::lazy() |
82
|
|
|
->that(9.9, 'foo')->tryAll()->integer('must be int')->between(10, 20, 'must be between') |
83
|
|
|
->verifyNow(); |
84
|
|
|
} catch (LazyAssertionException $ex) { |
85
|
|
|
$this->assertEquals(array( |
86
|
|
|
'must be int', |
87
|
|
|
'must be between' |
88
|
|
|
), array_map(function (\Exception $ex) { |
89
|
|
|
return $ex->getMessage(); |
90
|
|
|
}, $ex->getErrorExceptions())); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testCallsToThatFollowingTryAllSkipAssertionsAfterFailure() |
95
|
|
|
{ |
96
|
|
|
$this->setExpectedException('Assert\LazyAssertionException', <<<EXC |
97
|
|
|
The following 1 assertions failed: |
98
|
|
|
1) foo: Value "<NULL>" is empty, but non empty value was expected. |
99
|
|
|
|
100
|
|
|
EXC |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
Assert::lazy() |
104
|
|
|
->that(10, 'foo')->tryAll()->integer() |
105
|
|
|
->that(null, 'foo')->notEmpty()->string() |
106
|
|
|
->verifyNow(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testCallsToThatWithTryAllWithMultipleAssertionsAllGetReported() |
110
|
|
|
{ |
111
|
|
|
$this->setExpectedException('\Assert\LazyAssertionException', <<<EXC |
112
|
|
|
The following 4 assertions failed: |
113
|
|
|
1) foo: Value "10" is not a float. |
114
|
|
|
2) foo: Provided "10" is not greater than "100". |
115
|
|
|
3) foo: Value "<NULL>" is empty, but non empty value was expected. |
116
|
|
|
4) foo: Value "<NULL>" expected to be string, type NULL given. |
117
|
|
|
|
118
|
|
|
EXC |
119
|
|
|
); |
120
|
|
|
Assert::lazy() |
121
|
|
|
->that(10, 'foo')->tryAll()->float()->greaterThan(100) |
122
|
|
|
->that(null, 'foo')->tryAll()->notEmpty()->string() |
123
|
|
|
->verifyNow(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testCallsToTryAllOnLazyAlwaysReportAllGetReported() |
127
|
|
|
{ |
128
|
|
|
$this->setExpectedException('\Assert\LazyAssertionException', <<<EXC |
129
|
|
|
The following 4 assertions failed: |
130
|
|
|
1) foo: Value "10" is not a float. |
131
|
|
|
2) foo: Provided "10" is not greater than "100". |
132
|
|
|
3) foo: Value "<NULL>" is empty, but non empty value was expected. |
133
|
|
|
4) foo: Value "<NULL>" expected to be string, type NULL given. |
134
|
|
|
|
135
|
|
|
EXC |
136
|
|
|
); |
137
|
|
|
Assert::lazy()->tryAll() |
138
|
|
|
->that(10, 'foo')->float()->greaterThan(100) |
139
|
|
|
->that(null, 'foo')->notEmpty()->string() |
140
|
|
|
->verifyNow(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testThatLazyAssertionThrowsCustomExceptionWhenSet() |
144
|
|
|
{ |
145
|
|
|
$lazyAssertion = new LazyAssertion(); |
146
|
|
|
$lazyAssertion->setExceptionClass('Assert\Tests\CustomLazyAssertionException'); |
147
|
|
|
|
148
|
|
|
$this->setExpectedException('Assert\Tests\CustomLazyAssertionException'); |
149
|
|
|
$lazyAssertion |
150
|
|
|
->that('foo', 'property')->integer() |
151
|
|
|
->verifyNow() |
152
|
|
|
; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @dataProvider provideDataToTestThatSetExceptionClassWillNotAcceptInvalidExceptionClasses |
157
|
|
|
* @param mixed $exceptionClass |
158
|
|
|
*/ |
159
|
|
|
public function testThatSetExceptionClassWillNotAcceptInvalidExceptionClasses($exceptionClass) |
160
|
|
|
{ |
161
|
|
|
$lazyAssertion = new LazyAssertion(); |
162
|
|
|
|
163
|
|
|
$this->setExpectedException('LogicException'); |
164
|
|
|
$lazyAssertion->setExceptionClass($exceptionClass); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return array |
|
|
|
|
169
|
|
|
*/ |
170
|
|
|
public function provideDataToTestThatSetExceptionClassWillNotAcceptInvalidExceptionClasses() |
171
|
|
|
{ |
172
|
|
|
return array( |
173
|
|
|
'null' => array(null), |
174
|
|
|
'string' => array('foo'), |
175
|
|
|
'array' => array(array()), |
176
|
|
|
'object' => array(new \stdClass()), |
177
|
|
|
'other class' => array(__CLASS__), |
178
|
|
|
'other exception' => array('Exception'), |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function testLazyAssertionExceptionExtendsInvalidArgumentException() |
183
|
|
|
{ |
184
|
|
|
$this->setExpectedException(InvalidArgumentException::class); |
185
|
|
|
|
186
|
|
|
Assert::lazy()->tryAll() |
187
|
|
|
->that(10, 'foo')->float()->greaterThan(100) |
188
|
|
|
->that(null, 'foo')->notEmpty()->string() |
189
|
|
|
->verifyNow(); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|