dev-master
9999999-dev https://github.com/JeffreyWay/PHPUnit-WrapperShould and Assert wrappers for PHPUnit
MIT
The Requires
- php >=5.3.0
by Jeffrey Way
phpunit assert should
Wallogit.com
2017 © Pedro Peláez
Should and Assert wrappers for PHPUnit
Prefer a video introduction?, (*1)
This project provides two wrappers (you can add more) around PHPUnit's assertion library. For example, rather than typing:, (*2)
$this->assertEquals(4, 2 + 2);
You can instead do:, (*3)
Assert::equals(4, 2 + 2); # or Should::equal(4, 2 + 2);
To allow for more readability, when using Should, you may prepend be or have, like so:, (*4)
Should::beTrue(true); Should::haveCount(2, ['a', 'b']);
Additionally, you can register your own aliases., (*5)
Should::getInstance()->registerAliases([
'beCakesAndPies' => 'assertTrue'
]);
# or
Assert::getInstance()->registerAliases([
'eq' => 'assertEquals'
]);
Now, you can use Should::beCakesAndPies and Assert::eq in your tests, and they will map to assertTrue and assertEquals, respectively., (*6)
Install these helpers through Composer. To your composer.json file, add:, (*7)
{
"require-dev": {
"way/phpunit-wrappers": "dev-master"
}
}
Next, as always, run a composer install or composer update for development., (*8)
composer install --dev
That command will pull in those clases under the Way\Tests namespace., (*9)
Next, within your test file, use your desired assertion wrapper (or both)., (*10)
<?php
use Way\Tests\Should;
class DemoTest extends PHPUnit_Framework_TestCase {
public function testItWorks()
{
Should::beTrue(true);
}
}
If you wish, you can optionally alias Should to something shorter, such as S., (*11)
use Way\Tests\Should as S;
This would allow for S::beTrue(). It's an option, but I prefer to stick with the more readable Should., (*12)
And that's it! Here's a few examples:, (*13)
<?php
use Way\Tests\Should;
use Way\Tests\Assert;
class DemoTest extends PHPUnit_Framework_TestCase {
public function testItWorks()
{
Should::beTrue(true);
Assert::true(true);
Should::equal(4, 2 + 2);
Assert::equals(4, 2 + 2);
Should::beGreaterThan(20, 21);
Assert::greaterThan(20, 21);
Should::contain('Joe', ['John', 'Joe']);
Should::have('Joe', ['John', 'Joe']);
Assert::has('Joe', ['John', 'Joe']);
Assert::has('Joe', 'Joey');
Should::haveCount(2, ['1', '2']);
Assert::count(2, ['1', '2']);
}
}
Don't forget to include Composer's autoloader into your project somewhere:
require_once 'vendor/autoload.php'. Alternatively, you can specify a bootstrap option when running PHPUnit:phpunit --bootstrap vendor/autoload.php --colors SomeTest.php, (*14)
Remember: these are simple wrappers around PHPUnit's assertions. Refer to the sidebar here for a full list., (*15)
Should and Assert wrappers for PHPUnit
MIT
phpunit assert should