Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / tests / Drupal / Tests / PhpunitCompatibilityTrait.php
1 <?php
2
3 namespace Drupal\Tests;
4
5 /**
6  * Makes Drupal's test API forward compatible with multiple versions of PHPUnit.
7  */
8 trait PhpunitCompatibilityTrait {
9
10   /**
11    * Returns a mock object for the specified class using the available method.
12    *
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.
16    *
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
33    *   double class.
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.
40    *
41    * @see \PHPUnit_Framework_TestCase::getMock
42    * @see https://github.com/sebastianbergmann/phpunit/wiki/Release-Announcement-for-PHPUnit-5.4.0
43    *
44    * @return \PHPUnit_Framework_MockObject_MockObject
45    *
46    * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0.
47    *   Use \Drupal\Tests\PhpunitCompatibilityTrait::createMock() instead.
48    *
49    * @see https://www.drupal.org/node/2907725
50    */
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();
60       }
61       else {
62         $mock->disableOriginalConstructor();
63       }
64       if ($callOriginalClone) {
65         $mock->enableOriginalClone();
66       }
67       else {
68         $mock->disableOriginalClone();
69       }
70       if ($callAutoload) {
71         $mock->enableAutoload();
72       }
73       else {
74         $mock->disableAutoload();
75       }
76       if ($cloneArguments) {
77         $mock->enableArgumentCloning();
78       }
79       else {
80         $mock->disableArgumentCloning();
81       }
82       if ($callOriginalMethods) {
83         $mock->enableProxyingToOriginalMethods();
84       }
85       else {
86         $mock->disableProxyingToOriginalMethods();
87       }
88       return $mock->getMock();
89     }
90     else {
91       return parent::getMock($originalClassName, $methods, $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload, $cloneArguments, $callOriginalMethods, $proxyTarget);
92     }
93   }
94
95   /**
96    * Returns a mock object for the specified class using the available method.
97    *
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
101    * it isn't.
102    *
103    * @param string $originalClassName
104    *   Name of the class to mock.
105    *
106    * @see \PHPUnit_Framework_TestCase::getMock
107    *
108    * @return \PHPUnit_Framework_MockObject_MockObject
109    */
110   public function createMock($originalClassName) {
111     if ($this->supports('createMock')) {
112       return parent::createMock($originalClassName);
113     }
114     else {
115       return $this->getMock($originalClassName, [], [], '', FALSE, FALSE);
116     }
117   }
118
119   /**
120    * Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
121    *
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.
128    */
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);
134       }
135       if ($exception_code !== NULL) {
136         $this->expectExceptionCode($exception_code);
137       }
138     }
139     else {
140       parent::setExpectedException($class, $message, $exception_code);
141     }
142   }
143
144   /**
145    * Checks if the trait is used in a class that has a method.
146    *
147    * @param string $method
148    *   Method to check.
149    *
150    * @return bool
151    *   TRUE if the method is supported, FALSE if not.
152    */
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);
163     }
164     return method_exists($parent, $method);
165   }
166
167 }