File tree Expand file tree Collapse file tree 3 files changed +62
-1
lines changed
tests/unit/Framework/MockObject Expand file tree Collapse file tree 3 files changed +62
-1
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1010namespace PHPUnit \Framework \MockObject \Stub ;
1111
1212use function array_shift ;
13+ use function count ;
1314use 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) {
Original file line number Diff line number Diff 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);
You can’t perform that action at this time.
0 commit comments