cornermonkey / phpunit-conditional-assertions
PHPUnit test case extension allowing for conditional assertions to be perofmred.
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/cornermonkey/phpunit-conditional-assertions
Requires
- php: ^8.0
- phpunit/phpunit: ^8.0 || ^9.0 || ^10.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: ^1.3
- rector/rector: ^0.18.6
README
This library adds the ability conditionally call any exiting assertions for PHPUnit. Inspiration for this library came from Pest's conditional expectations and the desire to have the similar functionality in PHPUnit.
Author and copyright
Tim Lawson tim@lawson.fyi
This library is MIT-licensed.
Installation
$ composer require --dev cornermonkey/phpunit-conditional-assertions
Compatibility
This package is compatible with PHP 8.0 and later, and PHPUnit 8.0 and later.
Usage
Simply use the trait CornerMonkey\ConditionalAssertions\ConditionalAssertionTrait in your test case. This
trait adds an when and unless methods to your test case.
Example:
<?php use CornerMonkey\ConditionalAssertion\ConditionalAssertionTrait; use PHPUnit\Framework\TestCase; class MyTestCase extends TestCase { use ConditionalAssertionTrait; public function testConditionIsValid() { $this->when(true)->assertThat(true, true); $this->when(false)->assertThat(true, true); // This assertion will not be called $this->unless(true)->assertThat(true, true); // This assertion will not be called $this->unless(false)->assertThat(true, true); } }
You can also pass a callback to the when and unless methods. The supplied callback will be called
when the condition is true.
<?php use CornerMonkey\ConditionalAssertion\ConditionalAssertionTrait; use PHPUnit\Framework\TestCase; class MyTestCase extends TestCase { use ConditionalAssertionTrait; public function dataProvider() { return [ [true], [false] ]; } /** * @dataProvider dataProvider */ public function testIfExceptionShouldBeThrown($shouldThrow) { $this->when($shouldThrow, function(TestCase $testCase, $value) { $testCase->expectException(Exception::class); }); if ($shouldThrow) { throw new Exception(); } } }