Skip to content

Commit

Permalink
Fix MockInterface::shouldHaveReceived() and MockInterface::shouldNotH…
Browse files Browse the repository at this point in the history
…aveReceived() stub methods
  • Loading branch information
fancyweb authored and ondrejmirtes committed Jan 10, 2024
1 parent 40dc3e2 commit 88ae859
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
4 changes: 2 additions & 2 deletions stubs/MockInterface.stub
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ interface LegacyMockInterface

/**
* @param null|string $method
* @param null|array<string> $args
* @param null|array<mixed>|\Closure $args
* @return Expectation
*/
public function shouldHaveReceived($method, $args = null);

/**
* @param null|string $method
* @param null|array<string> $args
* @param null|array<mixed>|\Closure $args
* @return Expectation
*/
public function shouldNotHaveReceived($method, $args = null);
Expand Down
24 changes: 19 additions & 5 deletions tests/Mockery/MockeryBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
class MockeryBarTest extends MockeryTestCase
{

/** @var MockInterface|Foo */
/** @var MockInterface&Foo */
private $fooMock;

/** @var MockInterface&Foo */
private $fooSpy;

protected function setUp(): void
{
parent::setUp();

$this->fooMock = Mockery::mock(Foo::class);
$this->fooSpy = Mockery::spy(Foo::class);
}

public function testFooIsCalled(): void
Expand Down Expand Up @@ -53,14 +57,24 @@ public function testShouldReceive(): void

public function testShouldNotHaveReceived(): void
{
$this->fooMock->shouldNotHaveReceived(null)->withArgs(['bar']);
$this->fooSpy->shouldNotHaveReceived(null)->withArgs(['bar']);
$this->fooSpy->doBar('ccc');
$this->fooSpy->shouldNotHaveReceived('doBar', ['ddd']);
$this->fooSpy->shouldNotHaveReceived('doBar', static function (string $arg): bool {
return $arg !== 'ccc';
});
}

public function testShouldHaveReceived(): void
{
$this->fooMock->allows('doFoo')->andReturn('bar');
self::assertSame('bar', $this->fooMock->doFoo());
$this->fooMock->shouldHaveReceived('doFoo')->once();
$this->fooSpy->allows('doFoo')->andReturn('bar');
self::assertSame('bar', $this->fooSpy->doFoo());
$this->fooSpy->shouldHaveReceived('doFoo')->once();
$this->fooSpy->doBar('ccc');
$this->fooSpy->shouldHaveReceived('doBar', ['ccc']);
$this->fooSpy->shouldHaveReceived('doBar', static function (string $arg): bool {
return $arg === 'ccc';
});
}

}
3 changes: 3 additions & 0 deletions tests/Mockery/data/Foo.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ public function doFoo(): ?string
return 'foo';
}

public function doBar(string $arg): void
{
}
}

0 comments on commit 88ae859

Please sign in to comment.