jacobstr/esperance

BDD style assertion library for PHP.

Installs: 7 263

Dependents: 2

Stars: 1

Watchers: 2

Forks: 3

Language: PHP

v0.1.4 2015-02-27 21:48 UTC

README

master: Build Status develop: Build Status

BDD style assertion library for PHP.

Heavily inspired by expect.js.

Usage

Installation

Espérance can be installed using Composer.

At first, save below as composer.json at the root of your project.

{
    "require": {
        "esperance/esperance": "dev-master"
    }
}

And run these commands.

$ wget http://getcomposer.org/composer.phar
$ php composer.phar install

Then Espérance would be installed in ./vendor directory and also ./vendor/autoload.php is generated.

Very minimal testing script by hand

Just define your expect method or function to construt Esperance\Assertion object.

<?php
require './vendor/autoload.php';

function expect($obj) {
    return new \Esperance\Assertion($obj);
}

expect(1)->to->be(1);

echo "All tests passed.", PHP_EOL;

PHPUnit integrtion

Use esperance/esperance-phpunit.

Extension

Extension using event dispatcher is available.

Events

  • before_assertion \Esperance\Assertion->beforeAssertion($callback)
  • assertion_success \Esperance\Assertion->onAssertionSuccess($callback)
  • assertion_failure \Esperance\Assertion->onAssertionFailure($callback)

Usage

Assertion counter example.

<?php
require './vendor/autoload.php';

$count = $success = $failure = 0;

function expect($subject) {
    global $count, $success, $failure;

    $extension = new \Esperance\Extension;
    $extension->beforeAssertion(function () use (&$count) {
        $count++;
    });

    return new \Esperance\Assertion($subject, $extension);
}

expect(NULL)->to->be(NULL);
expect(0)->to->be(0);
expect(1)->to->be(1);

echo "Count: {$count}", PHP_EOL; // => Count: 3

License

The MIT License

Author

Yuya Takeyama