Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/Framework/MockObject/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class PHPUnit_Framework_MockObject_Generator
/**
* @var array
*/
protected $blacklistedMethodNames = [
protected $legacyBlacklistedMethodNames = [
'__CLASS__' => true,
'__DIR__' => true,
'__FILE__' => true,
Expand Down Expand Up @@ -112,6 +112,22 @@ class PHPUnit_Framework_MockObject_Generator
'xor' => true
];

/**
* @var array
*/
protected $blacklistedMethodNames = [
'__CLASS__' => true,
'__DIR__' => true,
'__FILE__' => true,
'__FUNCTION__' => true,
'__LINE__' => true,
'__METHOD__' => true,
'__NAMESPACE__' => true,
'__TRAIT__' => true,
'__clone' => true,
'__halt_compiler' => true,
];

/**
* Returns a mock object for the specified class.
*
Expand Down Expand Up @@ -998,13 +1014,34 @@ protected function canMockMethod(ReflectionMethod $method)
if ($method->isConstructor() ||
$method->isFinal() ||
$method->isPrivate() ||
isset($this->blacklistedMethodNames[$method->getName()])) {
$this->isMethodNameBlacklisted($method->getName())) {
return false;
}

return true;
}

/**
* Returns whether i method name is blacklisted
*
* Since PHP 7 the only names that are still reserved for method names are the ones that start with an underscore
*
* @param string $name
* @return boolean
*/
protected function isMethodNameBlacklisted($name)
{
if (PHP_MAJOR_VERSION < 7 && isset($this->legacyBlacklistedMethodNames[$name])) {
return true;
}

if (PHP_MAJOR_VERSION >= 7 && isset($this->blacklistedMethodNames[$name])) {
return true;
}

return false;
}

/**
* Returns the parameters of a function or method.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ public function testGetMockGeneratorFails()
$mock = $this->generator->getMock('StdClass', ['foo', 'foo']);
}

/**
* @covers PHPUnit_Framework_MockObject_Generator::getMock
* @covers PHPUnit_Framework_MockObject_Generator::isMethodNameBlacklisted
*/
public function testGetMockBlacklistedMethodNamesPhp7()
{
if (PHP_MAJOR_VERSION < 7) {
$this->markTestSkipped('PHP >= 7.0.0 required');

return;
}

// Probably, this should be moved to tests/autoload.php
require_once __DIR__ . '/_fixture/InterfaceWithSemiReservedMethodName.php';

$mock = $this->generator->getMock('InterfaceWithSemiReservedMethodName');

$this->assertTrue(method_exists($mock, 'unset'));
$this->assertInstanceOf('InterfaceWithSemiReservedMethodName', $mock);
}

/**
* @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/_fixture/InterfaceWithSemiReservedMethodName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
interface InterfaceWithSemiReservedMethodName
{
public function unset();
}