Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}