From dbba41d241cd40edca7f88f9ef42a38fb2455995 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Mon, 28 Dec 2020 11:42:51 +0100 Subject: [PATCH] Closes #4564 --- ChangeLog-10.0.md | 1 + .../MockObject/Builder/InvocationMocker.php | 18 ++++ .../Builder/InvocationMockerTest.php | 14 --- .../Matcher/ConsecutiveParametersTest.php | 88 ------------------- .../Framework/MockObject/MockObjectTest.php | 23 ----- 5 files changed, 19 insertions(+), 125 deletions(-) delete mode 100644 tests/unit/Framework/MockObject/Matcher/ConsecutiveParametersTest.php diff --git a/ChangeLog-10.0.md b/ChangeLog-10.0.md index 749bfb2d2cc..fe04bf8e8b5 100644 --- a/ChangeLog-10.0.md +++ b/ChangeLog-10.0.md @@ -41,6 +41,7 @@ All notable changes of the PHPUnit 10.0 release series are documented in this fi * [#4397](https://github.com/sebastianbergmann/phpunit/issues/4397): Remove confusing parameter options for XML assertions * [#4531](https://github.com/sebastianbergmann/phpunit/pull/4531): Remove `--loader` option as well as `testSuiteLoaderClass` and `testSuiteLoaderFile` XML configuration settings * [#4536](https://github.com/sebastianbergmann/phpunit/issues/4536): Remove `assertFileNotIsWritable()` +* [#4564](https://github.com/sebastianbergmann/phpunit/issues/4564): Deprecate `withConsecutive()` * [#4567](https://github.com/sebastianbergmann/phpunit/issues/4567): Deprecate support for generators in `assertCount()` and `Count` constraint * Removed the `PHPUnit\Runner\TestSuiteLoader` interface diff --git a/src/Framework/MockObject/Builder/InvocationMocker.php b/src/Framework/MockObject/Builder/InvocationMocker.php index d8bd555f3dc..06d76fc3452 100644 --- a/src/Framework/MockObject/Builder/InvocationMocker.php +++ b/src/Framework/MockObject/Builder/InvocationMocker.php @@ -12,6 +12,7 @@ use function array_map; use function array_merge; use function count; +use function debug_backtrace; use function in_array; use function is_string; use function strtolower; @@ -35,6 +36,7 @@ use PHPUnit\Framework\MockObject\Stub\ReturnStub; use PHPUnit\Framework\MockObject\Stub\ReturnValueMap; use PHPUnit\Framework\MockObject\Stub\Stub; +use PHPUnit\Framework\TestCase; use Throwable; /** @@ -188,11 +190,27 @@ public function with(...$arguments): self * @throws MethodParametersAlreadyConfiguredException * * @return $this + * + * @deprecated https://github.com/sebastianbergmann/phpunit/issues/4564 */ public function withConsecutive(...$arguments): self { $this->ensureParametersCanBeConfigured(); + $stack = debug_backtrace(); + + while (!empty($stack)) { + $frame = array_pop($stack); + + if (isset($frame['object']) && $frame['object'] instanceof TestCase) { + $frame['object']->addWarning( + 'The withConsecutive() method has been deprecated. It will be removed in PHPUnit 11.' + ); + + break; + } + } + $this->matcher->setParametersRule(new Rule\ConsecutiveParameters($arguments)); return $this; diff --git a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php index f6fb60a4bc0..1442437ea22 100644 --- a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php +++ b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php @@ -52,20 +52,6 @@ public function testWillReturnWithMultipleValues(): void $this->assertEquals(3, $mock->foo()); } - public function testWillReturnOnConsecutiveCalls(): void - { - $mock = $this->getMockBuilder(stdClass::class) - ->addMethods(['foo']) - ->getMock(); - - $mock->method('foo') - ->willReturnOnConsecutiveCalls(1, 2, 3); - - $this->assertEquals(1, $mock->foo()); - $this->assertEquals(2, $mock->foo()); - $this->assertEquals(3, $mock->foo()); - } - public function testWillReturnByReference(): void { $mock = $this->getMockBuilder(stdClass::class) diff --git a/tests/unit/Framework/MockObject/Matcher/ConsecutiveParametersTest.php b/tests/unit/Framework/MockObject/Matcher/ConsecutiveParametersTest.php deleted file mode 100644 index 5ef4f13bba9..00000000000 --- a/tests/unit/Framework/MockObject/Matcher/ConsecutiveParametersTest.php +++ /dev/null @@ -1,88 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -use PHPUnit\Framework\ExpectationFailedException; -use PHPUnit\Framework\InvalidParameterGroupException; -use PHPUnit\Framework\TestCase; - -/** - * @small - */ -final class ConsecutiveParametersTest extends TestCase -{ - public function testIntegration(): void - { - $mock = $this->getMockBuilder(stdClass::class) - ->addMethods(['foo']) - ->getMock(); - - $mock->expects($this->any()) - ->method('foo') - ->withConsecutive( - ['bar'], - [21, 42] - ); - - $this->assertNull($mock->foo('bar')); - $this->assertNull($mock->foo(21, 42)); - } - - public function testIntegrationWithLessAssertionsThanMethodCalls(): void - { - $mock = $this->getMockBuilder(stdClass::class) - ->addMethods(['foo']) - ->getMock(); - - $mock->expects($this->any()) - ->method('foo') - ->withConsecutive( - ['bar'] - ); - - $this->assertNull($mock->foo('bar')); - $this->assertNull($mock->foo(21, 42)); - } - - public function testIntegrationExpectingException(): void - { - $mock = $this->getMockBuilder(stdClass::class) - ->addMethods(['foo']) - ->getMock(); - - $mock->expects($this->any()) - ->method('foo') - ->withConsecutive( - ['bar'], - [21, 42] - ); - - $mock->foo('bar'); - - $this->expectException(ExpectationFailedException::class); - - $mock->foo('invalid'); - } - - public function testIntegrationFailsWithNonIterableParameterGroup(): void - { - $mock = $this->getMockBuilder(stdClass::class) - ->addMethods(['foo']) - ->getMock(); - - $this->expectException(InvalidParameterGroupException::class); - $this->expectExceptionMessage('Parameter group #1 must be an array or Traversable, got object'); - - $mock->expects($this->any()) - ->method('foo') - ->withConsecutive( - ['bar'], - $this->identicalTo([21, 42]) // this is not an array - ); - } -} diff --git a/tests/unit/Framework/MockObject/MockObjectTest.php b/tests/unit/Framework/MockObject/MockObjectTest.php index b1afa67d4d4..6c226123164 100644 --- a/tests/unit/Framework/MockObject/MockObjectTest.php +++ b/tests/unit/Framework/MockObject/MockObjectTest.php @@ -319,29 +319,6 @@ public function testStubbedReturnSelf(): void $this->assertEquals($mock, $mock->doSomething()); } - public function testStubbedReturnOnConsecutiveCalls(): void - { - $mock = $this->getMockBuilder(AnInterface::class) - ->getMock(); - - $mock->method('doSomething') - ->will($this->onConsecutiveCalls('a', 'b', 'c')); - - $this->assertEquals('a', $mock->doSomething()); - $this->assertEquals('b', $mock->doSomething()); - $this->assertEquals('c', $mock->doSomething()); - - $mock = $this->getMockBuilder(AnInterface::class) - ->getMock(); - - $mock->method('doSomething') - ->willReturnOnConsecutiveCalls('a', 'b', 'c'); - - $this->assertEquals('a', $mock->doSomething()); - $this->assertEquals('b', $mock->doSomething()); - $this->assertEquals('c', $mock->doSomething()); - } - public function testStaticMethodCallback(): void { $mock = $this->getMockBuilder(SomeClass::class)