haijin / testing
Extensions to PHPUnit to ease testing and improve tests expressiveness.
Requires (Dev)
- phpunit/phpunit: ^7.4
This package is auto-updated.
Last update: 2022-02-01 13:15:19 UTC
README
Extensions to PHPUnit to ease testing and improve tests expressiveness.
This library is deprecated and will no longer be developed nor maintained. It was fully replaced by haijin/specs.
Table of contents
Installation
Include this library in your project composer.json
file:
{ ... "require-dev": { ... "haijin/testing": "^0.0.2", ... }, ... }
Usage
Add the trait to the test class
In the TestCase class add the following:
class YourTest extends TestCase { use \Haijin\Testing\AllExpectationsTrait; // ... }
Expectations
Use any of the following expectations:
expectExactExceptionRaised
Expects a tested closure to raise the exact expected Exception class.
Example of use with no assertion closure on the exception:
class YourTest extends TestCase { use \Haijin\Testing\ExceptionsExpectationsTrait; function someTest() { // test code here ... // Expects the closure (second parameter) to raise an exception named SomeException. $this->expectExactExceptionRaised( 'SomeException', function() { $this->some_object->do_something_that_may_raise_the_exception(); } ); // more test code here ... } }
Example of use with an assertion closure on the exception:
class YourTest extends TestCase { use \Haijin\Testing\ExceptionsExpectationsTrait; function someTest() { // test code here ... // Expects the closure (second parameter) to raise an exception named SomeException. $this->expectExactExceptionRaised( 'SomeException', function() { $this->some_object->do_something_that_may_raise_the_exception(); }, function($raised_exception) { $this->assertEquals( 123, $raised_exception->get_value() ); } ); // more test code here ... } }
expectObjectToBeLike
Expects an object or dictionary to be like a given spec, recursively asserting on each attribute of the object.
The expectations can be constant values to assert equality with assertEquals
:
$this->expectObjectToBeLike( $object, [ "name" => "Lisa", "last_name" => "Simpson", "address" => [ "street" => "Evergreen 742" ] ]);
or closures to use any assertion:
$this->expectObjectToBeLike( $object, [ "name" => function($value) { $this->assertEquals( "Lisa", $value ); }, "last_name" => "Simpson", "address" => [ // the closure also accepts an optional parameter with the attribute path: "street" => function($value, $attribute_path) { $this->assertEquals( "Evergreen 742", $value ); } ] ]);
The accessors can be array attributes, object public properties or object public getter methods:
$this->expectObjectToBeLike( $object, [ "get_name()" => function($value) { $this->assertEquals( "Lisa", $value ); }, "get_last_name()" => "Simpson", "get_address()" => [ "street" => "Evergreen 742" ] ]);
expectObjectToBeExactly
Just like expectObjectToBeLike
but if the validated object is an array with attributes not expected in the spec the assertion fails.
expectFileContents
Asserts that a file has the expected contents.
class YourTest extends TestCase { use \Haijin\Testing\FilesExpectationsTrait; function someTest() { // test code here ... $this->expectFileContents( 'File contents', $file_path ); // more test code here ... } }
or using a closure
class YourTest extends TestCase { use \Haijin\Testing\FilesExpectationsTrait; function someTest() { // test code here ... $this->expectFileContents( function($file_contents) { $this->assertEquals( "File contents", $file_contents ); }, $file_path ); // more test code here ... } }
Running the tests
composer test