3 namespace Drupal\Tests;
6 * Makes Drupal's test API forward compatible with multiple versions of PHPUnit.
8 trait PhpunitCompatibilityTrait {
11 * Returns a mock object for the specified class using the available method.
13 * The getMock method does not exist in PHPUnit 6. To provide backward
14 * compatibility this trait provides the getMock method and uses createMock if
15 * this method is available on the parent class.
17 * @param string $originalClassName
18 * Name of the class to mock.
19 * @param array|null $methods
20 * When provided, only methods whose names are in the array are replaced
21 * with a configurable test double. The behavior of the other methods is not
22 * changed. Providing null means that no methods will be replaced.
23 * @param array $arguments
24 * Parameters to pass to the original class' constructor.
25 * @param string $mockClassName
26 * Class name for the generated test double class.
27 * @param bool $callOriginalConstructor
28 * Can be used to disable the call to the original class' constructor.
29 * @param bool $callOriginalClone
30 * Can be used to disable the call to the original class' clone constructor.
31 * @param bool $callAutoload
32 * Can be used to disable __autoload() during the generation of the test
34 * @param bool $cloneArguments
35 * Enables the cloning of arguments passed to mocked methods.
36 * @param bool $callOriginalMethods
37 * Enables the invocation of the original methods.
38 * @param object $proxyTarget
39 * Sets the proxy target.
41 * @see \PHPUnit_Framework_TestCase::getMock
42 * @see https://github.com/sebastianbergmann/phpunit/wiki/Release-Announcement-for-PHPUnit-5.4.0
44 * @return \PHPUnit_Framework_MockObject_MockObject
46 * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0.
47 * Use \Drupal\Tests\PhpunitCompatibilityTrait::createMock() instead.
49 * @see https://www.drupal.org/node/2907725
51 public function getMock($originalClassName, $methods = [], array $arguments = [], $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE, $cloneArguments = FALSE, $callOriginalMethods = FALSE, $proxyTarget = NULL) {
52 if (!$this->supports('getMock')) {
53 $mock = $this->getMockBuilder($originalClassName)
54 ->setMethods($methods)
55 ->setConstructorArgs($arguments)
56 ->setMockClassName($mockClassName)
57 ->setProxyTarget($proxyTarget);
58 if ($callOriginalConstructor) {
59 $mock->enableOriginalConstructor();
62 $mock->disableOriginalConstructor();
64 if ($callOriginalClone) {
65 $mock->enableOriginalClone();
68 $mock->disableOriginalClone();
71 $mock->enableAutoload();
74 $mock->disableAutoload();
76 if ($cloneArguments) {
77 $mock->enableArgumentCloning();
80 $mock->disableArgumentCloning();
82 if ($callOriginalMethods) {
83 $mock->enableProxyingToOriginalMethods();
86 $mock->disableProxyingToOriginalMethods();
88 return $mock->getMock();
91 return parent::getMock($originalClassName, $methods, $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload, $cloneArguments, $callOriginalMethods, $proxyTarget);
96 * Returns a mock object for the specified class using the available method.
98 * The createMock method does not exist in PHPUnit 4. To provide forward
99 * compatibility this trait provides the createMock method and uses createMock
100 * if this method is available on the parent class or falls back to getMock if
103 * @param string $originalClassName
104 * Name of the class to mock.
106 * @see \PHPUnit_Framework_TestCase::getMock
108 * @return \PHPUnit_Framework_MockObject_MockObject
110 public function createMock($originalClassName) {
111 if ($this->supports('createMock')) {
112 return parent::createMock($originalClassName);
115 return $this->getMock($originalClassName, [], [], '', FALSE, FALSE);
120 * Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
122 * @param mixed $class
123 * The expected exception class.
124 * @param string $message
125 * The expected exception message.
126 * @param int $exception_code
127 * The expected exception code.
129 public function setExpectedException($class, $message = '', $exception_code = NULL) {
130 if (method_exists($this, 'expectException')) {
131 $this->expectException($class);
132 if (!empty($message)) {
133 $this->expectExceptionMessage($message);
135 if ($exception_code !== NULL) {
136 $this->expectExceptionCode($exception_code);
140 parent::setExpectedException($class, $message, $exception_code);
145 * Checks if the trait is used in a class that has a method.
147 * @param string $method
151 * TRUE if the method is supported, FALSE if not.
153 private function supports($method) {
154 // Get the parent class of the currently running test class.
155 $parent = get_parent_class($this);
156 // Ensure that the method_exists() check on the createMock method is carried
157 // out on the first parent of $this that does not have access to this
158 // trait's methods. This is because the trait also has a method called
159 // createMock(). Most often the check will be made on
160 // \PHPUnit\Framework\TestCase.
161 while (method_exists($parent, 'supports')) {
162 $parent = get_parent_class($parent);
164 return method_exists($parent, $method);