Security update for Core, with self-updated composer
[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(), array $arguments = array(), $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    * Checks if the trait is used in a class that has a method.
121    *
122    * @param string $method
123    *   Method to check.
124    *
125    * @return bool
126    *   TRUE if the method is supported, FALSE if not.
127    */
128   private function supports($method) {
129     // Get the parent class of the currently running test class.
130     $parent = get_parent_class($this);
131     // Ensure that the method_exists() check on the createMock method is carried
132     // out on the first parent of $this that does not have access to this
133     // trait's methods. This is because the trait also has a method called
134     // createMock(). Most often the check will be made on
135     // \PHPUnit\Framework\TestCase.
136     while (method_exists($parent, 'supports')) {
137       $parent = get_parent_class($parent);
138     }
139     return method_exists($parent, $method);
140   }
141
142 }