Skip to content

Instantly share code, notes, and snippets.

@timoschinkel
Last active April 5, 2024 11:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timoschinkel/5cd4f43ce3884b3cf43bb851eea1f924 to your computer and use it in GitHub Desktop.
Save timoschinkel/5cd4f43ce3884b3cf43bb851eea1f924 to your computer and use it in GitHub Desktop.
Prophecy cheatsheet

I typically find myself struggling when doing some more complex expectations using Prophecy. To make things easier for myself I decided to create this cheatsheet.

Type agnostic checks

Check for exact value

$ObjectProphecy->method(
    'value'
);

or

$ObjectProphecy->method(
    Argument::exact('value')
);

Check for identical

$ObjectProphecy->method(
    Argument::is('value')
);

Check for type

$ObjectProphecy->method(
    Argument::type(MyClass::class),
    Argument::type('int')
);

Check for existence of parameter, ignore type and value

$ObjectProphecy->method(
    Argument::any()
);

Don't care about parameters

$ObjectProphecy->method(
    Argument::cetera() // will pass with any list of parameters, or even without any parameters
);

Check for multiple conditions

$ObjectProphecy->method(Argument::allOf(
    Argument::containingString('value_1'),
    Argument::containingString('value_2')
));

Negate argument

Check for specific value in argument

$ObjectProphecy->method(
    Argument::not('value'),
    Argument::not(Argument::type('array'))
);

Strings

Check for existence of a string

$ObjectProphecy->method(
    Argument::containingString('value')
);

Numbers

Check for range

$ObjectProphecy->method(
    Argument::approximate(5, 1)
);

Arrays

Check for number of elements

$ObjectProphecy->method(
    Argument::size($expectedNumberOfElements)
);

Check for specific value in argument

$ObjectProphecy->method(
    Argument::containing('value')
);

Check for specific key in argument

$ObjectProphecy->method(
    Argument::withKey('key')
);

Check for key-value combination in argument

$ObjectProphecy->method(
    Argument::withEntry('key', 'value')
);

Check a condition on every element in the argument

The argument supplied to Argument::withEveryEntry() will be matched against every value in the array passed to the prophesized method:

$ObjectProphecy->method(
    Argument::withEveryEntry(Argument::type('string'))
);

Check for multiple key-value combinations in argument

$ObjectProphecy->method(Argument::allOf(
    Argument::withEntry('key_1', 'value_1'),
    Argument::withEntry('key_2', 'value_2')
));

Spies

Sometimes you might want to make multiple or elaborate checks on arguments.

$ObjectProphecy->method(Argument::that(function(int $param1, string $param2): bool {
    // callback function should have the same parameter signature as spied method
    // Return `true` to match the argument, `false` if it does not, eg.

    return $param1 === $param2;
})
    ->shouldBeCalled()
    ->willReturn(/*...*/);

Mock return values

Return specific value

$objectProphecy
    ->method(Argument::cetera())
    ->willReturn('a value');

Return argument

$objectProphecy
    ->method(Argument::cetera())
    ->willReturnArgument(0);

Construct object using parameters

$objectProphecy
    ->method(Argument::cetera())
    ->will(function(array $args) {
        return new Object($args[0]);
    });

NB In the callback $this is bound to the active MethodProphecy. So in the callback $this does not refer to the TestCase!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment