Skip to content

Commit 9cf4acc

Browse files
committed
Merge pull request #7 from cmoralesweb/more-asserts
More asserts
2 parents aa9a7c5 + 8978f80 commit 9cf4acc

7 files changed

Lines changed: 431 additions & 3 deletions

File tree

src/Codeception/Verify.php

Lines changed: 227 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Verify {
77

88
protected $actual = null;
99
protected $description = '';
10+
protected $isFileExpectation = false;
1011

1112
public function __construct($description)
1213
{
@@ -19,16 +20,32 @@ public function __construct($description)
1920
$this->actual = $actual[1];
2021
$this->description = $description;
2122
}
22-
}
23+
}
24+
25+
/**
26+
* @param boolean $isFileExpectation
27+
*/
28+
public function setIsFileExpectation($isFileExpectation)
29+
{
30+
$this->isFileExpectation = $isFileExpectation;
31+
}
2332

2433
public function equals($expected)
2534
{
26-
a::assertEquals($expected, $this->actual, $this->description);
35+
if ( ! $this->isFileExpectation ) {
36+
a::assertEquals($expected, $this->actual, $this->description);
37+
} else {
38+
a::assertFileEquals($expected, $this->actual, $this->description);
39+
}
2740
}
2841

2942
public function notEquals($expected)
3043
{
31-
a::assertNotEquals($expected, $this->actual, $this->description);
44+
if ( ! $this->isFileExpectation ) {
45+
a::assertNotEquals($expected, $this->actual, $this->description);
46+
} else {
47+
a::assertFileNotEquals($expected, $this->actual, $this->description);
48+
}
3249
}
3350

3451
public function contains($needle)
@@ -101,5 +118,212 @@ public function hasntKey($key)
101118
a::assertArrayNotHasKey($key, $this->actual, $this->description);
102119
}
103120

121+
public function isInstanceOf($class)
122+
{
123+
a::assertInstanceOf($class, $this->actual, $this->description);
124+
}
125+
126+
public function isNotInstanceOf($class)
127+
{
128+
a::assertNotInstanceOf($class, $this->actual, $this->description);
129+
}
130+
131+
public function internalType($type)
132+
{
133+
a::assertInternalType($type, $this->actual, $this->description);
134+
}
135+
136+
public function notInternalType($type)
137+
{
138+
a::assertNotInternalType($type, $this->actual, $this->description);
139+
}
140+
141+
public function hasAttribute($attribute)
142+
{
143+
if ( is_string($attribute)) {
144+
a::assertClassHasAttribute($attribute, $this->actual, $this->description);
145+
} else {
146+
a::assertObjectHasAttribute($attribute, $this->actual, $this->description);
147+
}
148+
}
149+
150+
public function notHasAttribute($attribute)
151+
{
152+
if ( is_string($attribute)) {
153+
a::assertClassNotHasAttribute($attribute, $this->actual, $this->description);
154+
} else {
155+
a::assertObjectNotHasAttribute($attribute, $this->actual, $this->description);
156+
}
157+
}
158+
159+
public function hasStaticAttribute($attribute)
160+
{
161+
a::assertClassHasStaticAttribute($attribute, $this->actual, $this->description);
162+
}
163+
164+
public function notHasStaticAttribute($attribute)
165+
{
166+
a::assertClassNotHasStaticAttribute($attribute, $this->actual, $this->description);
167+
}
168+
169+
public function containsOnly($type, $isNativeType = NULL)
170+
{
171+
a::assertContainsOnly($type, $this->actual, $isNativeType, $this->description);
172+
}
173+
174+
public function notContainsOnly($type, $isNativeType = NULL)
175+
{
176+
a::assertNotContainsOnly($type, $this->actual, $isNativeType, $this->description);
177+
}
178+
179+
public function containsOnlyInstancesOf($class)
180+
{
181+
a::assertContainsOnlyInstancesOf($class, $this->actual, $this->description);
182+
}
183+
184+
public function count($array)
185+
{
186+
a::assertCount($array, $this->actual, $this->description);
187+
}
188+
189+
public function notCount($array)
190+
{
191+
a::assertNotCount($array, $this->actual, $this->description);
192+
}
193+
194+
public function equalXMLStructure($xml, $checkAttributes = FALSE)
195+
{
196+
a::assertEqualXMLStructure($xml, $this->actual, $checkAttributes, $this->description);
197+
}
198+
199+
public function exists()
200+
{
201+
if ( ! $this->isFileExpectation ) {
202+
throw new \Exception('exists() expectation should be called with expect_file()');
203+
}
204+
a::assertFileExists($this->actual, $this->description);
205+
}
206+
207+
public function notExists()
208+
{
209+
if ( ! $this->isFileExpectation ) {
210+
throw new \Exception('notExists() expectation should be called with expect_file()');
211+
}
212+
a::assertFileNotExists($this->actual, $this->description);
213+
}
214+
215+
public function equalsJsonFile($file)
216+
{
217+
if ( ! $this->isFileExpectation ) {
218+
a::assertJsonStringEqualsJsonFile($file, $this->actual, $this->description);
219+
} else {
220+
a::assertJsonFileEqualsJsonFile($file, $this->actual, $this->description);
221+
}
222+
}
223+
224+
public function equalsJsonString($string)
225+
{
226+
a::assertJsonStringEqualsJsonString($string, $this->actual, $this->description);
227+
}
228+
229+
public function regExp($expression)
230+
{
231+
a::assertRegExp($expression, $this->actual, $this->description);
232+
}
233+
234+
public function matchesFormat($format)
235+
{
236+
a::assertStringMatchesFormat($format, $this->actual, $this->description);
237+
}
238+
239+
public function notMatchesFormat($format)
240+
{
241+
a::assertStringNotMatchesFormat($format, $this->actual, $this->description);
242+
}
243+
244+
public function matchesFormatFile($formatFile)
245+
{
246+
a::assertStringMatchesFormatFile($formatFile, $this->actual, $this->description);
247+
}
248+
249+
public function notMatchesFormatFile($formatFile)
250+
{
251+
a::assertStringNotMatchesFormatFile($formatFile, $this->actual, $this->description);
252+
}
253+
254+
public function same($expected)
255+
{
256+
a::assertSame($expected, $this->actual, $this->description);
257+
}
258+
259+
public function notSame($expected)
260+
{
261+
a::assertNotSame($expected, $this->actual, $this->description);
262+
}
263+
264+
public function selectCount($selector, $count)
265+
{
266+
a::assertSelectCount($selector, $count, $this->actual, $this->description);
267+
}
268+
269+
public function selectEquals($selector, $content, $count)
270+
{
271+
a::assertSelectEquals($selector, $content, $count, $this->actual, $this->description);
272+
}
273+
274+
public function selectRegExp($selector, $pattern, $count)
275+
{
276+
a::assertSelectRegExp($selector, $pattern, $count, $this->actual, $this->description);
277+
}
278+
279+
public function endsWith($suffix)
280+
{
281+
a::assertStringEndsWith($suffix, $this->actual, $this->description);
282+
}
283+
284+
public function notEndsWith($suffix)
285+
{
286+
a::assertStringEndsNotWith($suffix, $this->actual, $this->description);
287+
}
288+
289+
public function equalsFile($file)
290+
{
291+
a::assertStringEqualsFile($file, $this->actual, $this->description);
292+
}
293+
294+
public function notEqualsFile($file)
295+
{
296+
a::assertStringNotEqualsFile($file, $this->actual, $this->description);
297+
}
298+
299+
public function startsWith($prefix)
300+
{
301+
a::assertStringStartsWith($prefix, $this->actual, $this->description);
302+
}
303+
304+
public function notStartsWith($prefix)
305+
{
306+
a::assertStringStartsNotWith($prefix, $this->actual, $this->description);
307+
}
308+
309+
public function tag($matcher)
310+
{
311+
a::assertTag($matcher, $this->actual, $this->description);
312+
}
313+
314+
public function equalsXmlFile($file)
315+
{
316+
if ( ! $this->isFileExpectation ) {
317+
a::assertXmlStringEqualsXmlFile($file, $this->actual, $this->description);
318+
} else {
319+
a::assertXmlFileEqualsXmlFile($file, $this->actual, $this->description);
320+
}
321+
}
322+
323+
public function equalsXmlString($xmlString)
324+
{
325+
a::assertXmlStringEqualsXmlString($xmlString, $this->actual, $this->description);
326+
}
327+
104328
}
105329

src/Codeception/function.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,31 @@ function expect_not($fallacy) {
4242
}
4343

4444
}
45+
46+
if (!function_exists('verify_file')) {
47+
48+
/**
49+
* @param $description
50+
* @param null $actual
51+
* @return \Codeception\Verify
52+
*/
53+
function verify_file() {
54+
include_once __DIR__.'/Verify.php';
55+
56+
$reflect = new ReflectionClass('\Codeception\Verify');
57+
$verify = $reflect->newInstanceArgs(func_get_args());
58+
$verify->setIsFileExpectation(true);
59+
return $verify;
60+
}
61+
}
62+
63+
if (!function_exists('expect_file')) {
64+
/**
65+
* @param $description
66+
* @param null $actual
67+
* @return \Codeception\Verify
68+
*/
69+
function expect_file() {
70+
return call_user_func_array('verify_file', func_get_args());
71+
}
72+
}

0 commit comments

Comments
 (0)