2017 © Pedro Pelรกez
 

library spectre

Simple spec-testing tools

image

habanero/spectre

Simple spec-testing tools

  • Monday, February 1, 2016
  • by pateketrueke
  • Repository
  • 3 Watchers
  • 5 Stars
  • 391 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 12 Versions
  • 0 % Grown

The README.md

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

CI, (*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

The Versions

01/02 2016

dev-master

9999999-dev

Simple spec-testing tools

  Sources   Download

MIT

The Requires

 

by Alvaro Cabrera

01/02 2016

v0.3.0

0.3.0.0

Simple spec-testing tools

  Sources   Download

MIT

The Requires

 

by Alvaro Cabrera

31/01 2016

v0.2.7

0.2.7.0

Simple spec-testing tools

  Sources   Download

MIT

The Requires

 

by Alvaro Cabrera

31/01 2016

v0.2.6

0.2.6.0

Simple spec-testing tools

  Sources   Download

MIT

The Requires

 

by Alvaro Cabrera

21/01 2016

v0.2.5

0.2.5.0

Simple spec-testing tools

  Sources   Download

MIT

The Requires

 

by Alvaro Cabrera

18/07 2015

v0.2.4

0.2.4.0

Simple spec-testing tools

  Sources   Download

MIT

The Requires

 

by Alvaro Cabrera

28/10 2014

v0.2.3

0.2.3.0

Simple spec-testing tools

  Sources   Download

MIT

The Requires

 

by Alvaro Cabrera

15/09 2014

dev-develop

dev-develop

Simple spec-testing tools

  Sources   Download

MIT

The Requires

 

by Alvaro Cabrera

15/09 2014

v0.2.2

0.2.2.0

Simple spec-testing tools

  Sources   Download

MIT

The Requires

 

by Alvaro Cabrera

12/09 2014

v0.2.1

0.2.1.0

Simple spec-testing tools

  Sources   Download

MIT

The Requires

 

by Alvaro Cabrera

09/09 2014

v0.2.0

0.2.0.0

Simple spec-testing tools

  Sources   Download

MIT

The Requires

 

by Alvaro Cabrera

29/08 2014

v0.1.0

0.1.0.0

Simple spec-testing tools

  Sources   Download

MIT

The Requires

 

by Alvaro Cabrera