1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Assert |
4
|
|
|
* |
5
|
|
|
* LICENSE |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this package in the file LICENSE.txt. |
9
|
|
|
* If you did not receive a copy of the license and are unable to |
10
|
|
|
* obtain it through the world-wide-web, please send an email |
11
|
|
|
* to [email protected] so I can send you a copy immediately. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Assert\Tests; |
15
|
|
|
|
16
|
|
|
use Assert\Assert; |
17
|
|
|
use Assert\InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
class CustomAssertionClassTest extends \PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
protected function setUp() |
22
|
|
|
{ |
23
|
|
|
CustomAssertion::clearCalls(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @test |
28
|
|
|
*/ |
29
|
|
|
public function it_uses_custom_exception_class() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$this->setExpectedException('Assert\Tests\CustomException'); |
32
|
|
|
CustomAssertion::integer('foo'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @test |
37
|
|
|
*/ |
38
|
|
|
public function it_uses_custom_assertion_class_for_assertion_chains() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$string = 's' . uniqid(); |
41
|
|
|
CustomAssert::that($string)->string(); |
42
|
|
|
$this->assertSame(array(array('string', $string)), CustomAssertion::getCalls()); |
43
|
|
|
|
44
|
|
|
$this->setExpectedException('Assert\Tests\CustomException'); |
45
|
|
|
CustomAssert::that($string)->integer(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @test |
50
|
|
|
*/ |
51
|
|
|
public function it_uses_custom_exception_for_lazy_assertion_chains() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this->setExpectedException('Assert\Tests\CustomLazyAssertionException'); |
54
|
|
|
CustomAssert::lazy() |
55
|
|
|
->that('foo', 'foo')->integer() |
56
|
|
|
->verifyNow() |
57
|
|
|
; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @test |
62
|
|
|
*/ |
63
|
|
|
public function it_uses_custom_exception_for_lazy_assertion_chains_when_first_assertion_does_not_fail() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this->setExpectedException('Assert\Tests\CustomLazyAssertionException'); |
66
|
|
|
CustomAssert::lazy() |
67
|
|
|
->that('foo', 'foo')->string() |
68
|
|
|
->that('bar', 'bar')->integer() |
69
|
|
|
->verifyNow() |
70
|
|
|
; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @test |
75
|
|
|
*/ |
76
|
|
|
public function it_uses_custom_exception_for_lazy_assertion_chains_that_try_all_assertions_per_chain() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$this->setExpectedException('Assert\Tests\CustomLazyAssertionException', <<< MESSAGE |
79
|
|
|
The following 4 assertions failed: |
80
|
|
|
1) foo: Value "foo" is not an integer. |
81
|
|
|
2) foo: Value "foo" is not an array. |
82
|
|
|
3) bar: Value "123" expected to be string, type integer given. |
83
|
|
|
4) bar: Value "123" is not an array. |
84
|
|
|
MESSAGE |
85
|
|
|
); |
86
|
|
|
CustomAssert::lazy() |
87
|
|
|
->that('foo', 'foo')->tryAll()->integer()->isArray() |
88
|
|
|
->that(123, 'bar')->tryAll()->string()->isArray() |
89
|
|
|
->verifyNow() |
90
|
|
|
; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @test |
95
|
|
|
*/ |
96
|
|
|
public function it_uses_custom_exception_for_lazy_assertion_chains_that_try_all_assertions() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$this->setExpectedException('Assert\Tests\CustomLazyAssertionException', <<< MESSAGE |
99
|
|
|
The following 4 assertions failed: |
100
|
|
|
1) foo: Value "foo" is not an integer. |
101
|
|
|
2) foo: Value "foo" is not an array. |
102
|
|
|
3) bar: Value "123" expected to be string, type integer given. |
103
|
|
|
4) bar: Value "123" is not an array. |
104
|
|
|
MESSAGE |
105
|
|
|
); |
106
|
|
|
CustomAssert::lazy() |
107
|
|
|
->tryAll() |
108
|
|
|
->that('foo', 'foo')->integer()->isArray() |
109
|
|
|
->that(123, 'bar')->string()->isArray() |
110
|
|
|
->verifyNow() |
111
|
|
|
; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
class CustomException extends InvalidArgumentException |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
class CustomAssert extends Assert |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
protected static $assertionClass = 'Assert\Tests\CustomAssertion'; |
122
|
|
|
protected static $lazyAssertionExceptionClass = 'Assert\Tests\CustomLazyAssertionException'; |
123
|
|
|
} |
124
|
|
|
|