Skip to content

Commit

Permalink
Merge pull request #6 from kenjis/fix-getDouble
Browse files Browse the repository at this point in the history
Fix getDouble() causes MethodCannotBeConfiguredException
  • Loading branch information
kenjis authored Sep 30, 2021
2 parents 1bdd278 + 5ac9cbc commit d79bfff
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/TestDouble.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,14 @@ public function getDouble(
$mockBuilder->setConstructorArgs($constructorParams);
}

$methods = [];
$onConsecutiveCalls = [];
$otherCalls = [];
[$onConsecutiveCalls, $otherCalls, $methods] = $this->processParams($params);

foreach ($params as $key => $val) {
if (is_int($key)) {
$onConsecutiveCalls = array_merge($onConsecutiveCalls, $val);
$methods[] = array_keys($val)[0];
} else {
$otherCalls[$key] = $val;
$methods[] = $key;
}
if ($methods === []) {
$mock = $mockBuilder->getMock();
} else {
$mock = $mockBuilder->onlyMethods($methods)->getMock();
}

$mock = $mockBuilder->onlyMethods($methods)->getMock();

foreach ($onConsecutiveCalls as $method => $returns) {
$mock->expects($this->any())->method($method)
->will(
Expand Down Expand Up @@ -99,6 +91,30 @@ public function getDouble(
return $mock;
}

/**
* @param array<string, mixed>|array<array> $params [method_name => return_value]
*
* @return array{0: array<string, array<string>>, 1: array<string, mixed>, 2: string[]}
*/
private function processParams(array $params): array
{
$onConsecutiveCalls = [];
$otherCalls = [];
$methods = [];

foreach ($params as $key => $val) {
if (is_int($key)) {
$onConsecutiveCalls = array_merge($onConsecutiveCalls, $val);
$methods[] = array_keys($val)[0];
} else {
$otherCalls[$key] = $val;
$methods[] = $key;
}
}

return [$onConsecutiveCalls, $otherCalls, $methods];
}

/**
* @param array<mixed>|null $params
* @param mixed $expects
Expand Down
4 changes: 4 additions & 0 deletions tests/Fake/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ public function to(string $to): Email
{
return $this;
}

public function batch_bcc_send(): void
{
}
}
8 changes: 8 additions & 0 deletions tests/TestDoubleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,12 @@ public function test_verifyNeverInvoked(): void

$this->verifyNeverInvoked($mock, 'method');
}

public function test_create_mock_with_no_params_and_verifyInvokedOnce(): void
{
$mock = $this->getDouble(Email::class, []);
$this->verifyInvokedOnce($mock, 'batch_bcc_send');

$mock->batch_bcc_send();
}
}

0 comments on commit d79bfff

Please sign in to comment.