Commit d1ffb5bd authored by alexpott's avatar alexpott

Issue #3053417 by amateescu, Lendude, mondrake, xjm: Clean up PhpunitCompatibilityTrait

parent 0356445b
......@@ -50,71 +50,42 @@ trait PhpunitCompatibilityTrait {
*/
public function getMock($originalClassName, $methods = [], array $arguments = [], $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE, $cloneArguments = FALSE, $callOriginalMethods = FALSE, $proxyTarget = NULL) {
@trigger_error('\Drupal\Tests\PhpunitCompatibilityTrait::getMock() is deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use \Drupal\Tests\PhpunitCompatibilityTrait::createMock() instead. See https://www.drupal.org/node/2907725', E_USER_DEPRECATED);
if (!$this->supports('getMock')) {
$mock = $this->getMockBuilder($originalClassName)
->setMethods($methods)
->setConstructorArgs($arguments)
->setMockClassName($mockClassName)
->setProxyTarget($proxyTarget);
if ($callOriginalConstructor) {
$mock->enableOriginalConstructor();
}
else {
$mock->disableOriginalConstructor();
}
if ($callOriginalClone) {
$mock->enableOriginalClone();
}
else {
$mock->disableOriginalClone();
}
if ($callAutoload) {
$mock->enableAutoload();
}
else {
$mock->disableAutoload();
}
if ($cloneArguments) {
$mock->enableArgumentCloning();
}
else {
$mock->disableArgumentCloning();
}
if ($callOriginalMethods) {
$mock->enableProxyingToOriginalMethods();
}
else {
$mock->disableProxyingToOriginalMethods();
}
return $mock->getMock();
$mock = $this->getMockBuilder($originalClassName)
->setMethods($methods)
->setConstructorArgs($arguments)
->setMockClassName($mockClassName)
->setProxyTarget($proxyTarget);
if ($callOriginalConstructor) {
$mock->enableOriginalConstructor();
}
else {
return parent::getMock($originalClassName, $methods, $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload, $cloneArguments, $callOriginalMethods, $proxyTarget);
$mock->disableOriginalConstructor();
}
}
/**
* Returns a mock object for the specified class using the available method.
*
* The createMock method does not exist in PHPUnit 4. To provide forward
* compatibility this trait provides the createMock method and uses createMock
* if this method is available on the parent class or falls back to getMock if
* it isn't.
*
* @param string $originalClassName
* Name of the class to mock.
*
* @see \PHPUnit_Framework_TestCase::getMock
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function createMock($originalClassName) {
if ($this->supports('createMock')) {
return parent::createMock($originalClassName);
if ($callOriginalClone) {
$mock->enableOriginalClone();
}
else {
$mock->disableOriginalClone();
}
if ($callAutoload) {
$mock->enableAutoload();
}
else {
$mock->disableAutoload();
}
if ($cloneArguments) {
$mock->enableArgumentCloning();
}
else {
return $this->getMock($originalClassName, [], [], '', FALSE, FALSE);
$mock->disableArgumentCloning();
}
if ($callOriginalMethods) {
$mock->enableProxyingToOriginalMethods();
}
else {
$mock->disableProxyingToOriginalMethods();
}
return $mock->getMock();
}
/**
......@@ -128,41 +99,13 @@ public function createMock($originalClassName) {
* The expected exception code.
*/
public function setExpectedException($class, $message = '', $exception_code = NULL) {
if (method_exists($this, 'expectException')) {
$this->expectException($class);
if (!empty($message)) {
$this->expectExceptionMessage($message);
}
if ($exception_code !== NULL) {
$this->expectExceptionCode($exception_code);
}
$this->expectException($class);
if (!empty($message)) {
$this->expectExceptionMessage($message);
}
else {
parent::setExpectedException($class, $message, $exception_code);
}
}
/**
* Checks if the trait is used in a class that has a method.
*
* @param string $method
* Method to check.
*
* @return bool
* TRUE if the method is supported, FALSE if not.
*/
private function supports($method) {
// Get the parent class of the currently running test class.
$parent = get_parent_class($this);
// Ensure that the method_exists() check on the createMock method is carried
// out on the first parent of $this that does not have access to this
// trait's methods. This is because the trait also has a method called
// createMock(). Most often the check will be made on
// \PHPUnit\Framework\TestCase.
while (method_exists($parent, 'supports')) {
$parent = get_parent_class($parent);
if ($exception_code !== NULL) {
$this->expectExceptionCode($exception_code);
}
return method_exists($parent, $method);
}
}
......@@ -11,132 +11,18 @@
class PhpunitCompatibilityTraitTest extends UnitTestCase {
/**
* Tests that getMock is available and calls the correct parent method.
* Tests that getMock is available.
*
* @covers ::getMock
* @dataProvider providerMockVersions
* @group legacy
* @expectedDeprecation \Drupal\Tests\PhpunitCompatibilityTrait::getMock() is deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use \Drupal\Tests\PhpunitCompatibilityTrait::createMock() instead. See https://www.drupal.org/node/2907725
*/
public function testGetMock($className, $expected) {
$class = new $className();
$this->assertSame($expected, $class->getMock($this->randomMachineName()));
}
/**
* Tests that createMock is available and calls the correct parent method.
*
* @covers ::createMock
* @dataProvider providerMockVersions
*/
public function testCreateMock($className, $expected) {
$class = new $className();
$this->assertSame($expected, $class->createMock($this->randomMachineName()));
}
/**
* Tests that createMock is available and calls the correct parent method.
*
* @covers ::createMock
* @dataProvider providerMockLegacyVersions
* @group legacy
* @expectedDeprecation \Drupal\Tests\PhpunitCompatibilityTrait::getMock() is deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use \Drupal\Tests\PhpunitCompatibilityTrait::createMock() instead. See https://www.drupal.org/node/2907725
*/
public function testLegacyCreateMock($className, $expected) {
$this->testCreateMock($className, $expected);
}
/**
* Returns the class names and the string they return.
*
* @return array
*/
public function providerMockVersions() {
return [
[UnitTestCasePhpunit6TestClass::class, 'PHPUnit 6'],
[UnitTestCasePhpunit6TestClassExtends::class, 'PHPUnit 6'],
];
}
/**
* Returns the class names and the string they return.
*
* @return array
*/
public function providerMockLegacyVersions() {
return [
[UnitTestCasePhpunit4TestClass::class, 'PHPUnit 4'],
[UnitTestCasePhpunit4TestClassExtends::class, 'PHPUnit 4'],
];
}
}
/**
* Test class for \PHPUnit\Framework\TestCase in PHPUnit 4.
*/
class Phpunit4TestClass {
public function getMock($originalClassName) {
return 'PHPUnit 4';
}
}
/**
* Test class for \PHPUnit\Framework\TestCase in PHPUnit 6.
*/
class Phpunit6TestClass {
public function createMock($originalClassName) {
return 'PHPUnit 6';
}
public function getMockbuilder() {
return new Mockbuilder();
public function testGetMock() {
$this->assertInstanceOf('\Drupal\Tests\MockTestClassInterface', $this->getMock(MockTestClassInterface::class));
}
}
/**
* Test double for PHPUnit_Framework_MockObject_MockBuilder.
*/
class Mockbuilder {
public function __call($name, $arguments) {
return $this;
}
public function getMock() {
return 'PHPUnit 6';
}
}
interface MockTestClassInterface {
/**
* Test class for \Drupal\Tests\UnitTestCase with PHPUnit 4.
*/
class UnitTestCasePhpunit4TestClass extends Phpunit4TestClass {
use PhpunitCompatibilityTrait;
}
/**
* Test class for \Drupal\Tests\UnitTestCase with PHPUnit 4.
*/
class UnitTestCasePhpunit4TestClassExtends extends UnitTestCasePhpunit4TestClass {
}
/**
* Test class for \Drupal\Tests\UnitTestCase with PHPUnit 6.
*/
class UnitTestCasePhpunit6TestClass extends Phpunit6TestClass {
use PhpunitCompatibilityTrait;
}
/**
* Test class for \Drupal\Tests\UnitTestCase with PHPUnit 6.
*/
class UnitTestCasePhpunit6TestClassExtends extends UnitTestCasePhpunit6TestClass {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment