annotate core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests;
Chris@0 4
Chris@0 5 /**
Chris@0 6 * Makes Drupal's test API forward compatible with multiple versions of PHPUnit.
Chris@0 7 */
Chris@0 8 trait PhpunitCompatibilityTrait {
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Returns a mock object for the specified class using the available method.
Chris@0 12 *
Chris@0 13 * The getMock method does not exist in PHPUnit 6. To provide backward
Chris@0 14 * compatibility this trait provides the getMock method and uses createMock if
Chris@0 15 * this method is available on the parent class.
Chris@0 16 *
Chris@0 17 * @param string $originalClassName
Chris@0 18 * Name of the class to mock.
Chris@0 19 * @param array|null $methods
Chris@0 20 * When provided, only methods whose names are in the array are replaced
Chris@0 21 * with a configurable test double. The behavior of the other methods is not
Chris@0 22 * changed. Providing null means that no methods will be replaced.
Chris@0 23 * @param array $arguments
Chris@0 24 * Parameters to pass to the original class' constructor.
Chris@0 25 * @param string $mockClassName
Chris@0 26 * Class name for the generated test double class.
Chris@0 27 * @param bool $callOriginalConstructor
Chris@0 28 * Can be used to disable the call to the original class' constructor.
Chris@0 29 * @param bool $callOriginalClone
Chris@0 30 * Can be used to disable the call to the original class' clone constructor.
Chris@0 31 * @param bool $callAutoload
Chris@0 32 * Can be used to disable __autoload() during the generation of the test
Chris@0 33 * double class.
Chris@0 34 * @param bool $cloneArguments
Chris@0 35 * Enables the cloning of arguments passed to mocked methods.
Chris@0 36 * @param bool $callOriginalMethods
Chris@0 37 * Enables the invocation of the original methods.
Chris@0 38 * @param object $proxyTarget
Chris@0 39 * Sets the proxy target.
Chris@0 40 *
Chris@0 41 * @see \PHPUnit_Framework_TestCase::getMock
Chris@0 42 * @see https://github.com/sebastianbergmann/phpunit/wiki/Release-Announcement-for-PHPUnit-5.4.0
Chris@0 43 *
Chris@0 44 * @return \PHPUnit_Framework_MockObject_MockObject
Chris@0 45 *
Chris@0 46 * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0.
Chris@0 47 * Use \Drupal\Tests\PhpunitCompatibilityTrait::createMock() instead.
Chris@0 48 *
Chris@0 49 * @see https://www.drupal.org/node/2907725
Chris@0 50 */
Chris@0 51 public function getMock($originalClassName, $methods = [], array $arguments = [], $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE, $cloneArguments = FALSE, $callOriginalMethods = FALSE, $proxyTarget = NULL) {
Chris@0 52 if (!$this->supports('getMock')) {
Chris@0 53 $mock = $this->getMockBuilder($originalClassName)
Chris@0 54 ->setMethods($methods)
Chris@0 55 ->setConstructorArgs($arguments)
Chris@0 56 ->setMockClassName($mockClassName)
Chris@0 57 ->setProxyTarget($proxyTarget);
Chris@0 58 if ($callOriginalConstructor) {
Chris@0 59 $mock->enableOriginalConstructor();
Chris@0 60 }
Chris@0 61 else {
Chris@0 62 $mock->disableOriginalConstructor();
Chris@0 63 }
Chris@0 64 if ($callOriginalClone) {
Chris@0 65 $mock->enableOriginalClone();
Chris@0 66 }
Chris@0 67 else {
Chris@0 68 $mock->disableOriginalClone();
Chris@0 69 }
Chris@0 70 if ($callAutoload) {
Chris@0 71 $mock->enableAutoload();
Chris@0 72 }
Chris@0 73 else {
Chris@0 74 $mock->disableAutoload();
Chris@0 75 }
Chris@0 76 if ($cloneArguments) {
Chris@0 77 $mock->enableArgumentCloning();
Chris@0 78 }
Chris@0 79 else {
Chris@0 80 $mock->disableArgumentCloning();
Chris@0 81 }
Chris@0 82 if ($callOriginalMethods) {
Chris@0 83 $mock->enableProxyingToOriginalMethods();
Chris@0 84 }
Chris@0 85 else {
Chris@0 86 $mock->disableProxyingToOriginalMethods();
Chris@0 87 }
Chris@0 88 return $mock->getMock();
Chris@0 89 }
Chris@0 90 else {
Chris@0 91 return parent::getMock($originalClassName, $methods, $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload, $cloneArguments, $callOriginalMethods, $proxyTarget);
Chris@0 92 }
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * Returns a mock object for the specified class using the available method.
Chris@0 97 *
Chris@0 98 * The createMock method does not exist in PHPUnit 4. To provide forward
Chris@0 99 * compatibility this trait provides the createMock method and uses createMock
Chris@0 100 * if this method is available on the parent class or falls back to getMock if
Chris@0 101 * it isn't.
Chris@0 102 *
Chris@0 103 * @param string $originalClassName
Chris@0 104 * Name of the class to mock.
Chris@0 105 *
Chris@0 106 * @see \PHPUnit_Framework_TestCase::getMock
Chris@0 107 *
Chris@0 108 * @return \PHPUnit_Framework_MockObject_MockObject
Chris@0 109 */
Chris@0 110 public function createMock($originalClassName) {
Chris@0 111 if ($this->supports('createMock')) {
Chris@0 112 return parent::createMock($originalClassName);
Chris@0 113 }
Chris@0 114 else {
Chris@0 115 return $this->getMock($originalClassName, [], [], '', FALSE, FALSE);
Chris@0 116 }
Chris@0 117 }
Chris@0 118
Chris@0 119 /**
Chris@0 120 * Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
Chris@0 121 *
Chris@0 122 * @param mixed $class
Chris@0 123 * The expected exception class.
Chris@0 124 * @param string $message
Chris@0 125 * The expected exception message.
Chris@0 126 * @param int $exception_code
Chris@0 127 * The expected exception code.
Chris@0 128 */
Chris@0 129 public function setExpectedException($class, $message = '', $exception_code = NULL) {
Chris@0 130 if (method_exists($this, 'expectException')) {
Chris@0 131 $this->expectException($class);
Chris@0 132 if (!empty($message)) {
Chris@0 133 $this->expectExceptionMessage($message);
Chris@0 134 }
Chris@0 135 if ($exception_code !== NULL) {
Chris@0 136 $this->expectExceptionCode($exception_code);
Chris@0 137 }
Chris@0 138 }
Chris@0 139 else {
Chris@0 140 parent::setExpectedException($class, $message, $exception_code);
Chris@0 141 }
Chris@0 142 }
Chris@0 143
Chris@0 144 /**
Chris@0 145 * Checks if the trait is used in a class that has a method.
Chris@0 146 *
Chris@0 147 * @param string $method
Chris@0 148 * Method to check.
Chris@0 149 *
Chris@0 150 * @return bool
Chris@0 151 * TRUE if the method is supported, FALSE if not.
Chris@0 152 */
Chris@0 153 private function supports($method) {
Chris@0 154 // Get the parent class of the currently running test class.
Chris@0 155 $parent = get_parent_class($this);
Chris@0 156 // Ensure that the method_exists() check on the createMock method is carried
Chris@0 157 // out on the first parent of $this that does not have access to this
Chris@0 158 // trait's methods. This is because the trait also has a method called
Chris@0 159 // createMock(). Most often the check will be made on
Chris@0 160 // \PHPUnit\Framework\TestCase.
Chris@0 161 while (method_exists($parent, 'supports')) {
Chris@0 162 $parent = get_parent_class($parent);
Chris@0 163 }
Chris@0 164 return method_exists($parent, $method);
Chris@0 165 }
Chris@0 166
Chris@0 167 }