haijin/testing

This package is abandoned and no longer maintained. The author suggests using the haijin/specs package instead.

Extensions to PHPUnit to ease testing and improve tests expressiveness.

0.0.2 2018-12-06 15:38 UTC

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.

Latest Stable Version Latest Unstable Version Build Status License

Table of contents

  1. Installation
  2. Usage
    1. Add the trait to the test class
    2. Expectations
      1. expectExactExceptionRaised
      2. expectObjectToBeLike
      3. expectObjectToBeExactly
      4. expectFileContents
  3. Running the tests

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