Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for argument expectations in Stubs #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/Stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,19 @@ protected static function bindParameters($mock, $params)
if ($reflectionClass->hasMethod($param)) {
if ($value instanceof StubMarshaler) {
$marshaler = $value;
$mock
->expects($marshaler->getMatcher())
->method($param)
->will(new ReturnCallback($marshaler->getValue()));
$methodArguments = $marshaler->getArguments();
if ($methodArguments !== null) {
$mock
->expects($marshaler->getMatcher())
->method($param)
->with(...$methodArguments)
->will(new ReturnCallback($marshaler->getValue()));
} else {
$mock
->expects($marshaler->getMatcher())
->method($param)
->will(new ReturnCallback($marshaler->getValue()));
}
} elseif ($value instanceof \Closure) {
$mock
->expects(new AnyInvokedCount)
Expand Down
37 changes: 37 additions & 0 deletions src/Stub/StubMarshaler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class StubMarshaler

private $methodValue;

private $methodArguments = null;

public function __construct(InvocationOrder $matcher, $value)
{
$this->methodMatcher = $matcher;
Expand All @@ -28,4 +30,39 @@ public function getValue()
{
return $this->methodValue;
}

/**
* @return array|null expected method arguments, null makes it skip the check
*/
public function getArguments()
rbait marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->methodArguments;
}

/**
* Checks if a method is called with the specified arguments
*
* If the method is invoked with different arguments, an exception will be thrown.
*
* ```php
* <?php
* use \Codeception\Stub\Expected;
*
* $user = $this->make('User', [
* 'setName' => Expected::once()->with('Davert')
* ]);
* $user->setName('Davert');
* ?>
* ```
*
* @see \PHPUnit\Framework\MockObject\Builder\InvocationMocker::with
* @param array ...$arguments
* @return StubMarshaler
*/
public function with(...$arguments)
rbait marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This docblock is a good code documentation, but in order to be visible in documentation pages of https://codeception.com it must be documented elsewhere.
Reference page https://codeception.com/docs/reference/Stub is generated from the docblocks of Stub.php so please mention your new functionality in appropriate place of that file.

There are some examples of using Stub in https://codeception.com/docs/05-UnitTests#Test-Doubles
You can update it by editing https://github.com/Codeception/codeception.github.com/blob/master/docs/05-UnitTests.md

Your new functionality will only be available on PHP 7.2+ with PHPUnit 8.4 or 9, unless this change is merged to 2.x branch too.

{
$this->methodArguments = $arguments;

return $this;
}
}
49 changes: 49 additions & 0 deletions tests/StubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,55 @@ public function testConsecutive()
$this->assertNull($dummy->helloWorld());
}

public static function argumentsMatchProvider()
{
return array(
array(Stub\Expected::once()->with(), []),
array(Stub\Expected::once()->with('it is me'), ['it is me']),
array(Stub\Expected::once()->with(new PHPUnit\Framework\Constraint\IsAnything), ['it is me']),
array(Stub\Expected::once()->with('it is me', 'and you'), ['it is me', 'and you']),
);
}

/**
* @dataProvider argumentsMatchProvider
*/
public function testWithArgumentsMatch($matcher, $arguments)
{
$dummy = Stub::make('DummyClass', array('helloWorld' => $matcher));

$this->assertNull($dummy->helloWorld(...$arguments));
}

public static function argumentsMismatchProvider()
{
return array(
array(Stub\Expected::once()->with('it is me'), [], 'Parameter count for invocation DummyClass::helloWorld() is too low.'),
array(Stub\Expected::once()->with('it is me'), ['it is you'], 'Parameter 0 for invocation DummyClass::helloWorld(\'it is you\') does not match expected value.'),
array(Stub\Expected::once()->with('it is me', 'and you'), ['it is me'], 'Parameter count for invocation DummyClass::helloWorld(\'it is me\') is too low.'),
);
}

/**
* @dataProvider argumentsMismatchProvider
*/
public function testWithArgumentsMismatch($matcher, $arguments, $failMessage)
{
$dummy = Stub::make('DummyClass', array('helloWorld' => $matcher));

try {
$dummy->helloWorld(...$arguments);
$this->fail('Expected exception');
} catch (\Exception $e) {
if ($e->getMessage() === 'Expected exception') {
$this->fail('Expected exception');
}
$this->assertStringContainsString($failMessage, $e->getMessage());
}

$this->resetMockObjects();
}

public function testStubPrivateProperties()
{
$tester = Stub::construct(
Expand Down