PhpunitCompatibilityTrait.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Drupal\Tests;
  3. use Drupal\TestTools\PhpUnitCompatibility\RunnerVersion;
  4. // In order to manage different method signatures between PHPUnit versions, we
  5. // dynamically load a compatibility trait dependent on the PHPUnit runner
  6. // version.
  7. if (!trait_exists(PhpunitVersionDependentTestCompatibilityTrait::class, FALSE)) {
  8. class_alias("Drupal\TestTools\PhpUnitCompatibility\PhpUnit" . RunnerVersion::getMajor() . "\TestCompatibilityTrait", PhpunitVersionDependentTestCompatibilityTrait::class);
  9. }
  10. /**
  11. * Makes Drupal's test API forward compatible with multiple versions of PHPUnit.
  12. */
  13. trait PhpunitCompatibilityTrait {
  14. use PhpunitVersionDependentTestCompatibilityTrait;
  15. /**
  16. * Returns a mock object for the specified class using the available method.
  17. *
  18. * The getMock method does not exist in PHPUnit 6. To provide backward
  19. * compatibility this trait provides the getMock method and uses createMock if
  20. * this method is available on the parent class.
  21. *
  22. * @param string $originalClassName
  23. * Name of the class to mock.
  24. * @param array|null $methods
  25. * When provided, only methods whose names are in the array are replaced
  26. * with a configurable test double. The behavior of the other methods is not
  27. * changed. Providing null means that no methods will be replaced.
  28. * @param array $arguments
  29. * Parameters to pass to the original class' constructor.
  30. * @param string $mockClassName
  31. * Class name for the generated test double class.
  32. * @param bool $callOriginalConstructor
  33. * Can be used to disable the call to the original class' constructor.
  34. * @param bool $callOriginalClone
  35. * Can be used to disable the call to the original class' clone constructor.
  36. * @param bool $callAutoload
  37. * Can be used to disable __autoload() during the generation of the test
  38. * double class.
  39. * @param bool $cloneArguments
  40. * Enables the cloning of arguments passed to mocked methods.
  41. * @param bool $callOriginalMethods
  42. * Enables the invocation of the original methods.
  43. * @param object $proxyTarget
  44. * Sets the proxy target.
  45. *
  46. * @see https://github.com/sebastianbergmann/phpunit/wiki/Release-Announcement-for-PHPUnit-5.4.0
  47. *
  48. * @return \PHPUnit\Framework\MockObject\MockObject
  49. *
  50. * @deprecated in drupal:8.5.0 and is removed from drupal:9.0.0.
  51. * Use \Drupal\Tests\PhpunitCompatibilityTrait::createMock() instead.
  52. *
  53. * @see https://www.drupal.org/node/2907725
  54. */
  55. public function getMock($originalClassName, $methods = [], array $arguments = [], $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE, $cloneArguments = FALSE, $callOriginalMethods = FALSE, $proxyTarget = NULL) {
  56. @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);
  57. $mock = $this->getMockBuilder($originalClassName)
  58. ->setMethods($methods)
  59. ->setConstructorArgs($arguments)
  60. ->setMockClassName($mockClassName)
  61. ->setProxyTarget($proxyTarget);
  62. if ($callOriginalConstructor) {
  63. $mock->enableOriginalConstructor();
  64. }
  65. else {
  66. $mock->disableOriginalConstructor();
  67. }
  68. if ($callOriginalClone) {
  69. $mock->enableOriginalClone();
  70. }
  71. else {
  72. $mock->disableOriginalClone();
  73. }
  74. if ($callAutoload) {
  75. $mock->enableAutoload();
  76. }
  77. else {
  78. $mock->disableAutoload();
  79. }
  80. if ($cloneArguments) {
  81. $mock->enableArgumentCloning();
  82. }
  83. else {
  84. $mock->disableArgumentCloning();
  85. }
  86. if ($callOriginalMethods) {
  87. $mock->enableProxyingToOriginalMethods();
  88. }
  89. else {
  90. $mock->disableProxyingToOriginalMethods();
  91. }
  92. return $mock->getMock();
  93. }
  94. /**
  95. * Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
  96. *
  97. * @param mixed $class
  98. * The expected exception class.
  99. * @param string $message
  100. * The expected exception message.
  101. * @param int $exception_code
  102. * The expected exception code.
  103. *
  104. * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0.
  105. * Backward compatibility for PHPUnit 4 will no longer be supported.
  106. *
  107. * @see https://www.drupal.org/node/3056869
  108. */
  109. public function setExpectedException($class, $message = '', $exception_code = NULL) {
  110. @trigger_error('\Drupal\Tests\PhpunitCompatibilityTrait:setExpectedException() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Backward compatibility for PHPUnit 4 will no longer be supported. See https://www.drupal.org/node/3056869', E_USER_DEPRECATED);
  111. $this->expectException($class);
  112. if (!empty($message)) {
  113. $this->expectExceptionMessage($message);
  114. }
  115. if ($exception_code !== NULL) {
  116. $this->expectExceptionCode($exception_code);
  117. }
  118. }
  119. }