You are here

public function PhpunitCompatibilityTrait::getMock in Drupal 8

Returns a mock object for the specified class using the available method.

The getMock method does not exist in PHPUnit 6. To provide backward compatibility this trait provides the getMock method and uses createMock if this method is available on the parent class.

Parameters

string $originalClassName: Name of the class to mock.

array|null $methods: When provided, only methods whose names are in the array are replaced with a configurable test double. The behavior of the other methods is not changed. Providing null means that no methods will be replaced.

array $arguments: Parameters to pass to the original class' constructor.

string $mockClassName: Class name for the generated test double class.

bool $callOriginalConstructor: Can be used to disable the call to the original class' constructor.

bool $callOriginalClone: Can be used to disable the call to the original class' clone constructor.

bool $callAutoload: Can be used to disable __autoload() during the generation of the test double class.

bool $cloneArguments: Enables the cloning of arguments passed to mocked methods.

bool $callOriginalMethods: Enables the invocation of the original methods.

object $proxyTarget: Sets the proxy target.

Return value

\PHPUnit\Framework\MockObject\MockObject

Deprecated

in drupal:8.5.0 and is removed from drupal:9.0.0. Use \Drupal\Tests\PhpunitCompatibilityTrait::createMock() instead.

See also

https://github.com/sebastianbergmann/phpunit/wiki/Release-Announcement-f...

https://www.drupal.org/node/2907725

1 call to PhpunitCompatibilityTrait::getMock()
PhpunitCompatibilityTraitTest::testGetMock in core/tests/Drupal/Tests/PhpunitCompatibilityTraitTest.php
Tests that getMock is available.

File

core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php, line 61

Class

PhpunitCompatibilityTrait
Makes Drupal's test API forward compatible with multiple versions of PHPUnit.

Namespace

Drupal\Tests

Code

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