Skip to content

Commit ee6ca60

Browse files
committed
Added new assertions
1 parent ad94064 commit ee6ca60

File tree

4 files changed

+222
-5
lines changed

4 files changed

+222
-5
lines changed

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,32 @@ verify($user->getPosts())->notNull();
4242
// empty
4343
verify($user->getComments())->isEmpty();
4444
verify($user->getRoles())->notEmpty();
45+
46+
//Other methods:
47+
* stringContainsString
48+
* stringNotContainsString
49+
* stringContainsStringIgnoringCase
50+
* stringNotContainsStringIgnoringCase
51+
* array
52+
* bool
53+
* float
54+
* int
55+
* numeric
56+
* object
57+
* resource
58+
* string
59+
* scalar
60+
* callable
61+
* notArray
62+
* notBool
63+
* notFloat
64+
* notInt
65+
* notNumeric
66+
* notObject
67+
* notResource
68+
* notString
69+
* notScalar
70+
* notCallable
4571
```
4672

4773
Shorthands for testing truth/fallacy:
@@ -51,6 +77,7 @@ verify_that($user->isActivated());
5177
verify_not($user->isBanned());
5278
```
5379

80+
5481
These two functions don't check for strict true/false matching, rather `empty` function is used.
5582
`verify_that` checks that result is not empty value, `verify_not` does the opposite.
5683

composer.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
],
1111
"require": {
1212
"php": ">= 7.0",
13-
"phpunit/phpunit": "> 6.0"
14-
},
15-
"require-dev": {
16-
"codeception/phpunit-wrapper": ">6.0.15 <6.1.0 | ^6.6.1 | ^7.7.1 | ^8.0.4"
13+
"phpunit/phpunit": "> 6.0",
14+
"codeception/phpunit-wrapper": ">6.0.15 <6.1.0 | ^6.6.1 | ^7.7.1 | ^8.0.4"
1715
},
1816
"autoload": {
1917
"files": ["src/Codeception/function.php"],

src/Codeception/Verify.php

+121-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Codeception;
33

4-
use PHPUnit\Framework\Assert as a;
4+
use \Codeception\PHPUnit\TestCase as a;
55

66
class Verify {
77

@@ -307,4 +307,124 @@ public function equalsXmlString($xmlString)
307307
{
308308
a::assertXmlStringEqualsXmlString($xmlString, $this->actual, $this->description);
309309
}
310+
311+
public function stringContainsString($needle)
312+
{
313+
a::assertStringContainsString($needle, $this->actual, $this->description);
314+
}
315+
316+
public function stringNotContainsString($needle)
317+
{
318+
a::assertStringNotContainsString($needle, $this->actual, $this->description);
319+
}
320+
321+
public function stringContainsStringIgnoringCase($needle)
322+
{
323+
a::assertStringContainsStringIgnoringCase($needle, $this->actual, $this->description);
324+
}
325+
326+
public function stringNotContainsStringIgnoringCase($needle)
327+
{
328+
a::assertStringNotContainsStringIgnoringCase($needle, $this->actual, $this->description);
329+
}
330+
331+
public function array()
332+
{
333+
a::assertIsArray($this->actual, $this->description);
334+
}
335+
336+
public function bool()
337+
{
338+
a::assertIsBool($this->actual, $this->description);
339+
}
340+
341+
public function float()
342+
{
343+
a::assertIsFloat($this->actual, $this->description);
344+
}
345+
346+
public function int()
347+
{
348+
a::assertIsInt($this->actual, $this->description);
349+
}
350+
351+
public function numeric()
352+
{
353+
a::assertIsNumeric($this->actual, $this->description);
354+
}
355+
356+
public function object()
357+
{
358+
a::assertIsObject($this->actual, $this->description);
359+
}
360+
361+
public function resource()
362+
{
363+
a::assertIsResource($this->actual, $this->description);
364+
}
365+
366+
public function string()
367+
{
368+
a::assertIsString($this->actual, $this->description);
369+
}
370+
371+
public function scalar()
372+
{
373+
a::assertIsScalar($this->actual, $this->description);
374+
}
375+
376+
public function callable()
377+
{
378+
a::assertIsCallable($this->actual, $this->description);
379+
}
380+
381+
public function notArray()
382+
{
383+
a::assertIsNotArray($this->actual, $this->description);
384+
}
385+
386+
public function notBool()
387+
{
388+
a::assertIsNotBool($this->actual, $this->description);
389+
}
390+
391+
public function notFloat()
392+
{
393+
a::assertIsNotFloat($this->actual, $this->description);
394+
}
395+
396+
public function notInt()
397+
{
398+
a::assertIsNotInt($this->actual, $this->description);
399+
}
400+
401+
public function notNumeric()
402+
{
403+
a::assertIsNotNumeric($this->actual, $this->description);
404+
}
405+
406+
public function notObject()
407+
{
408+
a::assertIsNotObject($this->actual, $this->description);
409+
}
410+
411+
public function notResource()
412+
{
413+
a::assertIsNotResource($this->actual, $this->description);
414+
}
415+
416+
public function notString()
417+
{
418+
a::assertIsNotString($this->actual, $this->description);
419+
}
420+
421+
public function notScalar()
422+
{
423+
a::assertIsNotScalar($this->actual, $this->description);
424+
}
425+
426+
public function notCallable()
427+
{
428+
a::assertIsNotCallable($this->actual, $this->description);
429+
}
310430
}

tests/VerifyTest.php

+72
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,78 @@ public function testEqualsXmlString()
218218
expect('<foo><bar>Baz</bar><bar>Baz</bar></foo>')
219219
->equalsXmlString('<foo><bar>Baz</bar><bar>Baz</bar></foo>');
220220
}
221+
222+
public function testStringContainsString()
223+
{
224+
verify('foo bar')->stringContainsString('o b');
225+
verify('foo bar')->stringNotContainsString('BAR');
226+
}
227+
228+
public function testStringContainsStringIgnoringCase()
229+
{
230+
verify('foo bar')->stringContainsStringIgnoringCase('O b');
231+
verify('foo bar')->stringNotContainsStringIgnoringCase('baz');
232+
}
233+
234+
public function testIsString()
235+
{
236+
verify('foo bar')->string();
237+
verify(false)->notString();
238+
}
239+
240+
public function testIsArray()
241+
{
242+
verify([1,2,3])->array();
243+
verify(false)->notArray();
244+
}
245+
246+
public function testIsBool()
247+
{
248+
verify(false)->bool();
249+
verify([1,2,3])->notBool();
250+
}
251+
252+
public function testIsFloat()
253+
{
254+
verify(1.5)->float();
255+
verify(1)->notFloat();
256+
}
257+
258+
public function testIsInt()
259+
{
260+
verify(5)->int();
261+
verify(1.5)->notInt();
262+
}
263+
264+
public function testIsNumeric()
265+
{
266+
verify('1.5')->numeric();
267+
verify('foo bar')->notNumeric();
268+
}
269+
270+
public function testIsObject()
271+
{
272+
verify(new stdClass)->object();
273+
verify(false)->notObject();
274+
}
275+
276+
public function testIsResource()
277+
{
278+
verify(fopen(__FILE__, 'r'))->resource();
279+
verify(false)->notResource();
280+
}
281+
282+
public function testIsScalar()
283+
{
284+
verify('foo bar')->scalar();
285+
verify([1,2,3])->notScalar();
286+
}
287+
288+
public function testIsCallable()
289+
{
290+
verify(function() {})->callable();
291+
verify(false)->notCallable();
292+
}
221293
}
222294

223295

0 commit comments

Comments
 (0)