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
|
|
|
|