expect/expect

Expectation library for unit testing

2.0.0 2016-08-08 02:53 UTC

This package is auto-updated.

Last update: 2024-04-15 14:51:04 UTC


README

Build Status HHVM Status Scrutinizer Code Quality Coverage Status Dependency Status
Stories in Ready Latest Stable Version License

Basic usage

use expect\Expect;
use expect\configurator\FileConfigurator;

$configurator = new FileConfigurator(__DIR__ . '/.expect.toml');
Expect::configure($configurator);

Expect::that(true)->toEqual(true); //pass
Expect::that(false)->toEqual(true); //failed

Configuration file

You can specify the matcher package and a reporter to be used in the configuration file.
Configuration file in toml format, examples are as follows.

packages = [
  "\\expect\\fixture\\package\\CustomPackageRegistrar"
]

reporter = "\\expect\\reporter\\ExceptionReporter"

Learn detailed document

Other recommended library

If you did not like this library, please try the following library.

  • Leo - Assertions and matcher library, in peridot and affinity is good.
  • Assert - Simple assertion library, Assertion api is easy to use with simple.

All of matcher

toEqual

Expect::that(true)->toEqual(true); //pass
Expect::that(false)->toEqual(true); //failed

toBeTrue

Expect::that(true)->toBeTrue(); //pass
Expect::that(false)->toBeTrue(); //failed

toBeFalse

Expect::that(false)->toBeFalse(); //pass
Expect::that(true)->toBeFalse(); //failed

toBeNull

Expect::that(null)->toBeNull(); //pass
Expect::that(100)->toBeNull(); //failed

toBeAnInstanceOf

Expect::that(new ToEqual(true))->toBeAnInstanceOf('expect\Mathcer'); //pass
Expect::that(new stdClass)->toBeAnInstanceOf('expect\Mathcer'); //failed

toBeAn / toBeA

Expect::that(1)->toBeAn('integer'); //pass
Expect::that('foo')->toBeAn('integer'); //failed
Expect::that('foo')->toBeInteger(); //failed
Expect::that('foo')->toBeAn('string'); //pass
Expect::that(1)->toBeAn('string'); //failed
Expect::that('foo')->toBeString(); //failed
Expect::that(1.1)->toBeAn('float'); //pass
Expect::that('foo')->toBeAn('float'); //failed
Expect::that('foo')->toBeFloat(); //failed
Expect::that(true)->toBeAn('boolean'); //pass
Expect::that('foo')->toBeAn('boolean'); //failed
Expect::that('foo')->toBeBoolean(); //failed
Expect::that(1)->toBeA('integer'); //pass
Expect::that('foo')->toBeA('integer'); //failed
Expect::that([])->toBeA('array'); //pass
Expect::that('foo')->toBeArray('array'); //failed

toMatch

Expect::that('foobar')->toMatch('/foo/'); //pass
Expect::that('foobar')->toMatch('/cat/'); //failed

toStartWith

Expect::that('foobar')->toStartWith('foo'); //pass
Expect::that('foobar')->toStartWith('cat'); //failed

toEndWith

Expect::that('foobar')->toEndWith('bar'); //pass
Expect::that('foobar')->toEndWith('cat'); //failed

toHaveLength

Expect::that('foobar')->toHaveLength(6); //pass
Expect::that('foobar')->toHaveLength(5); //failed
Expect::that([ 1 ])->toHaveLength(1); //pass
Expect::that([ 1, 2 ])->toHaveLength(3); //failed
Expect::that(new ArrayIterator([ 1 ]))->toHaveLength(1); //pass
Expect::that(new ArrayIterator([ 1, 2 ]))->toHaveLength(3); //failed

toBeEmpty

Expect::that([])->toBeEmpty(); //pass
Expect::that([ 1 ])->toBeEmpty(); //failed

toPrint

Expect::that(function () {
    echo 'foo';
})->toPrint('foo'); //pass

Expect::that(function () {
    echo 'bar';
})->toPrint('foo'); //failed

toBeGreaterThan / toBeAbove

Expect::that(11)->toBeGreaterThan(10); //pass
Expect::that(10)->toBeGreaterThan(10); //pass
Expect::that(9)->toBeGreaterThan(10); //failed
Expect::that(11)->toBeAbove(10); //pass
Expect::that(10)->toBeAbove(10); //pass
Expect::that(9)->toBeAbove(10); //failed

toBeLessThan / toBeBelow

Expect::that(9)->toBeLessThan(10); //pass
Expect::that(10)->toBeLessThan(10); //failed
Expect::that(9)->toBeBelow(10); //pass
Expect::that(10)->toBeBelow(10); //failed

toBeWithin

Expect::that(11)->toBeWithin(10, 20); //pass
Expect::that(9)->toBeWithin(10, 20); //failed
Expect::that(21)->toBeWithin(10, 20); //failed

toContain

Expect::that('foo')->toContain('foo'); //pass
Expect::that('foo')->toContain('foo', 'bar'); //failed
Expect::that([ 'foo', 'bar' ])->toContain('foo'); //pass
Expect::that([ 'foo', 'bar' ])->toContain('foo', 'bar'); //pass
Expect::that([ 'foo', 'bar' ])->toContain('foo', 'bar', 'bar1'); //failed

toHaveKey

Expect::that([ 'foo' => 1 ])->toHaveKey('foo'); //pass
Expect::that([ 'foo' => 1 ])->toHaveKey('bar'); //failed

ToBeTruthy

Expect::that(true)->ToBeTruthy(); //pass
Expect::that('')->ToBeTruthy(); //pass
Expect::that(0)->ToBeTruthy(); //pass
Expect::that(false)->ToBeTruthy(); //failed

ToBeFalsey

Expect::that(false)->ToBeFalsey(); //pass
Expect::that(null)->ToBeFalsey(); //pass
Expect::that('')->ToBeFalsey(); //failed
Expect::that(0)->ToBeFalsey(); //failed