Skip to content

Commit

Permalink
Added new assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Naktibalda committed Jul 13, 2019
1 parent ad94064 commit ee6ca60
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 5 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ verify($user->getPosts())->notNull();
// empty
verify($user->getComments())->isEmpty();
verify($user->getRoles())->notEmpty();

//Other methods:
* stringContainsString
* stringNotContainsString
* stringContainsStringIgnoringCase
* stringNotContainsStringIgnoringCase
* array
* bool
* float
* int
* numeric
* object
* resource
* string
* scalar
* callable
* notArray
* notBool
* notFloat
* notInt
* notNumeric
* notObject
* notResource
* notString
* notScalar
* notCallable
```

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


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

Expand Down
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
],
"require": {
"php": ">= 7.0",
"phpunit/phpunit": "> 6.0"
},
"require-dev": {
"codeception/phpunit-wrapper": ">6.0.15 <6.1.0 | ^6.6.1 | ^7.7.1 | ^8.0.4"
"phpunit/phpunit": "> 6.0",
"codeception/phpunit-wrapper": ">6.0.15 <6.1.0 | ^6.6.1 | ^7.7.1 | ^8.0.4"
},
"autoload": {
"files": ["src/Codeception/function.php"],
Expand Down
122 changes: 121 additions & 1 deletion src/Codeception/Verify.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Codeception;

use PHPUnit\Framework\Assert as a;
use \Codeception\PHPUnit\TestCase as a;

class Verify {

Expand Down Expand Up @@ -307,4 +307,124 @@ public function equalsXmlString($xmlString)
{
a::assertXmlStringEqualsXmlString($xmlString, $this->actual, $this->description);
}

public function stringContainsString($needle)
{
a::assertStringContainsString($needle, $this->actual, $this->description);
}

public function stringNotContainsString($needle)
{
a::assertStringNotContainsString($needle, $this->actual, $this->description);
}

public function stringContainsStringIgnoringCase($needle)
{
a::assertStringContainsStringIgnoringCase($needle, $this->actual, $this->description);
}

public function stringNotContainsStringIgnoringCase($needle)
{
a::assertStringNotContainsStringIgnoringCase($needle, $this->actual, $this->description);
}

public function array()
{
a::assertIsArray($this->actual, $this->description);
}

public function bool()
{
a::assertIsBool($this->actual, $this->description);
}

public function float()
{
a::assertIsFloat($this->actual, $this->description);
}

public function int()
{
a::assertIsInt($this->actual, $this->description);
}

public function numeric()
{
a::assertIsNumeric($this->actual, $this->description);
}

public function object()
{
a::assertIsObject($this->actual, $this->description);
}

public function resource()
{
a::assertIsResource($this->actual, $this->description);
}

public function string()
{
a::assertIsString($this->actual, $this->description);
}

public function scalar()
{
a::assertIsScalar($this->actual, $this->description);
}

public function callable()
{
a::assertIsCallable($this->actual, $this->description);
}

public function notArray()
{
a::assertIsNotArray($this->actual, $this->description);
}

public function notBool()
{
a::assertIsNotBool($this->actual, $this->description);
}

public function notFloat()
{
a::assertIsNotFloat($this->actual, $this->description);
}

public function notInt()
{
a::assertIsNotInt($this->actual, $this->description);
}

public function notNumeric()
{
a::assertIsNotNumeric($this->actual, $this->description);
}

public function notObject()
{
a::assertIsNotObject($this->actual, $this->description);
}

public function notResource()
{
a::assertIsNotResource($this->actual, $this->description);
}

public function notString()
{
a::assertIsNotString($this->actual, $this->description);
}

public function notScalar()
{
a::assertIsNotScalar($this->actual, $this->description);
}

public function notCallable()
{
a::assertIsNotCallable($this->actual, $this->description);
}
}
72 changes: 72 additions & 0 deletions tests/VerifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,78 @@ public function testEqualsXmlString()
expect('<foo><bar>Baz</bar><bar>Baz</bar></foo>')
->equalsXmlString('<foo><bar>Baz</bar><bar>Baz</bar></foo>');
}

public function testStringContainsString()
{
verify('foo bar')->stringContainsString('o b');
verify('foo bar')->stringNotContainsString('BAR');
}

public function testStringContainsStringIgnoringCase()
{
verify('foo bar')->stringContainsStringIgnoringCase('O b');
verify('foo bar')->stringNotContainsStringIgnoringCase('baz');
}

public function testIsString()
{
verify('foo bar')->string();
verify(false)->notString();
}

public function testIsArray()
{
verify([1,2,3])->array();
verify(false)->notArray();
}

public function testIsBool()
{
verify(false)->bool();
verify([1,2,3])->notBool();
}

public function testIsFloat()
{
verify(1.5)->float();
verify(1)->notFloat();
}

public function testIsInt()
{
verify(5)->int();
verify(1.5)->notInt();
}

public function testIsNumeric()
{
verify('1.5')->numeric();
verify('foo bar')->notNumeric();
}

public function testIsObject()
{
verify(new stdClass)->object();
verify(false)->notObject();
}

public function testIsResource()
{
verify(fopen(__FILE__, 'r'))->resource();
verify(false)->notResource();
}

public function testIsScalar()
{
verify('foo bar')->scalar();
verify([1,2,3])->notScalar();
}

public function testIsCallable()
{
verify(function() {})->callable();
verify(false)->notCallable();
}
}


Expand Down

0 comments on commit ee6ca60

Please sign in to comment.