| 148 |
lars |
1 |
<?php
|
|
|
2 |
namespace Hamcrest;
|
|
|
3 |
|
|
|
4 |
use PHPUnit\Framework\TestCase;
|
|
|
5 |
|
|
|
6 |
class MatcherAssertTest extends TestCase
|
|
|
7 |
{
|
|
|
8 |
|
|
|
9 |
protected function setUp()
|
|
|
10 |
{
|
|
|
11 |
\Hamcrest\MatcherAssert::resetCount();
|
|
|
12 |
}
|
|
|
13 |
|
|
|
14 |
public function testResetCount()
|
|
|
15 |
{
|
|
|
16 |
\Hamcrest\MatcherAssert::assertThat(true);
|
|
|
17 |
self::assertEquals(1, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
|
|
|
18 |
\Hamcrest\MatcherAssert::resetCount();
|
|
|
19 |
self::assertEquals(0, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
public function testAssertThatWithTrueArgPasses()
|
|
|
23 |
{
|
|
|
24 |
\Hamcrest\MatcherAssert::assertThat(true);
|
|
|
25 |
\Hamcrest\MatcherAssert::assertThat('non-empty');
|
|
|
26 |
\Hamcrest\MatcherAssert::assertThat(1);
|
|
|
27 |
\Hamcrest\MatcherAssert::assertThat(3.14159);
|
|
|
28 |
\Hamcrest\MatcherAssert::assertThat(array(true));
|
|
|
29 |
self::assertEquals(5, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
public function testAssertThatWithFalseArgFails()
|
|
|
33 |
{
|
|
|
34 |
try {
|
|
|
35 |
\Hamcrest\MatcherAssert::assertThat(false);
|
|
|
36 |
self::fail('expected assertion failure');
|
|
|
37 |
} catch (\Hamcrest\AssertionError $ex) {
|
|
|
38 |
self::assertEquals('', $ex->getMessage());
|
|
|
39 |
}
|
|
|
40 |
try {
|
|
|
41 |
\Hamcrest\MatcherAssert::assertThat(null);
|
|
|
42 |
self::fail('expected assertion failure');
|
|
|
43 |
} catch (\Hamcrest\AssertionError $ex) {
|
|
|
44 |
self::assertEquals('', $ex->getMessage());
|
|
|
45 |
}
|
|
|
46 |
try {
|
|
|
47 |
\Hamcrest\MatcherAssert::assertThat('');
|
|
|
48 |
self::fail('expected assertion failure');
|
|
|
49 |
} catch (\Hamcrest\AssertionError $ex) {
|
|
|
50 |
self::assertEquals('', $ex->getMessage());
|
|
|
51 |
}
|
|
|
52 |
try {
|
|
|
53 |
\Hamcrest\MatcherAssert::assertThat(0);
|
|
|
54 |
self::fail('expected assertion failure');
|
|
|
55 |
} catch (\Hamcrest\AssertionError $ex) {
|
|
|
56 |
self::assertEquals('', $ex->getMessage());
|
|
|
57 |
}
|
|
|
58 |
try {
|
|
|
59 |
\Hamcrest\MatcherAssert::assertThat(0.0);
|
|
|
60 |
self::fail('expected assertion failure');
|
|
|
61 |
} catch (\Hamcrest\AssertionError $ex) {
|
|
|
62 |
self::assertEquals('', $ex->getMessage());
|
|
|
63 |
}
|
|
|
64 |
try {
|
|
|
65 |
\Hamcrest\MatcherAssert::assertThat(array());
|
|
|
66 |
self::fail('expected assertion failure');
|
|
|
67 |
} catch (\Hamcrest\AssertionError $ex) {
|
|
|
68 |
self::assertEquals('', $ex->getMessage());
|
|
|
69 |
}
|
|
|
70 |
self::assertEquals(6, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
public function testAssertThatWithIdentifierAndTrueArgPasses()
|
|
|
74 |
{
|
|
|
75 |
\Hamcrest\MatcherAssert::assertThat('identifier', true);
|
|
|
76 |
\Hamcrest\MatcherAssert::assertThat('identifier', 'non-empty');
|
|
|
77 |
\Hamcrest\MatcherAssert::assertThat('identifier', 1);
|
|
|
78 |
\Hamcrest\MatcherAssert::assertThat('identifier', 3.14159);
|
|
|
79 |
\Hamcrest\MatcherAssert::assertThat('identifier', array(true));
|
|
|
80 |
self::assertEquals(5, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
public function testAssertThatWithIdentifierAndFalseArgFails()
|
|
|
84 |
{
|
|
|
85 |
try {
|
|
|
86 |
\Hamcrest\MatcherAssert::assertThat('identifier', false);
|
|
|
87 |
self::fail('expected assertion failure');
|
|
|
88 |
} catch (\Hamcrest\AssertionError $ex) {
|
|
|
89 |
self::assertEquals('identifier', $ex->getMessage());
|
|
|
90 |
}
|
|
|
91 |
try {
|
|
|
92 |
\Hamcrest\MatcherAssert::assertThat('identifier', null);
|
|
|
93 |
self::fail('expected assertion failure');
|
|
|
94 |
} catch (\Hamcrest\AssertionError $ex) {
|
|
|
95 |
self::assertEquals('identifier', $ex->getMessage());
|
|
|
96 |
}
|
|
|
97 |
try {
|
|
|
98 |
\Hamcrest\MatcherAssert::assertThat('identifier', '');
|
|
|
99 |
self::fail('expected assertion failure');
|
|
|
100 |
} catch (\Hamcrest\AssertionError $ex) {
|
|
|
101 |
self::assertEquals('identifier', $ex->getMessage());
|
|
|
102 |
}
|
|
|
103 |
try {
|
|
|
104 |
\Hamcrest\MatcherAssert::assertThat('identifier', 0);
|
|
|
105 |
self::fail('expected assertion failure');
|
|
|
106 |
} catch (\Hamcrest\AssertionError $ex) {
|
|
|
107 |
self::assertEquals('identifier', $ex->getMessage());
|
|
|
108 |
}
|
|
|
109 |
try {
|
|
|
110 |
\Hamcrest\MatcherAssert::assertThat('identifier', 0.0);
|
|
|
111 |
self::fail('expected assertion failure');
|
|
|
112 |
} catch (\Hamcrest\AssertionError $ex) {
|
|
|
113 |
self::assertEquals('identifier', $ex->getMessage());
|
|
|
114 |
}
|
|
|
115 |
try {
|
|
|
116 |
\Hamcrest\MatcherAssert::assertThat('identifier', array());
|
|
|
117 |
self::fail('expected assertion failure');
|
|
|
118 |
} catch (\Hamcrest\AssertionError $ex) {
|
|
|
119 |
self::assertEquals('identifier', $ex->getMessage());
|
|
|
120 |
}
|
|
|
121 |
self::assertEquals(6, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
public function testAssertThatWithActualValueAndMatcherArgsThatMatchPasses()
|
|
|
125 |
{
|
|
|
126 |
\Hamcrest\MatcherAssert::assertThat(true, is(true));
|
|
|
127 |
self::assertEquals(1, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
public function testAssertThatWithActualValueAndMatcherArgsThatDontMatchFails()
|
|
|
131 |
{
|
|
|
132 |
$expected = 'expected';
|
|
|
133 |
$actual = 'actual';
|
|
|
134 |
|
|
|
135 |
$expectedMessage =
|
|
|
136 |
'Expected: "expected"' . PHP_EOL .
|
|
|
137 |
' but: was "actual"';
|
|
|
138 |
|
|
|
139 |
try {
|
|
|
140 |
\Hamcrest\MatcherAssert::assertThat($actual, equalTo($expected));
|
|
|
141 |
self::fail('expected assertion failure');
|
|
|
142 |
} catch (\Hamcrest\AssertionError $ex) {
|
|
|
143 |
self::assertEquals($expectedMessage, $ex->getMessage());
|
|
|
144 |
self::assertEquals(1, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
public function testAssertThatWithIdentifierAndActualValueAndMatcherArgsThatMatchPasses()
|
|
|
149 |
{
|
|
|
150 |
\Hamcrest\MatcherAssert::assertThat('identifier', true, is(true));
|
|
|
151 |
self::assertEquals(1, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
public function testAssertThatWithIdentifierAndActualValueAndMatcherArgsThatDontMatchFails()
|
|
|
155 |
{
|
|
|
156 |
$expected = 'expected';
|
|
|
157 |
$actual = 'actual';
|
|
|
158 |
|
|
|
159 |
$expectedMessage =
|
|
|
160 |
'identifier' . PHP_EOL .
|
|
|
161 |
'Expected: "expected"' . PHP_EOL .
|
|
|
162 |
' but: was "actual"';
|
|
|
163 |
|
|
|
164 |
try {
|
|
|
165 |
\Hamcrest\MatcherAssert::assertThat('identifier', $actual, equalTo($expected));
|
|
|
166 |
self::fail('expected assertion failure');
|
|
|
167 |
} catch (\Hamcrest\AssertionError $ex) {
|
|
|
168 |
self::assertEquals($expectedMessage, $ex->getMessage());
|
|
|
169 |
self::assertEquals(1, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
public function testAssertThatWithNoArgsThrowsErrorAndDoesntIncrementCount()
|
|
|
174 |
{
|
|
|
175 |
try {
|
|
|
176 |
\Hamcrest\MatcherAssert::assertThat();
|
|
|
177 |
self::fail('expected invalid argument exception');
|
|
|
178 |
} catch (\InvalidArgumentException $ex) {
|
|
|
179 |
self::assertEquals(0, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
public function testAssertThatWithFourArgsThrowsErrorAndDoesntIncrementCount()
|
|
|
184 |
{
|
|
|
185 |
try {
|
|
|
186 |
\Hamcrest\MatcherAssert::assertThat(1, 2, 3, 4);
|
|
|
187 |
self::fail('expected invalid argument exception');
|
|
|
188 |
} catch (\InvalidArgumentException $ex) {
|
|
|
189 |
self::assertEquals(0, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
}
|