1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Assert\Tests; |
4
|
|
|
|
5
|
|
|
use Assert\LazyAssertionException; |
6
|
|
|
|
7
|
|
|
class LazyAssertionTest extends \PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @test |
11
|
|
|
*/ |
12
|
|
|
public function it_collects_errors_until_assertall() |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
$this->setExpectedException('Assert\LazyAssertionException', <<<EXC |
15
|
|
|
The following 3 assertions failed: |
16
|
|
|
1) foo: Value "10" expected to be string, type integer given. |
17
|
|
|
2) bar: Value "<NULL>" is empty, but non empty value was expected. |
18
|
|
|
3) baz: Value "string" is not an array. |
19
|
|
|
|
20
|
|
|
EXC |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
\Assert\lazy() |
24
|
|
|
->that(10, 'foo')->string() |
25
|
|
|
->that(null, 'bar')->notEmpty() |
26
|
|
|
->that('string', 'baz')->isArray() |
27
|
|
|
->verifyNow(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @test |
32
|
|
|
*/ |
33
|
|
|
public function it_skips_assertions_of_current_chain_after_failure() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$this->setExpectedException('Assert\LazyAssertionException', <<<EXC |
36
|
|
|
The following 1 assertions failed: |
37
|
|
|
1) foo: Value "<NULL>" is empty, but non empty value was expected. |
38
|
|
|
|
39
|
|
|
EXC |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
\Assert\lazy() |
43
|
|
|
->that(null, 'foo')->notEmpty()->string() |
44
|
|
|
->verifyNow(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testLazyAssertionExceptionCanReturnAllErrors() |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
|
|
\Assert\lazy() |
51
|
|
|
->that(10, 'foo')->string() |
52
|
|
|
->that(null, 'bar')->notEmpty() |
53
|
|
|
->that('string', 'baz')->isArray() |
54
|
|
|
->verifyNow(); |
55
|
|
|
} catch (LazyAssertionException $ex) { |
56
|
|
|
self::assertEquals(array( |
57
|
|
|
'Value "10" expected to be string, type integer given.', |
58
|
|
|
'Value "<NULL>" is empty, but non empty value was expected.', |
59
|
|
|
'Value "string" is not an array.', |
60
|
|
|
), array_map(function (\Exception $ex) { |
61
|
|
|
return $ex->getMessage(); |
62
|
|
|
}, $ex->getErrorExceptions())); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testVerifyNowReturnsTrueIfAssertionsPass() |
67
|
|
|
{ |
68
|
|
|
$this->assertTrue( |
69
|
|
|
\Assert\lazy() |
70
|
|
|
->that(2, 'Two')->eq(2) |
71
|
|
|
->verifyNow() |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testRestOfChainNotSkippedWhenTryAllUsed() |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
|
|
\Assert\lazy() |
79
|
|
|
->that(9.9, 'foo')->tryAll()->integer('must be int')->between(10, 20, 'must be between') |
80
|
|
|
->verifyNow(); |
81
|
|
|
} catch (LazyAssertionException $ex) { |
82
|
|
|
$this->assertEquals(array( |
83
|
|
|
'must be int', |
84
|
|
|
'must be between' |
85
|
|
|
), array_map(function (\Exception $ex) { |
86
|
|
|
return $ex->getMessage(); |
87
|
|
|
}, $ex->getErrorExceptions())); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testCallsToThatFollowingTryAllSkipAssertionsAfterFailure() |
92
|
|
|
{ |
93
|
|
|
$this->setExpectedException('Assert\LazyAssertionException', <<<EXC |
94
|
|
|
The following 1 assertions failed: |
95
|
|
|
1) foo: Value "<NULL>" is empty, but non empty value was expected. |
96
|
|
|
|
97
|
|
|
EXC |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
\Assert\lazy() |
101
|
|
|
->that(10, 'foo')->tryAll()->integer() |
102
|
|
|
->that(null, 'foo')->notEmpty()->string() |
103
|
|
|
->verifyNow(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testCallsToThatWithTryAllWithMultipleAssertionsAllGetReported() |
107
|
|
|
{ |
108
|
|
|
$this->setExpectedException('\Assert\LazyAssertionException', <<<EXC |
109
|
|
|
The following 4 assertions failed: |
110
|
|
|
1) foo: Value "10" is not a float. |
111
|
|
|
2) foo: Provided "10" is not greater than "100". |
112
|
|
|
3) foo: Value "<NULL>" is empty, but non empty value was expected. |
113
|
|
|
4) foo: Value "<NULL>" expected to be string, type NULL given. |
114
|
|
|
|
115
|
|
|
EXC |
116
|
|
|
); |
117
|
|
|
\Assert\lazy() |
118
|
|
|
->that(10, 'foo')->tryAll()->float()->greaterThan(100) |
119
|
|
|
->that(null, 'foo')->tryAll()->notEmpty()->string() |
120
|
|
|
->verifyNow(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testCallsToTryAllOnLazyAlwaysReportAllGetReported() |
124
|
|
|
{ |
125
|
|
|
$this->setExpectedException('\Assert\LazyAssertionException', <<<EXC |
126
|
|
|
The following 4 assertions failed: |
127
|
|
|
1) foo: Value "10" is not a float. |
128
|
|
|
2) foo: Provided "10" is not greater than "100". |
129
|
|
|
3) foo: Value "<NULL>" is empty, but non empty value was expected. |
130
|
|
|
4) foo: Value "<NULL>" expected to be string, type NULL given. |
131
|
|
|
|
132
|
|
|
EXC |
133
|
|
|
); |
134
|
|
|
\Assert\lazy()->tryAll() |
135
|
|
|
->that(10, 'foo')->float()->greaterThan(100) |
136
|
|
|
->that(null, 'foo')->notEmpty()->string() |
137
|
|
|
->verifyNow(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|