Unverified Commit d1ffb5bd authored by Alex Pott's avatar Alex Pott
Browse files

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

parent 0356445b
Loading
Loading
Loading
Loading
+35 −92
Original line number Diff line number Diff line
@@ -50,7 +50,6 @@ 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)
@@ -88,34 +87,6 @@ public function getMock($originalClassName, $methods = [], array $arguments = []
    }
    return $mock->getMock();
  }
    else {
      return parent::getMock($originalClassName, $methods, $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload, $cloneArguments, $callOriginalMethods, $proxyTarget);
    }
  }

  /**
   * 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);
    }
    else {
      return $this->getMock($originalClassName, [], [], '', FALSE, FALSE);
    }
  }

  /**
   * Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
@@ -128,7 +99,6 @@ 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);
@@ -137,32 +107,5 @@ public function setExpectedException($class, $message = '', $exception_code = NU
      $this->expectExceptionCode($exception_code);
    }
  }
    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);
    }
    return method_exists($parent, $method);
  }

}
+4 −118
Original line number Diff line number Diff line
@@ -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()));
  public function testGetMock() {
    $this->assertInstanceOf('\Drupal\Tests\MockTestClassInterface', $this->getMock(MockTestClassInterface::class));
  }

  /**
   * 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();
  }

}

/**
 * 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 {
}