Spectre
Aims to write-and-run your specs in a easy way. Quickly., (*1)
- Mock support for classes and functions*
- Code-coverage reporting with PHPUnit
- Don't struggle with classes!
- Save as TAP or JSON
- Watch mode
, (*2)
How to?
composer.json, (*3)
{
"require-dev": {
"habanero/spectre": "v0.2.0"
}
}
inc/sum.php, (*4)
<?php
function sum($a, $b)
{
return $a + $b;
}
specs/sum-spec.php, (*5)
<?php
require 'inc/sum.php';
describe('sum()', function () {
it('sums two numbers', function () {
expect(sum(2, 2))->toBe(4);
});
});
Execute your specs:, (*6)
$ vendor/bin/spectre specs
Running specs...
sum()
โ sums two numbers ... OK
Done (0.0017s)
Available matchers
-
toBe($value) โ Strict equal comparison (with
===
)
-
toBeA($value) โ Data-type comparison (with
is_<type>()
)
-
toBeLike($value) โ Alias for toEqual
-
toEquals($value) โ Alias for toEqual
-
toBeGreaterThan($value) โ Comparison using the
>
operator
-
toBeLessThan($value) โ Comparison using the
<
operator
-
toBeAnInstanceOf($value) โ Alias for toBeInstanceOf
-
toBeInstanceOf($value) โ Comparison using the
instanceof
operator
-
toBeEmpty() โ Tests agains
empty()
-
toBeTruthy() โ Test for truthy-values
-
toBeFalsy() โ Test for falsy-values
-
toBeArray() โ Test using
is_array()
-
toBeBoolean() โ Alias for toBeBool
-
toBeBool() โ Test using
is_bool()
-
toBeCallable() โ Test using
is_callable()
-
toBeDouble() โ Test using
is_double()
-
toBeFloat() โ Test using
is_float()
-
toBeInt() โ Test using
is_int()
-
toBeInteger() โ Test using
is_integer()
-
toBeLong() โ Test using
is_long()
-
toBeNull() โ Test using
is_null()
-
toBeNumeric() โ Test using
is_numeric()
-
toBeObject() โ Test using
is_object()
-
toBeReal() โ Test using
is_real()
-
toBeResource() โ Test using
is_resource()
-
toBeScalar() โ Test using
is_scalar()
-
toBeString() โ Test using
is_string()
-
toHaveKey($value) โ Test arrays using
isset()
-
toHaveLength([$value]) โ Tests using
count()
and strlen()
-
toEndWith($value) โ Test for trailing substrings
-
toStartWith($value) โ Test for leading substrings
-
toContains($value) โ Alias for toContain
-
toContain($value) โ Test for including substrings
-
toEqual($value) โ Weak equal comparison (with
==
)
-
toMatch($value) โ Test strings with regular expressions
-
toPrint($value) โ Test for buffered-substrings (capture includes/echos with buffers)
-
toThrow([$value]) โ Test for exceptions, if
$value
is provided will test against instanceof
-
toWarn([$value]) โ Test for buffered-substrings at user-level errors, notices and warnings (no fatal ones)
Custom matchers
To register your own matchers you should implements the following code:, (*7)
\Spectre\Base::customMatchers('toBeCustomValue', function ($expected, $value) {
// test $value against $this->expected
// then return true or false
//
// or for custom messages:
//
// return array(
// 'result' => $expected === $value,
// 'negative' => "Expected '{subject}' {verb} '{value}', but it did not",
// 'positive' => "Did not expect '{subject}' {verb} '{value}', but it did",
// );
});
Note that any additional arguments will be passed after the $value
argument., (*8)
Mock support
Since 0.3.0
you can mock classes and some built-in functions using the same API, see spec/mock-spec.php for more examples., (*9)
In short, you can mock any class or function as follows:, (*10)
// class
$stub = spy($namespace, $className)
->methods('testMethod')
->getMock();
$stub->expects($callback = any())
->method('testMethod')
->willReturn(42);
// function
$stub = fun($namespace, $function)
->expects($callback = any())
->willReturn(42);
Available constraints
- any()
- never()
- atLeast($count)
- atLeastOnce()
- once()
- exactly($count)
- atMost($count)
- at($index)
- returnValue($test)
- returnValueMap($test)
- returnArgument($index)
- returnCallback($fn)
- returnSelf()
- throwException($e)
- onConsecutiveCalls()
Some constraints are also spies, given a $callback
reference you can ask later for:, (*11)
- hasBeenInvoked()
- getInvocations()
- getInvocationCount()
CLI options
Type vendor/bin/spectre
without arguments to get the following:, (*12)
Usage: vendor/bin/spectre [options] <folders|files>
-h --help Display this help
-w --watch Enables the watch mode
-t --timeout Timeout in seconds for watch mode
-c --coverage Enables code coverage instrumentation
-f --filter Filter for executing specific tests by name
-x --exclude Folders and files to exclude from coverage
-o --output Custom filename for saving coverage report
-r --reporter Default reporter for coverage. Options: JSON, TAP
Examples
You can mix almost all arguments on several ways, i.e:, (*13)
$ vendor/bin/spectre specs -rTAP -c -xvendor -xspecs
$ vendor/bin/spectre ./specs /path/to/specs --coverage --exclude docs
$ vendor/bin/spectre $PWD/specs --output results.json