BDD Assertions for PHPUnit or Codeception
This is very tiny wrapper for PHPUnit assertions, that are aimed to make tests a bit more readable. With BDD assertions influenced by Chai, Jasmine, and RSpec your assertions would be a bit closer to natural language.
Requires PHP 7.1 or higher
composer require codeception/verify --dev Use in any test verify function instead of $this->assert* methods:
$user = User::find(1); // equalverify($user->getName())->equals('davert'); verify("user have 5 posts", $user->getNumPosts())->equals(5); verify($user->getNumPosts())->notEquals(3); // containsverify('first user is admin', $user->getRoles())->contains('admin'); verify("first user is not banned", $user->getRoles())->notContains('banned'); // greater / less$rate = $user->getRate(); verify('first user rate is 7', $rate)->equals(7); verify($rate)->greaterThan(5); verify($rate)->lessThan(10); verify($rate)->lessOrEquals(7); verify($rate)->greaterOrEquals(5); // true / false / nullverify($user->isAdmin())->true(); verify($user->isBanned())->false(); verify($user->invitedBy)->null(); verify($user->getPosts())->notNull(); // emptyverify($user->getComments())->isEmpty(); verify($user->getRoles())->notEmpty(); // throwsverify($callback)->throws(); verify($callback)->throws(Exception::class); verify($callback)->throws(Exception::class, 'exception message'); verify($callback)->throws(newException()); verify($callback)->throws(newException('message')); // does not throwverify($callback)->doesNotThrow(); verify($callback)->throws(Exception::class); verify($callback)->doesNotThrow(newException()); // and many more !📄 See Verifiers full list here.
Shorthands for testing truth/fallacy:
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.
If you follow TDD/BDD you'd rather use expect instead of verify. Which are just an alias functions:
expect("user have 5 posts", $user->getNumPosts())->equals(5); expect_that($user->isActive()); expect_not($user->isBanned());In order to add more assertions you can override Codeception\Verify class:
class MyVerify extends \Codeception\Verify{publicfunctionsuccess(){} }Set the class name to Codeception\Verify::$override property to verify function use it:
\Codeception\Verify::$override = MyVerify::class; // access overridden classverify('it works')->success();Verify is open-sourced software licensed under the MIT License. © Codeception PHP Testing Framework
