Skip to content

Releases: sirbrillig/spies

v1.5

03 Jan 19:28
Compare
Choose a tag to compare

This release adds the ability to mock and spy on existing functions using the Patchwork library. Unfortunately, you'll have to load Patchwork manually if you want this feature to be available. See the docs for an explanation of how to do that.

v1.4.3

16 Oct 00:56
Compare
Choose a tag to compare
  • Update array_clone to better avoid namespaces
  • Silence warnings generated by array_map in resolve_delayed_expectations

v1.4.2

14 Oct 03:11
Compare
Choose a tag to compare
  • Update array_clone to better avoid namespaces

v1.4.1

12 Sep 17:08
Compare
Choose a tag to compare

If an argument passed to a Spy is an object, it will be passed by reference and stored as a reference in the SpyCall. Before it can be checked by the assertion, it could be mutated by changing another reference to the object.

This release deeply clones function arguments before saving them to prevent possible mutations.

v1.4.0

09 Aug 17:10
Compare
Choose a tag to compare

Better Expectation errors

Modifies Expectation errors to use the new failure message generators in the constraints.

Test:

$spy = \Spies\make_spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'foo', 'bar' );
$spy( 'foo', 'baz' );
$expectation->verify();

Error Before:

Expected "a spy" to be called with ["foo","bar"] but instead it was called with ["foo","baz"]
Failed asserting that false is true.

Error After:

Failed asserting that a spy is called with arguments: ( "foo", "bar" ).
a spy was actually called with:
 1. arguments: ( "foo", "baz" )

Add was_called_times_with

This adds a new Spy method, $spy->was_called_times_with( $count, $arg1, $arg2 ) which can be used to test if a method was called a number of times with specific arguments.

This was already possible using Expectations expect_spy( $spy )->to_have_been_called->with( $arg1, $arg2 )->times( $count ) but it's now backed by the Spy itself rather than implementing the test in the Expectation.

There's a matching assertSpyWasCalledTimesWith( $spy, $count, $args_array ) method also for the PHPUnit custom assertions.

Remove undocumented throw_exceptions

There used to be an undocumented parameter on Expectations called throw_exceptions which would cause verify() to throw an Exception instead of using an assertion. That flag has been removed.

Undocumented silent_failures now returns a boolean

The other undocumented switch on Expectations is silent_failures, which used to cause verify() to return a string version of the error, but now causes it to return a boolean instead. If you rely on this feature, beware that it is in flux and may change in the future!

v1.3.0

27 Jul 03:56
Compare
Choose a tag to compare

Add PHPUnit Custom Assertions

Add Custom assertions for PHPUnit if you subclass \Spies\TestCase: https://github.com/sirbrillig/spies/blob/master/API.md#phpunit-custom-assertions

class MyTest extends \Spies\TestCase {
    function test_spy_is_called_correctly() {
        $spy = \Spies\make_spy();
        $spy( 'hello', 'world', 7 );
        $spy( 'hello', 'world', 8 );
        $this->assertSpyWasCalledWith( 'hello', 'world', \Spies\any() );
    }
}

These can be used in place of Expectations if you want a more pure PHPUnit test experience. You should still call \Spies\finish_spying(); though in your tearDown method to clear all existing spies and mocks.

v1.2.0

22 Jul 17:56
Compare
Choose a tag to compare

Add Spy->was_called_when to allow arbitrary argument matching.

Also adds matching Expectation->when.

v1.1.3

31 May 21:08
Compare
Choose a tag to compare

Prevent accidentally ending an Expectation with once or twice (rather than the correct once() or twice()). This could cause breaking behavior, but any tests that break as a result of this fix were broken to begin with.

v1.1.2

28 Apr 23:58
Compare
Choose a tag to compare

Fix a bug wherein conditional returns could not be falsy (eg: an empty array). Now they can be whatever they want except for null.

v1.1.1

28 Apr 23:27
Compare
Choose a tag to compare

Fix glaring (but somehow hidden) bug which caused unconditional returns on stubs to be overwritten by conditional returns that were defined afterward.