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\AssertionChain; |
18
|
|
|
|
19
|
|
|
class AssertionChainTest extends \PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @test |
23
|
|
|
*/ |
24
|
|
|
public function it_chains_assertions() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
Assert::that(10)->notEmpty()->integer(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @test |
31
|
|
|
*/ |
32
|
|
|
public function it_shifts_arguments_to_assertions_by_one() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
Assert::that(10)->eq(10); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @test |
39
|
|
|
*/ |
40
|
|
|
public function it_knowns_default_error_message() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$this->setExpectedException('Assert\InvalidArgumentException', 'Not Null and such'); |
43
|
|
|
|
44
|
|
|
Assert::that(null, 'Not Null and such')->notEmpty(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @test |
49
|
|
|
*/ |
50
|
|
|
public function it_skips_assertions_on_valid_null() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
Assert::that(null)->nullOr()->integer()->eq(10); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @test |
57
|
|
|
*/ |
58
|
|
|
public function it_validates_all_inputs() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
Assert::that(array(1, 2, 3))->all()->integer(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @test |
65
|
|
|
*/ |
66
|
|
|
public function it_has_thatall_shortcut() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
Assert::thatAll(array(1, 2, 3))->integer(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
*/ |
74
|
|
|
public function it_has_nullor_shortcut() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
Assert::thatNullOr(null)->integer()->eq(10); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @expectedException \RuntimeException |
81
|
|
|
* @expectedExceptionMessage Assertion 'unknownAssertion' does not exist. |
82
|
|
|
* @test |
83
|
|
|
*/ |
84
|
|
|
public function it_throws_exception_for_unknown_assertion() |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
Assert::that(null)->unknownAssertion(); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @test |
91
|
|
|
*/ |
92
|
|
|
public function it_has_satisfy_shortcut() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
Assert::that(null)->satisfy(function ($value) { |
95
|
|
|
return is_null($value); |
96
|
|
|
}); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testThatCustomAssertionClassIsUsedWhenSet() |
100
|
|
|
{ |
101
|
|
|
$assertionChain = new AssertionChain('foo'); |
102
|
|
|
$assertionChain->setAssertionClassName('Assert\Tests\CustomAssertion'); |
103
|
|
|
|
104
|
|
|
CustomAssertion::clearCalls(); |
105
|
|
|
$message = uniqid(); |
106
|
|
|
$assertionChain->string($message); |
107
|
|
|
|
108
|
|
|
$this->assertSame(array(array('string', 'foo')), CustomAssertion::getCalls()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @dataProvider provideDataToTestThatSetAssertionClassNameWillNotAcceptInvalidAssertionClasses |
113
|
|
|
* @param $assertionClassName |
114
|
|
|
*/ |
115
|
|
|
public function testThatSetAssertionClassNameWillNotAcceptInvalidAssertionClasses($assertionClassName) |
116
|
|
|
{ |
117
|
|
|
$lazyAssertion = new AssertionChain('foo'); |
118
|
|
|
|
119
|
|
|
$this->setExpectedException('LogicException'); |
120
|
|
|
$lazyAssertion->setAssertionClassName($assertionClassName); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return array |
|
|
|
|
125
|
|
|
*/ |
126
|
|
|
public function provideDataToTestThatSetAssertionClassNameWillNotAcceptInvalidAssertionClasses() |
127
|
|
|
{ |
128
|
|
|
return array( |
129
|
|
|
'null' => array(null), |
130
|
|
|
'string' => array('foo'), |
131
|
|
|
'array' => array(array()), |
132
|
|
|
'object' => array(new \stdClass()), |
133
|
|
|
'other class' => array(__CLASS__), |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|