From 9c4c1f71e87f954246266d774c76e37ac2fb8cd9 Mon Sep 17 00:00:00 2001 From: David Fox Date: Wed, 15 May 2019 11:30:54 -0400 Subject: [PATCH] Warn of non-existent methods in createPartialMock --- src/Framework/TestCase.php | 16 ++++++++++++++++ tests/_files/TestWithDifferentStatuses.php | 11 +++++++++++ tests/unit/Framework/TestCaseTest.php | 20 ++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index 7c554249f82..ccdcbaedaf2 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -1388,6 +1388,22 @@ protected function createConfiguredMock($originalClassName, array $configuration */ protected function createPartialMock($originalClassName, array $methods): MockObject { + $reflection = new \ReflectionClass($originalClassName); + + $mockedMethodsThatDontExist = \array_filter($methods, function (string $method) use ($reflection) { + return !$reflection->hasMethod($method); + }); + + if ($mockedMethodsThatDontExist) { + $this->addWarning( + \sprintf( + 'createPartialMock called with method(s) %s that do not exist in %s. This will not be allowed in future versions of PHPUnit.', + \implode(', ', $mockedMethodsThatDontExist), + $originalClassName + ) + ); + } + try { return $this->getMockBuilder($originalClassName) ->disableOriginalConstructor() diff --git a/tests/_files/TestWithDifferentStatuses.php b/tests/_files/TestWithDifferentStatuses.php index e680f6ce2d3..da170332349 100644 --- a/tests/_files/TestWithDifferentStatuses.php +++ b/tests/_files/TestWithDifferentStatuses.php @@ -45,4 +45,15 @@ public function testThatAddsAWarning(): void { $this->addWarning('Sorry, Dave!'); } + + public function testWithCreatePartialMockWarning(): void + { + $this->createPartialMock(\Mockable::class, ['mockableMethod', 'fakeMethod1', 'fakeMethod2']); + } + + public function testWithCreatePartialMockPassesNoWarning(): void + { + $mock = $this->createPartialMock(\Mockable::class, ['mockableMethod']); + $this->assertNull($mock->mockableMethod()); + } } diff --git a/tests/unit/Framework/TestCaseTest.php b/tests/unit/Framework/TestCaseTest.php index 738f60ce082..d410cd8d5ad 100644 --- a/tests/unit/Framework/TestCaseTest.php +++ b/tests/unit/Framework/TestCaseTest.php @@ -856,6 +856,26 @@ public function testCreatePartialMockCanMockNoMethods(): void $this->assertTrue($mock->anotherMockableMethod()); } + public function testCreatePartialMockWithFakeMethods(): void + { + $test = new \TestWithDifferentStatuses('testWithCreatePartialMockWarning'); + + $test->run(); + + $this->assertSame(BaseTestRunner::STATUS_WARNING, $test->getStatus()); + $this->assertFalse($test->hasFailed()); + } + + public function testCreatePartialMockWithRealMethods(): void + { + $test = new \TestWithDifferentStatuses('testWithCreatePartialMockPassesNoWarning'); + + $test->run(); + + $this->assertSame(BaseTestRunner::STATUS_PASSED, $test->getStatus()); + $this->assertFalse($test->hasFailed()); + } + public function testCreateMockSkipsConstructor(): void { /** @var \Mockable $mock */