Skip to content

Commit d79bfff

Browse files
authored
Merge pull request #6 from kenjis/fix-getDouble
Fix getDouble() causes MethodCannotBeConfiguredException
2 parents 1bdd278 + 5ac9cbc commit d79bfff

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

src/TestDouble.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,14 @@ public function getDouble(
5353
$mockBuilder->setConstructorArgs($constructorParams);
5454
}
5555

56-
$methods = [];
57-
$onConsecutiveCalls = [];
58-
$otherCalls = [];
56+
[$onConsecutiveCalls, $otherCalls, $methods] = $this->processParams($params);
5957

60-
foreach ($params as $key => $val) {
61-
if (is_int($key)) {
62-
$onConsecutiveCalls = array_merge($onConsecutiveCalls, $val);
63-
$methods[] = array_keys($val)[0];
64-
} else {
65-
$otherCalls[$key] = $val;
66-
$methods[] = $key;
67-
}
58+
if ($methods === []) {
59+
$mock = $mockBuilder->getMock();
60+
} else {
61+
$mock = $mockBuilder->onlyMethods($methods)->getMock();
6862
}
6963

70-
$mock = $mockBuilder->onlyMethods($methods)->getMock();
71-
7264
foreach ($onConsecutiveCalls as $method => $returns) {
7365
$mock->expects($this->any())->method($method)
7466
->will(
@@ -99,6 +91,30 @@ public function getDouble(
9991
return $mock;
10092
}
10193

94+
/**
95+
* @param array<string, mixed>|array<array> $params [method_name => return_value]
96+
*
97+
* @return array{0: array<string, array<string>>, 1: array<string, mixed>, 2: string[]}
98+
*/
99+
private function processParams(array $params): array
100+
{
101+
$onConsecutiveCalls = [];
102+
$otherCalls = [];
103+
$methods = [];
104+
105+
foreach ($params as $key => $val) {
106+
if (is_int($key)) {
107+
$onConsecutiveCalls = array_merge($onConsecutiveCalls, $val);
108+
$methods[] = array_keys($val)[0];
109+
} else {
110+
$otherCalls[$key] = $val;
111+
$methods[] = $key;
112+
}
113+
}
114+
115+
return [$onConsecutiveCalls, $otherCalls, $methods];
116+
}
117+
102118
/**
103119
* @param array<mixed>|null $params
104120
* @param mixed $expects

tests/Fake/Email.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ public function to(string $to): Email
1010
{
1111
return $this;
1212
}
13+
14+
public function batch_bcc_send(): void
15+
{
16+
}
1317
}

tests/TestDoubleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,12 @@ public function test_verifyNeverInvoked(): void
168168

169169
$this->verifyNeverInvoked($mock, 'method');
170170
}
171+
172+
public function test_create_mock_with_no_params_and_verifyInvokedOnce(): void
173+
{
174+
$mock = $this->getDouble(Email::class, []);
175+
$this->verifyInvokedOnce($mock, 'batch_bcc_send');
176+
177+
$mock->batch_bcc_send();
178+
}
171179
}

0 commit comments

Comments
 (0)