Skip to content

Commit

Permalink
Improve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jul 31, 2024
1 parent cff3c4a commit 4908798
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Framework\MockObject;

use function sprintf;

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class NoMoreReturnValuesConfiguredException extends \PHPUnit\Framework\Exception implements Exception
{
public function __construct(Invocation $invocation, int $numberOfConfiguredReturnValues)
{
parent::__construct(
sprintf(
'Only %d return values have been configured for %s::%s()',
$numberOfConfiguredReturnValues,
$invocation->className(),
$invocation->methodName(),
),
);
}
}
16 changes: 15 additions & 1 deletion src/Framework/MockObject/Runtime/Stub/ConsecutiveCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace PHPUnit\Framework\MockObject\Stub;

use function array_shift;
use function count;
use PHPUnit\Framework\MockObject\Invocation;
use PHPUnit\Framework\MockObject\NoMoreReturnValuesConfiguredException;

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
Expand All @@ -20,14 +22,26 @@
final class ConsecutiveCalls implements Stub
{
private array $stack;
private int $numberOfConfiguredReturnValues;

public function __construct(array $stack)
{
$this->stack = $stack;
$this->stack = $stack;
$this->numberOfConfiguredReturnValues = count($stack);
}

/**
* @throws NoMoreReturnValuesConfiguredException
*/
public function invoke(Invocation $invocation): mixed
{
if (empty($this->stack)) {
throw new NoMoreReturnValuesConfiguredException(
$invocation,
$this->numberOfConfiguredReturnValues,
);
}

$value = array_shift($this->stack);

if ($value instanceof Stub) {
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Framework/MockObject/TestDoubleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use PHPUnit\TestFixture\MockObject\InterfaceWithNeverReturningMethod;
use PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration;
use stdClass;
use TypeError;

abstract class TestDoubleTestCase extends TestCase
{
Expand Down Expand Up @@ -198,7 +197,8 @@ final public function testMethodConfiguredToReturnDifferentValuesOnConsecutiveCa
$this->assertFalse($double->doSomething());
$this->assertTrue($double->doSomething());

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

$double->doSomething();
}
Expand Down

0 comments on commit 4908798

Please sign in to comment.