| Total Complexity | 69 |
| Total Lines | 528 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TestCase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TestCase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class TestCase extends PhpunitTestCase |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var null|string |
||
| 32 | */ |
||
| 33 | private $expectedException; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var null|string |
||
| 37 | */ |
||
| 38 | private $expectedExceptionMessage; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param $actual |
||
| 42 | * @param string $message |
||
| 43 | */ |
||
| 44 | public static function assertIsArray($actual, $message = ''): void |
||
| 45 | { |
||
| 46 | if (is_callable('parent::' . __FUNCTION__)) { |
||
| 47 | parent::assertIsArray($actual, $message); |
||
| 48 | |||
| 49 | return; |
||
| 50 | } |
||
| 51 | |||
| 52 | self::assertInternalType('array', $actual, $message); |
||
|
|
|||
| 53 | } |
||
| 54 | |||
| 55 | public static function assertIsBool($actual, $message = ''): void |
||
| 56 | { |
||
| 57 | if (is_callable('parent::' . __FUNCTION__)) { |
||
| 58 | parent::assertIsBool($actual, $message); |
||
| 59 | |||
| 60 | return; |
||
| 61 | } |
||
| 62 | |||
| 63 | self::assertInternalType('bool', $actual, $message); |
||
| 64 | } |
||
| 65 | |||
| 66 | public static function assertIsCallable($actual, $message = ''): void |
||
| 67 | { |
||
| 68 | if (is_callable('parent::' . __FUNCTION__)) { |
||
| 69 | parent::assertIsCallable($actual, $message); |
||
| 70 | |||
| 71 | return; |
||
| 72 | } |
||
| 73 | |||
| 74 | self::assertInternalType('callable', $actual, $message); |
||
| 75 | } |
||
| 76 | |||
| 77 | public static function assertIsInt($actual, $message = ''): void |
||
| 78 | { |
||
| 79 | if (is_callable('parent::' . __FUNCTION__)) { |
||
| 80 | parent::assertIsInt($actual, $message); |
||
| 81 | |||
| 82 | return; |
||
| 83 | } |
||
| 84 | |||
| 85 | self::assertInternalType('integer', $actual, $message); |
||
| 86 | } |
||
| 87 | |||
| 88 | public static function assertIsString($actual, $message = ''): void |
||
| 89 | { |
||
| 90 | if (is_callable('parent::' . __FUNCTION__)) { |
||
| 91 | parent::assertIsString($actual, $message); |
||
| 92 | |||
| 93 | return; |
||
| 94 | } |
||
| 95 | |||
| 96 | self::assertInternalType('string', $actual, $message); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Asserts that a string matches a given regular expression. |
||
| 101 | * |
||
| 102 | * @param string $pattern |
||
| 103 | * @param string $string |
||
| 104 | * @param string $message |
||
| 105 | */ |
||
| 106 | public static function assertMatchesRegularExpression($pattern, $string, $message = ''): void |
||
| 107 | { |
||
| 108 | if (is_callable('parent::' . __FUNCTION__)) { |
||
| 109 | parent::assertMatchesRegularExpression($pattern, $string, $message); |
||
| 110 | |||
| 111 | return; |
||
| 112 | } |
||
| 113 | |||
| 114 | self::assertRegExp($pattern, $string, $message); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * assertContains() with string haystacks >>> assertStringContainsString |
||
| 119 | * |
||
| 120 | * assertStringContainsString was added in Phpunit 8 to lighten up usage |
||
| 121 | * of assertContains power-factory |
||
| 122 | * |
||
| 123 | * @param string $needle |
||
| 124 | * @param string $haystack |
||
| 125 | * @param string $message |
||
| 126 | */ |
||
| 127 | public static function assertStringContainsString($needle, $haystack, $message = ''): void |
||
| 128 | { |
||
| 129 | if (is_callable('parent::' . __FUNCTION__)) { |
||
| 130 | parent::assertStringContainsString($needle, $haystack, $message); |
||
| 131 | |||
| 132 | return; |
||
| 133 | } |
||
| 134 | |||
| 135 | self::assertContains($needle, $haystack, $message); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * assertContains() with string haystacks >>> assertStringContainsString |
||
| 140 | * |
||
| 141 | * assertStringContainsString was added in Phpunit 8 to lighten up usage |
||
| 142 | * of assertContains power-factory |
||
| 143 | * |
||
| 144 | * @param string $needle |
||
| 145 | * @param string $haystack |
||
| 146 | * @param string $message |
||
| 147 | * |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | public static function assertStringNotContainsString($needle, $haystack, $message = ''): void |
||
| 151 | { |
||
| 152 | if (is_callable('parent::' . __FUNCTION__)) { |
||
| 153 | parent::assertStringNotContainsString($needle, $haystack, $message); |
||
| 154 | |||
| 155 | return; |
||
| 156 | } |
||
| 157 | |||
| 158 | self::assertNotContains($needle, $haystack, $message); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Backwards compatible assertions (as far as in use) |
||
| 163 | * |
||
| 164 | * @param string $name |
||
| 165 | * @param array $arguments |
||
| 166 | */ |
||
| 167 | public function __call($name, array $arguments) |
||
| 168 | { |
||
| 169 | switch ($name) { |
||
| 170 | // file and directory assertions came w/ PHPUnit 5.7 |
||
| 171 | case 'assertDirectoryExists': |
||
| 172 | list($directory, $message) = $arguments + array(1 => ''); |
||
| 173 | $this->shimAssertDirectoryExists($directory, $message); |
||
| 174 | |||
| 175 | return; |
||
| 176 | case 'assertDirectoryNotExists': |
||
| 177 | case 'assertDirectoryNotExist': |
||
| 178 | list($directory, $message) = $arguments + array(1 => ''); |
||
| 179 | $this->shimAssertDirectoryNotExists($directory, $message); |
||
| 180 | |||
| 181 | return; |
||
| 182 | case 'assertFileExists': |
||
| 183 | list($filename, $message) = $arguments + array(1 => ''); |
||
| 184 | $this->shimAssertFileExists($filename, $message); |
||
| 185 | |||
| 186 | return; |
||
| 187 | case 'assertFileNotExists': |
||
| 188 | case 'assertFileNotExist': |
||
| 189 | list($filename, $message) = $arguments + array(1 => ''); |
||
| 190 | $this->shimAssertFileNotExists($filename, $message); |
||
| 191 | |||
| 192 | return; |
||
| 193 | } |
||
| 194 | |||
| 195 | throw new \BadMethodCallException( |
||
| 196 | sprintf('Testcase %s::%s(%d)', get_class($this), $name, count($arguments)) |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | |||
| 200 | /* setUp / tearDown phpunit compat overrides */ |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @deprecated phpunit compat shim, use doSetUp() downstream |
||
| 204 | * @see doSetUp |
||
| 205 | */ |
||
| 206 | protected function setUp(): void |
||
| 207 | { |
||
| 208 | $this->doSetUp(); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * phpunit compat shim |
||
| 213 | * |
||
| 214 | * @see setUp |
||
| 215 | */ |
||
| 216 | protected function doSetUp() |
||
| 217 | { |
||
| 218 | $this->expectedException = null; |
||
| 219 | $this->expectedExceptionMessage = null; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @deprecated phpunit compat shim, use doTearDown() downstream |
||
| 224 | * @see doTearDown |
||
| 225 | */ |
||
| 226 | protected function tearDown(): void |
||
| 227 | { |
||
| 228 | $this->doTearDown(); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * phpunit compat shim |
||
| 233 | * |
||
| 234 | * @see tearDown |
||
| 235 | */ |
||
| 236 | protected function doTearDown() |
||
| 237 | { |
||
| 238 | parent::tearDown(); |
||
| 239 | } |
||
| 240 | |||
| 241 | /* exceptions */ |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $exception |
||
| 245 | * |
||
| 246 | * @return void |
||
| 247 | */ |
||
| 248 | public function expectException($exception): void |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $message |
||
| 263 | * |
||
| 264 | * @throws Exception |
||
| 265 | * |
||
| 266 | * @return void |
||
| 267 | */ |
||
| 268 | public function expectExceptionMessage($message): void |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param int|string $code |
||
| 286 | * |
||
| 287 | * @throws Exception |
||
| 288 | * |
||
| 289 | * @return void |
||
| 290 | */ |
||
| 291 | public function expectExceptionCode($code): void |
||
| 292 | { |
||
| 293 | if (is_callable('parent::' . __FUNCTION__)) { |
||
| 294 | parent::expectExceptionCode($code); |
||
| 295 | |||
| 296 | return; |
||
| 297 | } |
||
| 298 | |||
| 299 | if (null === $this->expectedException) { |
||
| 300 | throw new BadMethodCallException('No exception expected'); |
||
| 301 | } |
||
| 302 | |||
| 303 | $this->setExpectedException($this->expectedException, $this->expectedExceptionMessage, $code); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Phpunit 8 deprecated (first silently) expectExceptionMessageRegExp(), |
||
| 308 | * will be gone in Phpunit 9. |
||
| 309 | * |
||
| 310 | * @param string $messageRegExp |
||
| 311 | * |
||
| 312 | * @throws Exception |
||
| 313 | * |
||
| 314 | * @return void |
||
| 315 | */ |
||
| 316 | public function expectExceptionMessageMatches($messageRegExp): void |
||
| 317 | { |
||
| 318 | if (is_callable('parent::' . __FUNCTION__)) { |
||
| 319 | parent::expectExceptionMessageMatches($messageRegExp); |
||
| 320 | |||
| 321 | return; |
||
| 322 | } |
||
| 323 | |||
| 324 | $this->expectExceptionMessageRegExp($messageRegExp); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $messageRegExp |
||
| 329 | * |
||
| 330 | * @deprecated In phpunit 8. Use expectExceptionMessageMatches() instead |
||
| 331 | * @see expectExceptionMessageMatches |
||
| 332 | * |
||
| 333 | * @return void |
||
| 334 | */ |
||
| 335 | public function expectExceptionMessageRegExp($messageRegExp): void |
||
| 336 | { |
||
| 337 | if (is_callable('parent::' . __FUNCTION__)) { |
||
| 338 | parent::expectExceptionMessageRegExp($messageRegExp); |
||
| 339 | |||
| 340 | return; |
||
| 341 | } |
||
| 342 | |||
| 343 | if (null === $this->expectedException) { |
||
| 344 | throw new \BadMethodCallException('Hmm this is message-regex without class *gg* - reflection?'); |
||
| 345 | } |
||
| 346 | |||
| 347 | $this->setExpectedExceptionRegExp($this->expectedException, $messageRegExp); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $class |
||
| 352 | * @param null|string $message |
||
| 353 | * @param null|int|string $code |
||
| 354 | * |
||
| 355 | * @return void |
||
| 356 | */ |
||
| 357 | public function setExpectedException($class, $message = null, $code = null) |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param string $class |
||
| 380 | * @param string $messageRegExp |
||
| 381 | * @param null|int|string $code |
||
| 382 | * |
||
| 383 | * @return void |
||
| 384 | */ |
||
| 385 | public function setExpectedExceptionRegExp($class, $messageRegExp = '', $code = null) |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | /* mocks */ |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Returns a test double for the specified class. |
||
| 410 | * |
||
| 411 | * @param string $originalClassName |
||
| 412 | * |
||
| 413 | * @throws Exception |
||
| 414 | * @throws \InvalidArgumentException |
||
| 415 | * |
||
| 416 | * @return MockObject |
||
| 417 | */ |
||
| 418 | protected function createMock($originalClassName): MockObject |
||
| 419 | { |
||
| 420 | if (is_callable('parent::' . __FUNCTION__)) { |
||
| 421 | return parent::createMock($originalClassName); |
||
| 422 | } |
||
| 423 | |||
| 424 | return $this->getMockBuilder($originalClassName) |
||
| 425 | ->disableOriginalConstructor() |
||
| 426 | ->disableOriginalClone() |
||
| 427 | ->disableArgumentCloning() |
||
| 428 | # phpunit ^4 does not have the method: |
||
| 429 | # ->disallowMockingUnknownTypes() |
||
| 430 | ->getMock(); |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Returns a configured test double for the specified class. |
||
| 435 | * |
||
| 436 | * @param string $originalClassName |
||
| 437 | * @param array $configuration |
||
| 438 | * |
||
| 439 | * @throws Exception |
||
| 440 | * @throws \InvalidArgumentException |
||
| 441 | * |
||
| 442 | * @return MockObject |
||
| 443 | */ |
||
| 444 | protected function createConfiguredMock($originalClassName, array $configuration): MockObject |
||
| 445 | { |
||
| 446 | if (is_callable('parent::' . __FUNCTION__)) { |
||
| 447 | return parent::createConfiguredMock($originalClassName, $configuration); |
||
| 448 | } |
||
| 449 | |||
| 450 | $o = $this->createMock($originalClassName); |
||
| 451 | |||
| 452 | foreach ($configuration as $method => $return) { |
||
| 453 | $o->method($method)->willReturn($return); |
||
| 454 | } |
||
| 455 | |||
| 456 | return $o; |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Returns a partial test double for the specified class. |
||
| 461 | * |
||
| 462 | * @param string $originalClassName |
||
| 463 | * @param string[] $methods |
||
| 464 | * |
||
| 465 | * @throws Exception |
||
| 466 | * @throws \InvalidArgumentException |
||
| 467 | * |
||
| 468 | * @return MockObject |
||
| 469 | */ |
||
| 470 | protected function createPartialMock($originalClassName, array $methods): MockObject |
||
| 471 | { |
||
| 472 | if (is_callable('parent::' . __FUNCTION__)) { |
||
| 473 | return parent::createPartialMock($originalClassName, $methods); |
||
| 474 | } |
||
| 475 | |||
| 476 | return $this->getMockBuilder($originalClassName) |
||
| 477 | ->disableOriginalConstructor() |
||
| 478 | ->disableOriginalClone() |
||
| 479 | ->disableArgumentCloning() |
||
| 480 | # phpunit ^4 does not have the method: |
||
| 481 | # ->disallowMockingUnknownTypes() |
||
| 482 | ->setMethods(empty($methods) ? null : $methods) |
||
| 483 | ->getMock(); |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Asserts that a directory exists. |
||
| 488 | * |
||
| 489 | * @param string $directory |
||
| 490 | * @param string $message |
||
| 491 | * |
||
| 492 | * @return void |
||
| 493 | */ |
||
| 494 | private function shimAssertDirectoryExists($directory, $message = '') |
||
| 495 | { |
||
| 496 | if (!\is_string($directory)) { |
||
| 497 | throw new \InvalidArgumentException('$directory is not a string'); |
||
| 498 | } |
||
| 499 | |||
| 500 | /** @noinspection PhpUnitTestsInspection */ |
||
| 501 | self::assertTrue(\is_dir($directory), $message); |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Asserts that a directory exists. |
||
| 506 | * |
||
| 507 | * @param string $directory |
||
| 508 | * @param string $message |
||
| 509 | * |
||
| 510 | * @return void |
||
| 511 | */ |
||
| 512 | private function shimAssertDirectoryNotExists($directory, $message = '') |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Asserts that a file exists. |
||
| 524 | * |
||
| 525 | * @param string $filename |
||
| 526 | * @param string $message |
||
| 527 | * |
||
| 528 | * @return void |
||
| 529 | */ |
||
| 530 | private function shimAssertFileExists($filename, $message = '') |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Asserts that a file exists. |
||
| 542 | * |
||
| 543 | * @param string $filename |
||
| 544 | * @param string $message |
||
| 545 | * |
||
| 546 | * @return void |
||
| 547 | */ |
||
| 548 | private function shimAssertFileNotExists($filename, $message = '') |
||
| 556 | } |
||
| 557 | } |
||
| 558 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.