Skip to content

Commit 998130e

Browse files
Merge branch '11.2'
2 parents 3ea544b + 140a3fe commit 998130e

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Framework\MockObject;
11+
12+
use function sprintf;
13+
14+
/**
15+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
16+
*
17+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
18+
*/
19+
final class NoMoreReturnValuesConfiguredException extends \PHPUnit\Framework\Exception implements Exception
20+
{
21+
public function __construct(Invocation $invocation, int $numberOfConfiguredReturnValues)
22+
{
23+
parent::__construct(
24+
sprintf(
25+
'Only %d return values have been configured for %s::%s()',
26+
$numberOfConfiguredReturnValues,
27+
$invocation->className(),
28+
$invocation->methodName(),
29+
),
30+
);
31+
}
32+
}

src/Framework/MockObject/Runtime/Stub/ConsecutiveCalls.php

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

1212
use function array_shift;
13+
use function count;
1314
use PHPUnit\Framework\MockObject\Invocation;
15+
use PHPUnit\Framework\MockObject\NoMoreReturnValuesConfiguredException;
1416

1517
/**
1618
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
@@ -23,17 +25,29 @@ final class ConsecutiveCalls implements Stub
2325
* @var array<mixed>
2426
*/
2527
private array $stack;
28+
private int $numberOfConfiguredReturnValues;
2629

2730
/**
2831
* @param array<mixed> $stack
2932
*/
3033
public function __construct(array $stack)
3134
{
32-
$this->stack = $stack;
35+
$this->stack = $stack;
36+
$this->numberOfConfiguredReturnValues = count($stack);
3337
}
3438

39+
/**
40+
* @throws NoMoreReturnValuesConfiguredException
41+
*/
3542
public function invoke(Invocation $invocation): mixed
3643
{
44+
if (empty($this->stack)) {
45+
throw new NoMoreReturnValuesConfiguredException(
46+
$invocation,
47+
$this->numberOfConfiguredReturnValues,
48+
);
49+
}
50+
3751
$value = array_shift($this->stack);
3852

3953
if ($value instanceof Stub) {

tests/unit/Framework/MockObject/TestDoubleTestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,21 @@ final public function testMethodCanBeConfiguredToReturnDifferentValuesOnConsecut
196196
$this->assertTrue($double->doSomething());
197197
}
198198

199+
final public function testMethodConfiguredToReturnDifferentValuesOnConsecutiveCallsCannotBeCalledMoreOftenThanReturnValuesHaveBeenConfigured(): void
200+
{
201+
$double = $this->createTestDouble(InterfaceWithReturnTypeDeclaration::class);
202+
203+
$double->method('doSomething')->willReturn(false, true);
204+
205+
$this->assertFalse($double->doSomething());
206+
$this->assertTrue($double->doSomething());
207+
208+
$this->expectException(NoMoreReturnValuesConfiguredException::class);
209+
$this->expectExceptionMessage('Only 2 return values have been configured for PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration::doSomething()');
210+
211+
$double->doSomething();
212+
}
213+
199214
final public function testMethodCanBeConfiguredToReturnDifferentValuesAndThrowExceptionsOnConsecutiveCalls(): void
200215
{
201216
$double = $this->createTestDouble(InterfaceWithReturnTypeDeclaration::class);

0 commit comments

Comments
 (0)