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 testRestOfChainNotSkippedWhenThatAllUsed() |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
|
|
\Assert\lazy() |
79
|
|
|
->thatAll(9.9, 'foo')->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 testCallsToThatFollowingThatAllSkipAssertionsAfterFailure() |
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
|
|
|
->thatAll(10, 'foo')->integer() |
102
|
|
|
->that(null, 'foo')->notEmpty()->string() |
103
|
|
|
->verifyNow(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|