Skip to content

Commit

Permalink
Warn of non-existent methods in createPartialMock
Browse files Browse the repository at this point in the history
  • Loading branch information
DFoxinator committed May 15, 2019
1 parent 29f04eb commit 9c4c1f7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Framework/TestCase.php
Expand Up @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions tests/_files/TestWithDifferentStatuses.php
Expand Up @@ -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());
}
}
20 changes: 20 additions & 0 deletions tests/unit/Framework/TestCaseTest.php
Expand Up @@ -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 */
Expand Down

0 comments on commit 9c4c1f7

Please sign in to comment.