Skip to content

Commit 4908798

Browse files
Improve error message
1 parent cff3c4a commit 4908798

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
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
@@ -20,14 +22,26 @@
2022
final class ConsecutiveCalls implements Stub
2123
{
2224
private array $stack;
25+
private int $numberOfConfiguredReturnValues;
2326

2427
public function __construct(array $stack)
2528
{
26-
$this->stack = $stack;
29+
$this->stack = $stack;
30+
$this->numberOfConfiguredReturnValues = count($stack);
2731
}
2832

33+
/**
34+
* @throws NoMoreReturnValuesConfiguredException
35+
*/
2936
public function invoke(Invocation $invocation): mixed
3037
{
38+
if (empty($this->stack)) {
39+
throw new NoMoreReturnValuesConfiguredException(
40+
$invocation,
41+
$this->numberOfConfiguredReturnValues,
42+
);
43+
}
44+
3145
$value = array_shift($this->stack);
3246

3347
if ($value instanceof Stub) {

tests/unit/Framework/MockObject/TestDoubleTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use PHPUnit\TestFixture\MockObject\InterfaceWithNeverReturningMethod;
1919
use PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration;
2020
use stdClass;
21-
use TypeError;
2221

2322
abstract class TestDoubleTestCase extends TestCase
2423
{
@@ -198,7 +197,8 @@ final public function testMethodConfiguredToReturnDifferentValuesOnConsecutiveCa
198197
$this->assertFalse($double->doSomething());
199198
$this->assertTrue($double->doSomething());
200199

201-
$this->expectException(TypeError::class);
200+
$this->expectException(NoMoreReturnValuesConfiguredException::class);
201+
$this->expectExceptionMessage('Only 2 return values have been configured for PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration::doSomething()');
202202

203203
$double->doSomething();
204204
}

0 commit comments

Comments
 (0)