X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Ftests%2FDrupal%2FTests%2FPhpunitCompatibilityTrait.php;fp=web%2Fcore%2Ftests%2FDrupal%2FTests%2FPhpunitCompatibilityTrait.php;h=5cf020a8df6b999663a06aa624779f200eea6f46;hp=0000000000000000000000000000000000000000;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/web/core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php b/web/core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php new file mode 100644 index 000000000..5cf020a8d --- /dev/null +++ b/web/core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php @@ -0,0 +1,142 @@ +supports('getMock')) { + $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(); + } + else { + return parent::getMock($originalClassName, $methods, $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload, $cloneArguments, $callOriginalMethods, $proxyTarget); + } + } + + /** + * Returns a mock object for the specified class using the available method. + * + * The createMock method does not exist in PHPUnit 4. To provide forward + * compatibility this trait provides the createMock method and uses createMock + * if this method is available on the parent class or falls back to getMock if + * it isn't. + * + * @param string $originalClassName + * Name of the class to mock. + * + * @see \PHPUnit_Framework_TestCase::getMock + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function createMock($originalClassName) { + if ($this->supports('createMock')) { + return parent::createMock($originalClassName); + } + else { + return $this->getMock($originalClassName, [], [], '', FALSE, FALSE); + } + } + + /** + * Checks if the trait is used in a class that has a method. + * + * @param string $method + * Method to check. + * + * @return bool + * TRUE if the method is supported, FALSE if not. + */ + private function supports($method) { + // Get the parent class of the currently running test class. + $parent = get_parent_class($this); + // Ensure that the method_exists() check on the createMock method is carried + // out on the first parent of $this that does not have access to this + // trait's methods. This is because the trait also has a method called + // createMock(). Most often the check will be made on + // \PHPUnit\Framework\TestCase. + while (method_exists($parent, 'supports')) { + $parent = get_parent_class($parent); + } + return method_exists($parent, $method); + } + +}