Complex classes like TestCaseTest 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 TestCaseTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class TestCaseTest extends TestCase |
||
| 16 | { |
||
| 17 | protected $backupGlobalsBlacklist = ['i', 'singleton']; |
||
| 18 | |||
| 19 | protected static $testStatic = 0; |
||
| 20 | |||
| 21 | public static function setUpBeforeClass() |
||
| 22 | { |
||
| 23 | $GLOBALS['a'] = 'a'; |
||
| 24 | $_ENV['b'] = 'b'; |
||
| 25 | $_POST['c'] = 'c'; |
||
| 26 | $_GET['d'] = 'd'; |
||
| 27 | $_COOKIE['e'] = 'e'; |
||
| 28 | $_SERVER['f'] = 'f'; |
||
| 29 | $_FILES['g'] = 'g'; |
||
| 30 | $_REQUEST['h'] = 'h'; |
||
| 31 | $GLOBALS['i'] = 'i'; |
||
| 32 | } |
||
| 33 | |||
| 34 | public static function tearDownAfterClass() |
||
| 35 | { |
||
| 36 | unset($GLOBALS['a']); |
||
| 37 | unset($_ENV['b']); |
||
| 38 | unset($_POST['c']); |
||
| 39 | unset($_GET['d']); |
||
| 40 | unset($_COOKIE['e']); |
||
| 41 | unset($_SERVER['f']); |
||
| 42 | unset($_FILES['g']); |
||
| 43 | unset($_REQUEST['h']); |
||
| 44 | unset($GLOBALS['i']); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function testCaseToString() |
||
| 48 | { |
||
| 49 | $this->assertEquals( |
||
| 50 | 'PHPUnit\Framework\TestCaseTest::testCaseToString', |
||
| 51 | $this->toString() |
||
| 52 | ); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function testSuccess() |
||
| 56 | { |
||
| 57 | $test = new \Success; |
||
| 58 | $result = $test->run(); |
||
| 59 | |||
| 60 | $this->assertEquals(BaseTestRunner::STATUS_PASSED, $test->getStatus()); |
||
| 61 | $this->assertEquals(0, $result->errorCount()); |
||
| 62 | $this->assertEquals(0, $result->failureCount()); |
||
| 63 | $this->assertEquals(0, $result->skippedCount()); |
||
| 64 | $this->assertCount(1, $result); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function testFailure() |
||
| 68 | { |
||
| 69 | $test = new \Failure; |
||
| 70 | $result = $test->run(); |
||
| 71 | |||
| 72 | $this->assertEquals(BaseTestRunner::STATUS_FAILURE, $test->getStatus()); |
||
| 73 | $this->assertEquals(0, $result->errorCount()); |
||
| 74 | $this->assertEquals(1, $result->failureCount()); |
||
| 75 | $this->assertEquals(0, $result->skippedCount()); |
||
| 76 | $this->assertCount(1, $result); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function testError() |
||
| 80 | { |
||
| 81 | $test = new \TestError; |
||
| 82 | $result = $test->run(); |
||
| 83 | |||
| 84 | $this->assertEquals(BaseTestRunner::STATUS_ERROR, $test->getStatus()); |
||
| 85 | $this->assertEquals(1, $result->errorCount()); |
||
| 86 | $this->assertEquals(0, $result->failureCount()); |
||
| 87 | $this->assertEquals(0, $result->skippedCount()); |
||
| 88 | $this->assertCount(1, $result); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function testSkipped() |
||
| 92 | { |
||
| 93 | $test = new \TestSkipped; |
||
| 94 | $result = $test->run(); |
||
| 95 | |||
| 96 | $this->assertEquals(BaseTestRunner::STATUS_SKIPPED, $test->getStatus()); |
||
| 97 | $this->assertEquals('Skipped test', $test->getStatusMessage()); |
||
| 98 | $this->assertEquals(0, $result->errorCount()); |
||
| 99 | $this->assertEquals(0, $result->failureCount()); |
||
| 100 | $this->assertEquals(1, $result->skippedCount()); |
||
| 101 | $this->assertCount(1, $result); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function testIncomplete() |
||
| 105 | { |
||
| 106 | $test = new \TestIncomplete; |
||
| 107 | $result = $test->run(); |
||
| 108 | |||
| 109 | $this->assertEquals(BaseTestRunner::STATUS_INCOMPLETE, $test->getStatus()); |
||
| 110 | $this->assertEquals('Incomplete test', $test->getStatusMessage()); |
||
| 111 | $this->assertEquals(0, $result->errorCount()); |
||
| 112 | $this->assertEquals(0, $result->failureCount()); |
||
| 113 | $this->assertEquals(0, $result->skippedCount()); |
||
| 114 | $this->assertCount(1, $result); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function testExceptionInSetUp() |
||
| 118 | { |
||
| 119 | $test = new \ExceptionInSetUpTest('testSomething'); |
||
| 120 | $test->run(); |
||
| 121 | |||
| 122 | $this->assertTrue($test->setUp); |
||
| 123 | $this->assertFalse($test->assertPreConditions); |
||
| 124 | $this->assertFalse($test->testSomething); |
||
| 125 | $this->assertFalse($test->assertPostConditions); |
||
| 126 | $this->assertTrue($test->tearDown); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function testExceptionInAssertPreConditions() |
||
| 130 | { |
||
| 131 | $test = new \ExceptionInAssertPreConditionsTest('testSomething'); |
||
| 132 | $test->run(); |
||
| 133 | |||
| 134 | $this->assertTrue($test->setUp); |
||
| 135 | $this->assertTrue($test->assertPreConditions); |
||
| 136 | $this->assertFalse($test->testSomething); |
||
| 137 | $this->assertFalse($test->assertPostConditions); |
||
| 138 | $this->assertTrue($test->tearDown); |
||
| 139 | } |
||
| 140 | |||
| 141 | public function testExceptionInTest() |
||
| 142 | { |
||
| 143 | $test = new \ExceptionInTest('testSomething'); |
||
| 144 | $test->run(); |
||
| 145 | |||
| 146 | $this->assertTrue($test->setUp); |
||
| 147 | $this->assertTrue($test->assertPreConditions); |
||
| 148 | $this->assertTrue($test->testSomething); |
||
| 149 | $this->assertFalse($test->assertPostConditions); |
||
| 150 | $this->assertTrue($test->tearDown); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function testExceptionInAssertPostConditions() |
||
| 154 | { |
||
| 155 | $test = new \ExceptionInAssertPostConditionsTest('testSomething'); |
||
| 156 | $test->run(); |
||
| 157 | |||
| 158 | $this->assertTrue($test->setUp); |
||
| 159 | $this->assertTrue($test->assertPreConditions); |
||
| 160 | $this->assertTrue($test->testSomething); |
||
| 161 | $this->assertTrue($test->assertPostConditions); |
||
| 162 | $this->assertTrue($test->tearDown); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function testExceptionInTearDown() |
||
| 166 | { |
||
| 167 | $test = new \ExceptionInTearDownTest('testSomething'); |
||
| 168 | $test->run(); |
||
| 169 | |||
| 170 | $this->assertTrue($test->setUp); |
||
| 171 | $this->assertTrue($test->assertPreConditions); |
||
| 172 | $this->assertTrue($test->testSomething); |
||
| 173 | $this->assertTrue($test->assertPostConditions); |
||
| 174 | $this->assertTrue($test->tearDown); |
||
| 175 | } |
||
| 176 | |||
| 177 | public function testNoArgTestCasePasses() |
||
| 178 | { |
||
| 179 | $result = new TestResult; |
||
| 180 | $t = new TestSuite(\NoArgTestCaseTest::class); |
||
| 181 | |||
| 182 | $t->run($result); |
||
| 183 | |||
| 184 | $this->assertCount(1, $result); |
||
| 185 | $this->assertEquals(0, $result->failureCount()); |
||
| 186 | $this->assertEquals(0, $result->errorCount()); |
||
| 187 | } |
||
| 188 | |||
| 189 | public function testWasRun() |
||
| 190 | { |
||
| 191 | $test = new \WasRun; |
||
| 192 | $test->run(); |
||
| 193 | |||
| 194 | $this->assertTrue($test->wasRun); |
||
| 195 | } |
||
| 196 | |||
| 197 | public function testException() |
||
| 198 | { |
||
| 199 | $test = new \ThrowExceptionTestCase('test'); |
||
| 200 | $test->expectException(\RuntimeException::class); |
||
| 201 | |||
| 202 | $result = $test->run(); |
||
| 203 | |||
| 204 | $this->assertCount(1, $result); |
||
| 205 | $this->assertTrue($result->wasSuccessful()); |
||
| 206 | } |
||
| 207 | |||
| 208 | public function testExceptionWithEmptyMessage() |
||
| 209 | { |
||
| 210 | $test = new \ThrowExceptionTestCase('test'); |
||
| 211 | $test->expectException(\RuntimeException::class); |
||
| 212 | |||
| 213 | $result = $test->run(); |
||
| 214 | |||
| 215 | $this->assertCount(1, $result); |
||
| 216 | $this->assertTrue($result->wasSuccessful()); |
||
| 217 | } |
||
| 218 | |||
| 219 | public function testExceptionWithNullMessage() |
||
| 220 | { |
||
| 221 | $test = new \ThrowExceptionTestCase('test'); |
||
| 222 | $test->expectException(\RuntimeException::class); |
||
| 223 | |||
| 224 | $result = $test->run(); |
||
| 225 | |||
| 226 | $this->assertCount(1, $result); |
||
| 227 | $this->assertTrue($result->wasSuccessful()); |
||
| 228 | } |
||
| 229 | |||
| 230 | public function testExceptionWithMessage() |
||
| 231 | { |
||
| 232 | $test = new \ThrowExceptionTestCase('test'); |
||
| 233 | $test->expectException(\RuntimeException::class); |
||
| 234 | $test->expectExceptionMessage('A runtime error occurred'); |
||
| 235 | |||
| 236 | $result = $test->run(); |
||
| 237 | |||
| 238 | $this->assertCount(1, $result); |
||
| 239 | $this->assertTrue($result->wasSuccessful()); |
||
| 240 | } |
||
| 241 | |||
| 242 | public function testExceptionWithWrongMessage() |
||
| 243 | { |
||
| 244 | $test = new \ThrowExceptionTestCase('test'); |
||
| 245 | $test->expectException(\RuntimeException::class); |
||
| 246 | $test->expectExceptionMessage('A logic error occurred'); |
||
| 247 | |||
| 248 | $result = $test->run(); |
||
| 249 | |||
| 250 | $this->assertEquals(1, $result->failureCount()); |
||
| 251 | $this->assertCount(1, $result); |
||
| 252 | $this->assertEquals( |
||
| 253 | "Failed asserting that exception message 'A runtime error occurred' contains 'A logic error occurred'.", |
||
| 254 | $test->getStatusMessage() |
||
| 255 | ); |
||
| 256 | } |
||
| 257 | |||
| 258 | public function testExceptionWithRegexpMessage() |
||
| 259 | { |
||
| 260 | $test = new \ThrowExceptionTestCase('test'); |
||
| 261 | $test->expectException(\RuntimeException::class); |
||
| 262 | $test->expectExceptionMessageRegExp('/runtime .*? occurred/'); |
||
| 263 | |||
| 264 | $result = $test->run(); |
||
| 265 | |||
| 266 | $this->assertCount(1, $result); |
||
| 267 | $this->assertTrue($result->wasSuccessful()); |
||
| 268 | } |
||
| 269 | |||
| 270 | public function testExceptionWithWrongRegexpMessage() |
||
| 271 | { |
||
| 272 | $test = new \ThrowExceptionTestCase('test'); |
||
| 273 | $test->expectException(\RuntimeException::class); |
||
| 274 | $test->expectExceptionMessageRegExp('/logic .*? occurred/'); |
||
| 275 | |||
| 276 | $result = $test->run(); |
||
| 277 | |||
| 278 | $this->assertEquals(1, $result->failureCount()); |
||
| 279 | $this->assertCount(1, $result); |
||
| 280 | $this->assertEquals( |
||
| 281 | "Failed asserting that exception message 'A runtime error occurred' matches '/logic .*? occurred/'.", |
||
| 282 | $test->getStatusMessage() |
||
| 283 | ); |
||
| 284 | } |
||
| 285 | |||
| 286 | public function testExceptionWithInvalidRegexpMessage() |
||
| 287 | { |
||
| 288 | $test = new \ThrowExceptionTestCase('test'); |
||
| 289 | $test->expectException(\RuntimeException::class); |
||
| 290 | $test->expectExceptionMessageRegExp('#runtime .*? occurred/'); |
||
| 291 | |||
| 292 | $test->run(); |
||
| 293 | |||
| 294 | $this->assertEquals( |
||
| 295 | "Invalid expected exception message regex given: '#runtime .*? occurred/'", |
||
| 296 | $test->getStatusMessage() |
||
| 297 | ); |
||
| 298 | } |
||
| 299 | |||
| 300 | public function testNoException() |
||
| 301 | { |
||
| 302 | $test = new \ThrowNoExceptionTestCase('test'); |
||
| 303 | $test->expectException(\RuntimeException::class); |
||
| 304 | |||
| 305 | $result = $test->run(); |
||
| 306 | |||
| 307 | $this->assertEquals(1, $result->failureCount()); |
||
| 308 | $this->assertCount(1, $result); |
||
| 309 | } |
||
| 310 | |||
| 311 | public function testWrongException() |
||
| 312 | { |
||
| 313 | $test = new \ThrowExceptionTestCase('test'); |
||
| 314 | $test->expectException(\InvalidArgumentException::class); |
||
| 315 | |||
| 316 | $result = $test->run(); |
||
| 317 | |||
| 318 | $this->assertEquals(1, $result->failureCount()); |
||
| 319 | $this->assertCount(1, $result); |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @backupGlobals enabled |
||
| 324 | */ |
||
| 325 | public function testGlobalsBackupPre() |
||
| 326 | { |
||
| 327 | global $a; |
||
| 328 | global $i; |
||
| 329 | |||
| 330 | $this->assertEquals('a', $a); |
||
| 331 | $this->assertEquals('a', $GLOBALS['a']); |
||
| 332 | $this->assertEquals('b', $_ENV['b']); |
||
| 333 | $this->assertEquals('c', $_POST['c']); |
||
| 334 | $this->assertEquals('d', $_GET['d']); |
||
| 335 | $this->assertEquals('e', $_COOKIE['e']); |
||
| 336 | $this->assertEquals('f', $_SERVER['f']); |
||
| 337 | $this->assertEquals('g', $_FILES['g']); |
||
| 338 | $this->assertEquals('h', $_REQUEST['h']); |
||
| 339 | $this->assertEquals('i', $i); |
||
| 340 | $this->assertEquals('i', $GLOBALS['i']); |
||
| 341 | |||
| 342 | $GLOBALS['a'] = 'aa'; |
||
| 343 | $GLOBALS['foo'] = 'bar'; |
||
| 344 | $_ENV['b'] = 'bb'; |
||
| 345 | $_POST['c'] = 'cc'; |
||
| 346 | $_GET['d'] = 'dd'; |
||
| 347 | $_COOKIE['e'] = 'ee'; |
||
| 348 | $_SERVER['f'] = 'ff'; |
||
| 349 | $_FILES['g'] = 'gg'; |
||
| 350 | $_REQUEST['h'] = 'hh'; |
||
| 351 | $GLOBALS['i'] = 'ii'; |
||
| 352 | |||
| 353 | $this->assertEquals('aa', $a); |
||
| 354 | $this->assertEquals('aa', $GLOBALS['a']); |
||
| 355 | $this->assertEquals('bar', $GLOBALS['foo']); |
||
| 356 | $this->assertEquals('bb', $_ENV['b']); |
||
| 357 | $this->assertEquals('cc', $_POST['c']); |
||
| 358 | $this->assertEquals('dd', $_GET['d']); |
||
| 359 | $this->assertEquals('ee', $_COOKIE['e']); |
||
| 360 | $this->assertEquals('ff', $_SERVER['f']); |
||
| 361 | $this->assertEquals('gg', $_FILES['g']); |
||
| 362 | $this->assertEquals('hh', $_REQUEST['h']); |
||
| 363 | $this->assertEquals('ii', $i); |
||
| 364 | $this->assertEquals('ii', $GLOBALS['i']); |
||
| 365 | } |
||
| 366 | |||
| 367 | public function testGlobalsBackupPost() |
||
| 368 | { |
||
| 369 | global $a; |
||
| 370 | global $i; |
||
| 371 | |||
| 372 | $this->assertEquals('a', $a); |
||
| 373 | $this->assertEquals('a', $GLOBALS['a']); |
||
| 374 | $this->assertEquals('b', $_ENV['b']); |
||
| 375 | $this->assertEquals('c', $_POST['c']); |
||
| 376 | $this->assertEquals('d', $_GET['d']); |
||
| 377 | $this->assertEquals('e', $_COOKIE['e']); |
||
| 378 | $this->assertEquals('f', $_SERVER['f']); |
||
| 379 | $this->assertEquals('g', $_FILES['g']); |
||
| 380 | $this->assertEquals('h', $_REQUEST['h']); |
||
| 381 | $this->assertEquals('ii', $i); |
||
| 382 | $this->assertEquals('ii', $GLOBALS['i']); |
||
| 383 | |||
| 384 | $this->assertArrayNotHasKey('foo', $GLOBALS); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @backupGlobals enabled |
||
| 389 | * @backupStaticAttributes enabled |
||
| 390 | * |
||
| 391 | * @doesNotPerformAssertions |
||
| 392 | */ |
||
| 393 | public function testStaticAttributesBackupPre() |
||
| 394 | { |
||
| 395 | $GLOBALS['singleton'] = \Singleton::getInstance(); |
||
| 396 | self::$testStatic = 123; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @depends testStaticAttributesBackupPre |
||
| 401 | */ |
||
| 402 | public function testStaticAttributesBackupPost() |
||
| 403 | { |
||
| 404 | $this->assertNotSame($GLOBALS['singleton'], \Singleton::getInstance()); |
||
| 405 | $this->assertSame(0, self::$testStatic); |
||
| 406 | } |
||
| 407 | |||
| 408 | public function testIsInIsolationReturnsFalse() |
||
| 409 | { |
||
| 410 | $test = new \IsolationTest('testIsInIsolationReturnsFalse'); |
||
| 411 | $result = $test->run(); |
||
| 412 | |||
| 413 | $this->assertCount(1, $result); |
||
| 414 | $this->assertTrue($result->wasSuccessful()); |
||
| 415 | } |
||
| 416 | |||
| 417 | public function testIsInIsolationReturnsTrue() |
||
| 418 | { |
||
| 419 | $test = new \IsolationTest('testIsInIsolationReturnsTrue'); |
||
| 420 | $test->setRunTestInSeparateProcess(true); |
||
| 421 | $result = $test->run(); |
||
| 422 | |||
| 423 | $this->assertCount(1, $result); |
||
| 424 | $this->assertTrue($result->wasSuccessful()); |
||
| 425 | } |
||
| 426 | |||
| 427 | public function testExpectOutputStringFooActualFoo() |
||
| 428 | { |
||
| 429 | $test = new \OutputTestCase('testExpectOutputStringFooActualFoo'); |
||
| 430 | $result = $test->run(); |
||
| 431 | |||
| 432 | $this->assertCount(1, $result); |
||
| 433 | $this->assertTrue($result->wasSuccessful()); |
||
| 434 | } |
||
| 435 | |||
| 436 | public function testExpectOutputStringFooActualBar() |
||
| 437 | { |
||
| 438 | $test = new \OutputTestCase('testExpectOutputStringFooActualBar'); |
||
| 439 | $result = $test->run(); |
||
| 440 | |||
| 441 | $this->assertCount(1, $result); |
||
| 442 | $this->assertFalse($result->wasSuccessful()); |
||
| 443 | } |
||
| 444 | |||
| 445 | public function testExpectOutputRegexFooActualFoo() |
||
| 446 | { |
||
| 447 | $test = new \OutputTestCase('testExpectOutputRegexFooActualFoo'); |
||
| 448 | $result = $test->run(); |
||
| 449 | |||
| 450 | $this->assertCount(1, $result); |
||
| 451 | $this->assertTrue($result->wasSuccessful()); |
||
| 452 | } |
||
| 453 | |||
| 454 | public function testExpectOutputRegexFooActualBar() |
||
| 455 | { |
||
| 456 | $test = new \OutputTestCase('testExpectOutputRegexFooActualBar'); |
||
| 457 | $result = $test->run(); |
||
| 458 | |||
| 459 | $this->assertCount(1, $result); |
||
| 460 | $this->assertFalse($result->wasSuccessful()); |
||
| 461 | } |
||
| 462 | |||
| 463 | public function testSkipsIfRequiresHigherVersionOfPHPUnit() |
||
| 464 | { |
||
| 465 | $test = new \RequirementsTest('testAlwaysSkip'); |
||
| 466 | $result = $test->run(); |
||
| 467 | |||
| 468 | $this->assertEquals(1, $result->skippedCount()); |
||
| 469 | $this->assertEquals( |
||
| 470 | 'PHPUnit >= 1111111 is required.', |
||
| 471 | $test->getStatusMessage() |
||
| 472 | ); |
||
| 473 | } |
||
| 474 | |||
| 475 | public function testSkipsIfRequiresHigherVersionOfPHP() |
||
| 476 | { |
||
| 477 | $test = new \RequirementsTest('testAlwaysSkip2'); |
||
| 478 | $result = $test->run(); |
||
| 479 | |||
| 480 | $this->assertEquals(1, $result->skippedCount()); |
||
| 481 | $this->assertEquals( |
||
| 482 | 'PHP >= 9999999 is required.', |
||
| 483 | $test->getStatusMessage() |
||
| 484 | ); |
||
| 485 | } |
||
| 486 | |||
| 487 | public function testSkipsIfRequiresNonExistingOs() |
||
| 488 | { |
||
| 489 | $test = new \RequirementsTest('testAlwaysSkip3'); |
||
| 490 | $result = $test->run(); |
||
| 491 | |||
| 492 | $this->assertEquals(1, $result->skippedCount()); |
||
| 493 | $this->assertEquals( |
||
| 494 | 'Operating system matching /DOESNOTEXIST/i is required.', |
||
| 495 | $test->getStatusMessage() |
||
| 496 | ); |
||
| 497 | } |
||
| 498 | |||
| 499 | public function testSkipsIfRequiresNonExistingFunction() |
||
| 500 | { |
||
| 501 | $test = new \RequirementsTest('testNine'); |
||
| 502 | $result = $test->run(); |
||
| 503 | |||
| 504 | $this->assertEquals(1, $result->skippedCount()); |
||
| 505 | $this->assertEquals( |
||
| 506 | 'Function testFunc is required.', |
||
| 507 | $test->getStatusMessage() |
||
| 508 | ); |
||
| 509 | } |
||
| 510 | |||
| 511 | public function testSkipsIfRequiresNonExistingExtension() |
||
| 512 | { |
||
| 513 | $test = new \RequirementsTest('testTen'); |
||
| 514 | $test->run(); |
||
| 515 | |||
| 516 | $this->assertEquals( |
||
| 517 | 'Extension testExt is required.', |
||
| 518 | $test->getStatusMessage() |
||
| 519 | ); |
||
| 520 | } |
||
| 521 | |||
| 522 | public function testSkipsIfRequiresExtensionWithAMinimumVersion() |
||
| 523 | { |
||
| 524 | $test = new \RequirementsTest('testSpecificExtensionVersion'); |
||
| 525 | $test->run(); |
||
| 526 | |||
| 527 | $this->assertEquals( |
||
| 528 | 'Extension testExt >= 1.8.0 is required.', |
||
| 529 | $test->getStatusMessage() |
||
| 530 | ); |
||
| 531 | } |
||
| 532 | |||
| 533 | public function testSkipsProvidesMessagesForAllSkippingReasons() |
||
| 534 | { |
||
| 535 | $test = new \RequirementsTest('testAllPossibleRequirements'); |
||
| 536 | $test->run(); |
||
| 537 | |||
| 538 | $this->assertEquals( |
||
| 539 | 'PHP >= 99-dev is required.' . PHP_EOL . |
||
| 540 | 'PHPUnit >= 9-dev is required.' . PHP_EOL . |
||
| 541 | 'Operating system matching /DOESNOTEXIST/i is required.' . PHP_EOL . |
||
| 542 | 'Function testFuncOne is required.' . PHP_EOL . |
||
| 543 | 'Function testFuncTwo is required.' . PHP_EOL . |
||
| 544 | 'Extension testExtOne is required.' . PHP_EOL . |
||
| 545 | 'Extension testExtTwo is required.' . PHP_EOL . |
||
| 546 | 'Extension testExtThree >= 2.0 is required.', |
||
| 547 | $test->getStatusMessage() |
||
| 548 | ); |
||
| 549 | } |
||
| 550 | |||
| 551 | public function testRequiringAnExistingMethodDoesNotSkip() |
||
| 552 | { |
||
| 553 | $test = new \RequirementsTest('testExistingMethod'); |
||
| 554 | $result = $test->run(); |
||
| 555 | $this->assertEquals(0, $result->skippedCount()); |
||
| 556 | } |
||
| 557 | |||
| 558 | public function testRequiringAnExistingFunctionDoesNotSkip() |
||
| 559 | { |
||
| 560 | $test = new \RequirementsTest('testExistingFunction'); |
||
| 561 | $result = $test->run(); |
||
| 562 | $this->assertEquals(0, $result->skippedCount()); |
||
| 563 | } |
||
| 564 | |||
| 565 | public function testRequiringAnExistingExtensionDoesNotSkip() |
||
| 566 | { |
||
| 567 | $test = new \RequirementsTest('testExistingExtension'); |
||
| 568 | $result = $test->run(); |
||
| 569 | $this->assertEquals(0, $result->skippedCount()); |
||
| 570 | } |
||
| 571 | |||
| 572 | public function testRequiringAnExistingOsDoesNotSkip() |
||
| 573 | { |
||
| 574 | $test = new \RequirementsTest('testExistingOs'); |
||
| 575 | $result = $test->run(); |
||
| 576 | $this->assertEquals(0, $result->skippedCount()); |
||
| 577 | } |
||
| 578 | |||
| 579 | public function testCurrentWorkingDirectoryIsRestored() |
||
| 580 | { |
||
| 581 | $expectedCwd = \getcwd(); |
||
| 582 | |||
| 583 | $test = new \ChangeCurrentWorkingDirectoryTest('testSomethingThatChangesTheCwd'); |
||
| 584 | $test->run(); |
||
| 585 | |||
| 586 | $this->assertSame($expectedCwd, \getcwd()); |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * @requires PHP 7 |
||
| 591 | * @expectedException TypeError |
||
| 592 | */ |
||
| 593 | public function testTypeErrorCanBeExpected() |
||
| 594 | { |
||
| 595 | $o = new \ClassWithScalarTypeDeclarations; |
||
| 596 | $o->foo(null, null); |
||
| 597 | } |
||
| 598 | |||
| 599 | public function testCreateMockFromClassName() |
||
| 600 | { |
||
| 601 | $mock = $this->createMock(\Mockable::class); |
||
| 602 | |||
| 603 | $this->assertInstanceOf(\Mockable::class, $mock); |
||
| 604 | $this->assertInstanceOf(\PHPUnit_Framework_MockObject_MockObject::class, $mock); |
||
| 605 | } |
||
| 606 | |||
| 607 | public function testCreateMockMocksAllMethods() |
||
| 608 | { |
||
| 609 | /** @var \Mockable $mock */ |
||
| 610 | $mock = $this->createMock(\Mockable::class); |
||
| 611 | |||
| 612 | $this->assertNull($mock->foo()); |
||
| 613 | $this->assertNull($mock->bar()); |
||
| 614 | } |
||
| 615 | |||
| 616 | public function testCreatePartialMockDoesNotMockAllMethods() |
||
| 617 | { |
||
| 618 | /** @var \Mockable $mock */ |
||
| 619 | $mock = $this->createPartialMock(\Mockable::class, ['foo']); |
||
| 620 | |||
| 621 | $this->assertNull($mock->foo()); |
||
| 622 | $this->assertTrue($mock->bar()); |
||
| 623 | } |
||
| 624 | |||
| 625 | public function testCreatePartialMockCanMockNoMethods() |
||
| 626 | { |
||
| 627 | /** @var \Mockable $mock */ |
||
| 628 | $mock = $this->createPartialMock(\Mockable::class, []); |
||
| 629 | |||
| 630 | $this->assertTrue($mock->foo()); |
||
| 631 | $this->assertTrue($mock->bar()); |
||
| 632 | } |
||
| 633 | |||
| 634 | public function testCreateMockSkipsConstructor() |
||
| 635 | { |
||
| 636 | /** @var \Mockable $mock */ |
||
| 637 | $mock = $this->createMock(\Mockable::class); |
||
| 638 | |||
| 639 | $this->assertFalse($mock->constructorCalled); |
||
| 640 | } |
||
| 641 | |||
| 642 | public function testCreateMockDisablesOriginalClone() |
||
| 643 | { |
||
| 644 | /** @var \Mockable $mock */ |
||
| 645 | $mock = $this->createMock(\Mockable::class); |
||
| 646 | |||
| 647 | $cloned = clone $mock; |
||
| 648 | $this->assertFalse($cloned->cloned); |
||
| 649 | } |
||
| 650 | |||
| 651 | public function testConfiguredMockCanBeCreated() |
||
| 652 | { |
||
| 653 | /** @var \Mockable $mock */ |
||
| 654 | $mock = $this->createConfiguredMock( |
||
| 655 | \Mockable::class, |
||
| 656 | [ |
||
| 657 | 'foo' => false |
||
| 658 | ] |
||
| 659 | ); |
||
| 660 | |||
| 661 | $this->assertFalse($mock->foo()); |
||
| 662 | $this->assertNull($mock->bar()); |
||
| 663 | } |
||
| 664 | |||
| 665 | public function testProvidingOfAutoreferencedArray() |
||
| 666 | { |
||
| 667 | $test = new \TestAutoreferenced('testJsonEncodeException', $this->getAutoreferencedArray()); |
||
| 668 | $test->runBare(); |
||
| 669 | |||
| 670 | $this->assertInternalType('array', $test->myTestData); |
||
| 671 | $this->assertArrayHasKey('data', $test->myTestData); |
||
| 672 | $this->assertEquals($test->myTestData['data'][0], $test->myTestData['data']); |
||
| 673 | } |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @return array |
||
| 677 | */ |
||
| 678 | private function getAutoreferencedArray() |
||
| 679 | { |
||
| 680 | $recursionData = []; |
||
| 681 | $recursionData[] = &$recursionData; |
||
| 682 | |||
| 683 | return [ |
||
| 684 | 'RECURSION' => [ |
||
| 685 | 'data' => $recursionData |
||
| 686 | ] |
||
| 687 | ]; |
||
| 688 | } |
||
| 689 | |||
| 690 | public function testProvidingArrayThatMixesObjectsAndScalars() |
||
| 691 | { |
||
| 692 | $data = [ |
||
| 693 | [123], |
||
| 694 | ['foo'], |
||
| 695 | [$this->createMock(\Mockable::class)], |
||
| 696 | ]; |
||
| 697 | |||
| 698 | $test = new \TestAutoreferenced('testJsonEncodeException', [$data]); |
||
| 699 | $test->runBare(); |
||
| 700 | |||
| 701 | $this->assertInternalType('array', $test->myTestData); |
||
| 702 | $this->assertSame($data, $test->myTestData); |
||
| 703 | } |
||
| 704 | } |
||
| 705 |