| Total Complexity | 61 |
| Total Lines | 522 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ApplyTaskTest 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 ApplyTaskTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class ApplyTaskTest extends BuildFileTest |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Whether test is being run on windows |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | protected $windows; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Setup the test |
||
| 37 | */ |
||
| 38 | public function setUp(): void |
||
| 39 | { |
||
| 40 | // Tests definitions |
||
| 41 | $this->configureProject(PHING_TEST_BASE . '/etc/tasks/system/ApplyTest.xml'); |
||
|
|
|||
| 42 | |||
| 43 | // Identifying the running environment |
||
| 44 | $this->windows = strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'; |
||
| 45 | } |
||
| 46 | |||
| 47 | /**********************************************************************************/ |
||
| 48 | /************************************** T E S T S *********************************/ |
||
| 49 | /**********************************************************************************/ |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Tests the OS configuration setting |
||
| 53 | */ |
||
| 54 | public function testPropertySetOs() |
||
| 55 | { |
||
| 56 | $this->assertAttributeIsSetTo('os', 'linux'); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Tests the dir configuration setting |
||
| 61 | */ |
||
| 62 | public function testPropertySetDir() |
||
| 63 | { |
||
| 64 | $this->assertAttributeIsSetTo('dir', new PhingFile($this->project->getProperty('php.tmpdir'))); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Tests the escape configuration setting |
||
| 69 | */ |
||
| 70 | public function testPropertySetEscape() |
||
| 71 | { |
||
| 72 | $this->assertAttributeIsSetTo('escape', true); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Tests the pass-thru configuration setting |
||
| 77 | */ |
||
| 78 | public function testPropertySetPassthru() |
||
| 79 | { |
||
| 80 | $this->assertAttributeIsSetTo('passthru', true); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Tests the spawn configuration setting |
||
| 85 | */ |
||
| 86 | public function testPropertySetSpawn() |
||
| 87 | { |
||
| 88 | $this->assertAttributeIsSetTo('spawn', true); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Tests the returnProperty configuration setting |
||
| 93 | */ |
||
| 94 | public function testPropertySetReturnProperty() |
||
| 95 | { |
||
| 96 | $this->assertAttributeIsSetTo('returnProperty', 'retval'); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Tests the outputProperty configuration setting |
||
| 101 | */ |
||
| 102 | public function testPropertySetOutputProperty() |
||
| 103 | { |
||
| 104 | $this->assertAttributeIsSetTo('outputProperty', 'outval'); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Tests the checkReturn/failonerror configuration setting |
||
| 109 | */ |
||
| 110 | public function testPropertySetCheckReturn() |
||
| 111 | { |
||
| 112 | $this->assertAttributeIsSetTo('checkreturn', true); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Tests the output configuration setting |
||
| 117 | */ |
||
| 118 | public function testPropertySetOutput() |
||
| 119 | { |
||
| 120 | $this->assertAttributeIsSetTo('output', |
||
| 121 | new PhingFile($this->project->getProperty('php.tmpdir') . '/outputfilename')); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Tests the error configuration setting |
||
| 126 | */ |
||
| 127 | public function testPropertySetError() |
||
| 128 | { |
||
| 129 | $this->assertAttributeIsSetTo('error', |
||
| 130 | new PhingFile($this->project->getProperty('php.tmpdir') . '/errorfilename')); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Tests the append configuration setting |
||
| 135 | */ |
||
| 136 | public function testPropertySetAppend() |
||
| 137 | { |
||
| 138 | $this->assertAttributeIsSetTo('append', true, 'appendoutput'); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Tests the parallel configuration setting |
||
| 143 | */ |
||
| 144 | public function testPropertySetParallel() |
||
| 145 | { |
||
| 146 | $this->assertAttributeIsSetTo('parallel', false); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Tests the addsourcefile configuration setting |
||
| 151 | */ |
||
| 152 | public function testPropertySetAddsourcefile() |
||
| 153 | { |
||
| 154 | $this->assertAttributeIsSetTo('addsourcefile', false); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Tests the relative configuration setting |
||
| 159 | */ |
||
| 160 | public function testPropertySetRelative() |
||
| 161 | { |
||
| 162 | $this->assertAttributeIsSetTo('relative', false); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Tests the forwardslash configuration setting |
||
| 167 | */ |
||
| 168 | public function testPropertySetForwardslash() |
||
| 169 | { |
||
| 170 | $this->assertAttributeIsSetTo('forwardslash', true); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Tests the maxparallel configuration setting |
||
| 175 | */ |
||
| 176 | public function testPropertySetMaxparallel() |
||
| 177 | { |
||
| 178 | $this->assertAttributeIsSetTo('maxparallel', 10); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Tests the OS execution for the unspecified OS |
||
| 183 | */ |
||
| 184 | public function testDoNotExecuteOnWrongOs() |
||
| 185 | { |
||
| 186 | |||
| 187 | // Process |
||
| 188 | $this->executeTarget(__FUNCTION__); |
||
| 189 | $this->assertInLogs('was not found in the specified list of valid OSes: unknownos'); |
||
| 190 | |||
| 191 | $this->assertNotContains('this should not be executed', $this->getOutput()); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Tests the OS execution for the specified OS list |
||
| 196 | */ |
||
| 197 | public function testExecuteOnCorrectOs() |
||
| 198 | { |
||
| 199 | $this->executeTarget(__FUNCTION__); |
||
| 200 | $this->assertInLogs('this should be executed'); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Tests the dir changing on a non-existent directory |
||
| 205 | */ |
||
| 206 | public function testFailOnNonExistingDir() |
||
| 207 | { |
||
| 208 | $nonExistentDir = $this->project->getProperty('php.tmpdir') . DIRECTORY_SEPARATOR |
||
| 209 | . 'non' . DIRECTORY_SEPARATOR |
||
| 210 | . 'existent' . DIRECTORY_SEPARATOR |
||
| 211 | . 'dir'; |
||
| 212 | |||
| 213 | return $this->expectBuildExceptionContaining( |
||
| 214 | __FUNCTION__, |
||
| 215 | __FUNCTION__, |
||
| 216 | "'$nonExistentDir' is not a valid directory" |
||
| 217 | ); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Tests the dir changing on an existent directory |
||
| 222 | */ |
||
| 223 | public function testChangeToDir() |
||
| 224 | { |
||
| 225 | |||
| 226 | // Validating the OS platform |
||
| 227 | if ($this->windows) { |
||
| 228 | $this->markTestSkipped("Windows does not have 'ls'"); |
||
| 229 | } |
||
| 230 | |||
| 231 | $this->executeTarget(__FUNCTION__); |
||
| 232 | $this->assertInLogs('Working directory change successful'); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Tests the failonerror/checkreturn value for 'true' |
||
| 237 | */ |
||
| 238 | public function testCheckreturnTrue() |
||
| 239 | { |
||
| 240 | |||
| 241 | // Validating the OS platform |
||
| 242 | if ($this->windows) { |
||
| 243 | $this->markTestSkipped("Windows does not have '/bin/true'"); |
||
| 244 | } |
||
| 245 | |||
| 246 | $this->executeTarget(__FUNCTION__); |
||
| 247 | $this->assertTrue(true); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Tests the failonerror/checkreturn value for 'false' |
||
| 252 | */ |
||
| 253 | public function testCheckreturnFalse() |
||
| 254 | { |
||
| 255 | |||
| 256 | // Validating the OS platform |
||
| 257 | if ($this->windows) { |
||
| 258 | $this->markTestSkipped("Windows does not have '/bin/false'"); |
||
| 259 | } |
||
| 260 | |||
| 261 | return $this->expectBuildExceptionContaining(__FUNCTION__, __FUNCTION__, 'Task exited with code (1)'); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Tests the outputProperty setting |
||
| 266 | */ |
||
| 267 | public function testOutputProperty() |
||
| 268 | { |
||
| 269 | $this->executeTarget(__FUNCTION__); |
||
| 270 | $this->assertInLogs('The output property\'s value is: "foo"'); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Tests the returnProperty setting |
||
| 275 | */ |
||
| 276 | public function testReturnProperty() |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Tests the command escaping for execution |
||
| 284 | */ |
||
| 285 | public function testEscape() |
||
| 286 | { |
||
| 287 | $this->executeTarget(__FUNCTION__); |
||
| 288 | $this->assertInLogs( |
||
| 289 | $this->windows |
||
| 290 | ? (escapeshellarg('echo') . ' ' . escapeshellarg('foo') . " " . escapeshellarg('|') . " " . escapeshellarg('cat')) |
||
| 291 | : ("'echo' 'foo' '|' 'cat'")); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Tests the command execution with 'passthru' function |
||
| 296 | */ |
||
| 297 | public function testPassThru() |
||
| 298 | { |
||
| 299 | $this->executeTarget(__FUNCTION__); |
||
| 300 | $this->assertInLogs('Executing command:'); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Tests the output file functionality |
||
| 305 | */ |
||
| 306 | public function testOutput() |
||
| 307 | { |
||
| 308 | |||
| 309 | // Getting a temp. file |
||
| 310 | $tempfile = tempnam(sys_get_temp_dir(), 'phing-exectest-'); |
||
| 311 | |||
| 312 | // Setting the property |
||
| 313 | $this->project->setProperty('execTmpFile', $tempfile); |
||
| 314 | $this->executeTarget(__FUNCTION__); |
||
| 315 | |||
| 316 | // Validating the output |
||
| 317 | $output = @file_get_contents($tempfile); |
||
| 318 | @unlink($tempfile); |
||
|
1 ignored issue
–
show
|
|||
| 319 | $this->assertEquals('outfoo', rtrim($output)); |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Tests the error file functionality |
||
| 324 | */ |
||
| 325 | public function testError() |
||
| 326 | { |
||
| 327 | |||
| 328 | // Validating the OS platform |
||
| 329 | if ($this->windows) { |
||
| 330 | $this->markTestSkipped("The script is unlikely to run on MS Windows"); |
||
| 331 | } |
||
| 332 | |||
| 333 | // Getting a temp. file |
||
| 334 | $tempfile = tempnam(sys_get_temp_dir(), 'phing-exectest-'); |
||
| 335 | |||
| 336 | $scriptFile = getcwd() . "/error_output.sh"; |
||
| 337 | file_put_contents($scriptFile, "echo errfoo 1>&2"); |
||
| 338 | chmod($scriptFile, 0744); |
||
| 339 | |||
| 340 | // Setting the property |
||
| 341 | $this->project->setProperty('executable', $scriptFile); |
||
| 342 | $this->project->setProperty('execTmpFile', $tempfile); |
||
| 343 | $this->executeTarget(__FUNCTION__); |
||
| 344 | |||
| 345 | // Validating the output |
||
| 346 | $output = @file_get_contents($tempfile); |
||
| 347 | @unlink($tempfile); |
||
|
1 ignored issue
–
show
|
|||
| 348 | @unlink($scriptFile); |
||
| 349 | $this->assertEquals("errfoo", rtrim($output)); |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Tests the execution with the background process spawning |
||
| 354 | */ |
||
| 355 | public function testSpawn() |
||
| 356 | { |
||
| 357 | // Validating the OS platform |
||
| 358 | if ($this->windows) { |
||
| 359 | $this->markTestSkipped("Windows does not have /bin/sleep"); |
||
| 360 | } |
||
| 361 | |||
| 362 | // Process |
||
| 363 | $start = time(); |
||
| 364 | $this->executeTarget(__FUNCTION__); |
||
| 365 | $end = time(); |
||
| 366 | $this->assertLessThan( |
||
| 367 | 4, |
||
| 368 | ($end - $start), |
||
| 369 | 'Execution time should be lower than 4 seconds, otherwise spawning did not work' |
||
| 370 | ); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Tests the nested arguments specified for the execution |
||
| 375 | */ |
||
| 376 | public function testNestedArgs() |
||
| 377 | { |
||
| 378 | $this->executeTarget(__FUNCTION__); |
||
| 379 | $this->assertInLogs('echo Hello World'); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Tests the missing/unspecified executable information |
||
| 384 | */ |
||
| 385 | public function testMissingExecutable() |
||
| 386 | { |
||
| 387 | $this->expectBuildExceptionContaining(__FUNCTION__, __FUNCTION__, 'Please provide "executable" information'); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Tests the escape functionality for special characters in argument |
||
| 392 | */ |
||
| 393 | public function testEscapedArg() |
||
| 394 | { |
||
| 395 | $this->executeTarget(__FUNCTION__); |
||
| 396 | $this->assertPropertyEquals('outval', $this->windows ? 'abc$b3 SB' : 'abc$b3!SB'); |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Tests the relative source filenames functionality |
||
| 401 | */ |
||
| 402 | public function testRelativeSourceFilenames() |
||
| 403 | { |
||
| 404 | // Validating the OS platform |
||
| 405 | if ($this->windows) { |
||
| 406 | $this->markTestSkipped("Windows does not have 'ls'"); |
||
| 407 | } |
||
| 408 | |||
| 409 | $this->executeTarget(__FUNCTION__); |
||
| 410 | $this->assertNotInLogs('/etc/'); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Tests the source filename addition functionality |
||
| 415 | */ |
||
| 416 | public function testSourceFilename() |
||
| 417 | { |
||
| 418 | |||
| 419 | // Validating the OS platform |
||
| 420 | if ($this->windows) { |
||
| 421 | $this->markTestSkipped("Windows does not have 'ls'"); |
||
| 422 | } |
||
| 423 | |||
| 424 | $this->executeTarget(__FUNCTION__); |
||
| 425 | // As the addsourcefilename is 'off', only the executable should be processed in the execution |
||
| 426 | $this->assertInLogs('Executing command: ls'); |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Tests the output file append functionality |
||
| 431 | */ |
||
| 432 | public function testOutputAppend() |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Tests the parallel configuration |
||
| 450 | */ |
||
| 451 | public function testParallel() |
||
| 452 | { |
||
| 453 | $this->executeTarget(__FUNCTION__); |
||
| 454 | $messages = []; |
||
| 455 | foreach ($this->logBuffer as $log) { |
||
| 456 | $messages[] = $log['message']; |
||
| 457 | } |
||
| 458 | $this->assertEquals(1, substr_count(implode("\n", $messages), 'Executing command:')); |
||
| 459 | } |
||
| 460 | |||
| 461 | public function testMapperSupport() |
||
| 462 | { |
||
| 463 | // Getting a temp. file |
||
| 464 | $tempfile = tempnam(sys_get_temp_dir(), 'phing-exectest-'); |
||
| 465 | |||
| 466 | // Setting the property |
||
| 467 | $this->project->setProperty('execTmpFile', $tempfile); |
||
| 468 | |||
| 469 | $this->executeTarget(__FUNCTION__); |
||
| 470 | $messages = []; |
||
| 471 | foreach ($this->logBuffer as $log) { |
||
| 472 | $messages[] = $log['message']; |
||
| 473 | } |
||
| 474 | $this->assertContains('Applied echo to 4 files and 0 directories.', $messages); |
||
| 475 | } |
||
| 476 | |||
| 477 | |||
| 478 | /**********************************************************************************/ |
||
| 479 | /************************** H E L P E R M E T H O D S ****************************/ |
||
| 480 | /**********************************************************************************/ |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param string $name |
||
| 484 | * @return Target |
||
| 485 | * @throws Exception |
||
| 486 | */ |
||
| 487 | protected function getTargetByName($name) |
||
| 488 | { |
||
| 489 | foreach ($this->project->getTargets() as $target) { |
||
| 490 | if ($target->getName() == $name) { |
||
| 491 | return $target; |
||
| 492 | } |
||
| 493 | } |
||
| 494 | throw new Exception(sprintf('Target "%s" not found', $name)); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param string $target |
||
| 499 | * @param string $taskName |
||
| 500 | * @param int $pos |
||
| 501 | * @return Task |
||
| 502 | * @throws Exception |
||
| 503 | */ |
||
| 504 | protected function getTaskFromTarget($target, $taskName, $pos = 0) |
||
| 505 | { |
||
| 506 | $rchildren = new ReflectionProperty(get_class($target), 'children'); |
||
| 507 | $rchildren->setAccessible(true); |
||
| 508 | $n = -1; |
||
| 509 | foreach ($rchildren->getValue($target) as $child) { |
||
| 510 | if ($child instanceof Task && ++$n == $pos) { |
||
| 511 | return $child; |
||
| 512 | } |
||
| 513 | } |
||
| 514 | |||
| 515 | throw new Exception(sprintf('%s #%d not found in task', $taskName, $pos)); |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @param string $target |
||
| 520 | * @param string $task |
||
| 521 | * @return Task |
||
| 522 | */ |
||
| 523 | protected function getConfiguredTask($target, $task) |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * @param string $property |
||
| 538 | * @param string $value |
||
| 539 | * @param string $propertyName |
||
| 540 | */ |
||
| 541 | protected function assertAttributeIsSetTo($property, $value, $propertyName = null) |
||
| 542 | { |
||
| 543 | $task = $this->getConfiguredTask('testPropertySet' . ucfirst($property), 'ApplyTask'); |
||
| 544 | |||
| 545 | $propertyName = ($propertyName === null) ? $property : $propertyName; |
||
| 546 | $rprop = new ReflectionProperty('ApplyTask', $propertyName); |
||
| 547 | $rprop->setAccessible(true); |
||
| 548 | $this->assertEquals($value, $rprop->getValue($task)); |
||
| 549 | } |
||
| 550 | } |
||
| 551 |