Skip to content

Commit

Permalink
Merge branch '11.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jul 31, 2024
2 parents 3ea544b + 140a3fe commit 998130e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
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 <sebastian@phpunit.de>
*
* 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 @@ -23,17 +25,29 @@ final class ConsecutiveCalls implements Stub
* @var array<mixed>
*/
private array $stack;
private int $numberOfConfiguredReturnValues;

/**
* @param array<mixed> $stack
*/
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
15 changes: 15 additions & 0 deletions tests/unit/Framework/MockObject/TestDoubleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ final public function testMethodCanBeConfiguredToReturnDifferentValuesOnConsecut
$this->assertTrue($double->doSomething());
}

final public function testMethodConfiguredToReturnDifferentValuesOnConsecutiveCallsCannotBeCalledMoreOftenThanReturnValuesHaveBeenConfigured(): void
{
$double = $this->createTestDouble(InterfaceWithReturnTypeDeclaration::class);

$double->method('doSomething')->willReturn(false, true);

$this->assertFalse($double->doSomething());
$this->assertTrue($double->doSomething());

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

$double->doSomething();
}

final public function testMethodCanBeConfiguredToReturnDifferentValuesAndThrowExceptionsOnConsecutiveCalls(): void
{
$double = $this->createTestDouble(InterfaceWithReturnTypeDeclaration::class);
Expand Down

0 comments on commit 998130e

Please sign in to comment.