<?php
use Prophecy\Argument\Token\AnyValueToken;
use PHPUnit\Framework\TestCase;
class ProphecyTest extends TestCase
{
public function testMockInvocation() : void
{
$fooProphecy = $this->prophesize(Foo::CLASS);
// allow every argument for method Foo::bar()
$fooProphecy->bar(new AnyValueToken())
->willReturn(true)
->shouldBeCalledTimes(2);
// reveal the prophecy of Foo
// this is the mock for your tests to inject
$foo = $fooProphecy->reveal();
$this->assertTrue($foo->bar(null));
$this->assertTrue($foo->bar(1));
// Only 2 invocations of Foo::bar() are expected
$this->assertTrue($foo->bar('test'));
}
}
interface Foo
{
public function bar($value);
}
Result
Time: 00:00.009, Memory: 4.00 MB
There was 1 failure:
1) ProphecyTest::testMockInvocation
Some predictions failed:
Double\Foo\P1:
Expected exactly 2 calls that match:
Double\Foo\P1->bar(*)
but 3 were made:
- bar(null) ProphecyTest.php:20
- bar(1) ProphecyTest.php:21
- bar("test") ProphecyTest.php:23
PhpunitExec.php:21
FAILURES!
Tests: 1, Assertions: 0, Failures: 1.