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