Skip to content

Commit 2d9ee25

Browse files
Merge branch '12.0'
2 parents fd75582 + 114c0fc commit 2d9ee25

File tree

4 files changed

+117
-88
lines changed

4 files changed

+117
-88
lines changed

src/Framework/MockObject/Runtime/Interface/InvocationStubber.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PHPUnit\Framework\MockObject;
1111

1212
use PHPUnit\Framework\Constraint\Constraint;
13+
use PHPUnit\Framework\MockObject\Runtime\PropertyHook;
1314
use PHPUnit\Framework\MockObject\Stub\Stub;
1415
use Throwable;
1516

@@ -19,11 +20,22 @@
1920
interface InvocationStubber
2021
{
2122
/**
23+
* @param Constraint|non-empty-string|PropertyHook $constraint
24+
*
2225
* @return $this
2326
*/
24-
public function method(Constraint|string $constraint): self;
27+
public function method(Constraint|PropertyHook|string $constraint): self;
2528

2629
/**
30+
* @param non-empty-string $id
31+
*
32+
* @return $this
33+
*/
34+
public function id(string $id): self;
35+
36+
/**
37+
* @param non-empty-string $id
38+
*
2739
* @return $this
2840
*/
2941
public function after(string $id): self;

src/Framework/MockObject/Runtime/InvocationHandler.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class InvocationHandler
2727
private array $matchers = [];
2828

2929
/**
30-
* @var array<string,Matcher>
30+
* @var array<non-empty-string, Matcher>
3131
*/
3232
private array $matcherMap = [];
3333

@@ -59,6 +59,8 @@ public function hasMatchers(): bool
5959

6060
/**
6161
* Looks up the match builder with identification $id and returns it.
62+
*
63+
* @param non-empty-string $id
6264
*/
6365
public function lookupMatcher(string $id): ?Matcher
6466
{
@@ -69,6 +71,8 @@ public function lookupMatcher(string $id): ?Matcher
6971
* Registers a matcher with the identification $id. The matcher can later be
7072
* looked up using lookupMatcher() to figure out if it has been invoked.
7173
*
74+
* @param non-empty-string $id
75+
*
7276
* @throws MatcherAlreadyRegisteredException
7377
*/
7478
public function registerMatcher(string $id, Matcher $matcher): void

src/Framework/MockObject/Runtime/InvocationStubberImplementation.php

Lines changed: 92 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,103 @@ public function __construct(InvocationHandler $handler, Matcher $matcher, Config
6161
}
6262

6363
/**
64+
* @param Constraint|non-empty-string|PropertyHook $constraint
65+
*
66+
* @throws InvalidArgumentException
67+
* @throws MethodCannotBeConfiguredException
68+
* @throws MethodNameAlreadyConfiguredException
69+
*
70+
* @return $this
71+
*/
72+
public function method(Constraint|PropertyHook|string $constraint): InvocationStubber
73+
{
74+
if ($this->matcher->hasMethodNameRule()) {
75+
throw new MethodNameAlreadyConfiguredException;
76+
}
77+
78+
if ($constraint instanceof PropertyHook) {
79+
$constraint = $constraint->asString();
80+
}
81+
82+
if (is_string($constraint)) {
83+
$this->configurableMethodNames ??= array_flip(
84+
array_map(
85+
static fn (ConfigurableMethod $configurable) => strtolower($configurable->name()),
86+
$this->configurableMethods,
87+
),
88+
);
89+
90+
if (!array_key_exists(strtolower($constraint), $this->configurableMethodNames)) {
91+
throw new MethodCannotBeConfiguredException($constraint);
92+
}
93+
}
94+
95+
$this->matcher->setMethodNameRule(new Rule\MethodName($constraint));
96+
97+
return $this;
98+
}
99+
100+
/**
101+
* @param non-empty-string $id
102+
*
64103
* @throws MatcherAlreadyRegisteredException
65104
*
66105
* @return $this
67106
*/
68-
public function id(string $id): self
107+
public function id(string $id): InvocationStubber
69108
{
70109
$this->invocationHandler->registerMatcher($id, $this->matcher);
71110

72111
return $this;
73112
}
74113

75114
/**
115+
* @param non-empty-string $id
116+
*
117+
* @return $this
118+
*/
119+
public function after(string $id): InvocationStubber
120+
{
121+
$this->matcher->setAfterMatchBuilderId($id);
122+
123+
return $this;
124+
}
125+
126+
/**
127+
* @throws \PHPUnit\Framework\Exception
128+
* @throws MethodNameNotConfiguredException
129+
* @throws MethodParametersAlreadyConfiguredException
130+
*
76131
* @return $this
77132
*/
78-
public function will(Stub $stub): self
133+
public function with(mixed ...$arguments): InvocationStubber
134+
{
135+
$this->ensureParametersCanBeConfigured();
136+
137+
$this->matcher->setParametersRule(new Rule\Parameters($arguments));
138+
139+
return $this;
140+
}
141+
142+
/**
143+
* @throws MethodNameNotConfiguredException
144+
* @throws MethodParametersAlreadyConfiguredException
145+
*
146+
* @return $this
147+
*/
148+
public function withAnyParameters(): InvocationStubber
149+
{
150+
$this->ensureParametersCanBeConfigured();
151+
152+
$this->matcher->setParametersRule(new Rule\AnyParameters);
153+
154+
return $this;
155+
}
156+
157+
/**
158+
* @return $this
159+
*/
160+
public function will(Stub $stub): InvocationStubber
79161
{
80162
$this->matcher->setStub($stub);
81163

@@ -85,7 +167,7 @@ public function will(Stub $stub): self
85167
/**
86168
* @throws IncompatibleReturnValueException
87169
*/
88-
public function willReturn(mixed $value, mixed ...$nextValues): self
170+
public function willReturn(mixed $value, mixed ...$nextValues): InvocationStubber
89171
{
90172
if (count($nextValues) === 0) {
91173
$this->ensureTypeOfReturnValues([$value]);
@@ -104,14 +186,14 @@ public function willReturn(mixed $value, mixed ...$nextValues): self
104186
return $this->will($stub);
105187
}
106188

107-
public function willReturnReference(mixed &$reference): self
189+
public function willReturnReference(mixed &$reference): InvocationStubber
108190
{
109191
$stub = new ReturnReference($reference);
110192

111193
return $this->will($stub);
112194
}
113195

114-
public function willReturnMap(array $valueMap): self
196+
public function willReturnMap(array $valueMap): InvocationStubber
115197
{
116198
$method = $this->configuredMethod();
117199

@@ -156,117 +238,41 @@ public function willReturnMap(array $valueMap): self
156238
return $this->will($stub);
157239
}
158240

159-
public function willReturnArgument(int $argumentIndex): self
241+
public function willReturnArgument(int $argumentIndex): InvocationStubber
160242
{
161243
$stub = new ReturnArgument($argumentIndex);
162244

163245
return $this->will($stub);
164246
}
165247

166-
public function willReturnCallback(callable $callback): self
248+
public function willReturnCallback(callable $callback): InvocationStubber
167249
{
168250
$stub = new ReturnCallback($callback);
169251

170252
return $this->will($stub);
171253
}
172254

173-
public function willReturnSelf(): self
255+
public function willReturnSelf(): InvocationStubber
174256
{
175257
$stub = new ReturnSelf;
176258

177259
return $this->will($stub);
178260
}
179261

180-
public function willReturnOnConsecutiveCalls(mixed ...$values): self
262+
public function willReturnOnConsecutiveCalls(mixed ...$values): InvocationStubber
181263
{
182264
$stub = new ConsecutiveCalls($values);
183265

184266
return $this->will($stub);
185267
}
186268

187-
public function willThrowException(Throwable $exception): self
269+
public function willThrowException(Throwable $exception): InvocationStubber
188270
{
189271
$stub = new Exception($exception);
190272

191273
return $this->will($stub);
192274
}
193275

194-
/**
195-
* @return $this
196-
*/
197-
public function after(string $id): self
198-
{
199-
$this->matcher->setAfterMatchBuilderId($id);
200-
201-
return $this;
202-
}
203-
204-
/**
205-
* @throws \PHPUnit\Framework\Exception
206-
* @throws MethodNameNotConfiguredException
207-
* @throws MethodParametersAlreadyConfiguredException
208-
*
209-
* @return $this
210-
*/
211-
public function with(mixed ...$arguments): self
212-
{
213-
$this->ensureParametersCanBeConfigured();
214-
215-
$this->matcher->setParametersRule(new Rule\Parameters($arguments));
216-
217-
return $this;
218-
}
219-
220-
/**
221-
* @throws MethodNameNotConfiguredException
222-
* @throws MethodParametersAlreadyConfiguredException
223-
*
224-
* @return $this
225-
*/
226-
public function withAnyParameters(): self
227-
{
228-
$this->ensureParametersCanBeConfigured();
229-
230-
$this->matcher->setParametersRule(new Rule\AnyParameters);
231-
232-
return $this;
233-
}
234-
235-
/**
236-
* @throws InvalidArgumentException
237-
* @throws MethodCannotBeConfiguredException
238-
* @throws MethodNameAlreadyConfiguredException
239-
*
240-
* @return $this
241-
*/
242-
public function method(Constraint|PropertyHook|string $constraint): self
243-
{
244-
if ($this->matcher->hasMethodNameRule()) {
245-
throw new MethodNameAlreadyConfiguredException;
246-
}
247-
248-
if ($constraint instanceof PropertyHook) {
249-
$constraint = $constraint->asString();
250-
}
251-
252-
if (is_string($constraint)) {
253-
$this->configurableMethodNames ??= array_flip(
254-
array_map(
255-
static fn (ConfigurableMethod $configurable) => strtolower($configurable->name()),
256-
$this->configurableMethods,
257-
),
258-
);
259-
260-
if (!array_key_exists(strtolower($constraint), $this->configurableMethodNames)) {
261-
throw new MethodCannotBeConfiguredException($constraint);
262-
}
263-
}
264-
265-
$this->matcher->setMethodNameRule(new Rule\MethodName($constraint));
266-
267-
return $this;
268-
}
269-
270276
/**
271277
* @throws MethodNameNotConfiguredException
272278
* @throws MethodParametersAlreadyConfiguredException

src/Framework/MockObject/Runtime/Matcher.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
final class Matcher
3030
{
3131
private readonly InvocationOrder $invocationRule;
32+
33+
/**
34+
* @var ?non-empty-string
35+
*/
3236
private ?string $afterMatchBuilderId = null;
3337
private ?MethodName $methodNameRule = null;
3438
private ?ParametersRule $parametersRule = null;
@@ -74,6 +78,9 @@ public function setStub(Stub $stub): void
7478
$this->stub = $stub;
7579
}
7680

81+
/**
82+
* @param non-empty-string $id
83+
*/
7784
public function setAfterMatchBuilderId(string $id): void
7885
{
7986
$this->afterMatchBuilderId = $id;

0 commit comments

Comments
 (0)