| Total Complexity | 59 |
| Total Lines | 347 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ExecTaskTest 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 ExecTaskTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class ExecTaskTest extends BuildFileTest |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Whether test is being run on windows |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | protected $windows; |
||
| 34 | |||
| 35 | public function setUp(): void |
||
| 36 | { |
||
| 37 | $this->configureProject( |
||
| 38 | PHING_TEST_BASE . '/etc/tasks/system/ExecTest.xml' |
||
|
|
|||
| 39 | ); |
||
| 40 | $this->windows = strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'; |
||
| 41 | } |
||
| 42 | |||
| 43 | protected function getTargetByName($name) |
||
| 44 | { |
||
| 45 | foreach ($this->project->getTargets() as $target) { |
||
| 46 | if ($target->getName() == $name) { |
||
| 47 | return $target; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | throw new Exception(sprintf('Target "%s" not found', $name)); |
||
| 51 | } |
||
| 52 | |||
| 53 | protected function getTaskFromTarget($target, $taskname, $pos = 0) |
||
| 54 | { |
||
| 55 | $rchildren = new ReflectionProperty(get_class($target), 'children'); |
||
| 56 | $rchildren->setAccessible(true); |
||
| 57 | $n = -1; |
||
| 58 | foreach ($rchildren->getValue($target) as $child) { |
||
| 59 | if ($child instanceof Task && ++$n == $pos) { |
||
| 60 | return $child; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | throw new Exception( |
||
| 64 | sprintf('%s #%d not found in task', $taskname, $pos) |
||
| 65 | ); |
||
| 66 | } |
||
| 67 | |||
| 68 | protected function getConfiguredTask($target, $task, $pos = 0) |
||
| 69 | { |
||
| 70 | $target = $this->getTargetByName($target); |
||
| 71 | $task = $this->getTaskFromTarget($target, $task); |
||
| 72 | $task->maybeConfigure(); |
||
| 73 | |||
| 74 | return $task; |
||
| 75 | } |
||
| 76 | |||
| 77 | protected function assertAttributeIsSetTo($property, $value, $propertyName = null) |
||
| 78 | { |
||
| 79 | $task = $this->getConfiguredTask( |
||
| 80 | 'testPropertySet' . ucfirst($property), |
||
| 81 | 'ExecTask' |
||
| 82 | ); |
||
| 83 | |||
| 84 | if ($propertyName === null) { |
||
| 85 | $propertyName = $property; |
||
| 86 | } |
||
| 87 | |||
| 88 | if ($task instanceof UnknownElement) { |
||
| 89 | $task = $task->getRuntimeConfigurableWrapper()->getProxy(); |
||
| 90 | } |
||
| 91 | |||
| 92 | $rprop = new ReflectionProperty('ExecTask', $propertyName); |
||
| 93 | $rprop->setAccessible(true); |
||
| 94 | $this->assertEquals($value, $rprop->getValue($task)); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function testPropertySetCommandline() |
||
| 98 | { |
||
| 99 | $this->assertAttributeIsSetTo('commandline', new Commandline("echo 'foo'")); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function testPropertySetDir() |
||
| 103 | { |
||
| 104 | $this->assertAttributeIsSetTo( |
||
| 105 | 'dir', |
||
| 106 | new PhingFile( |
||
| 107 | realpath(__DIR__ . '/../../../../etc/tasks/system') |
||
| 108 | ) |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | |||
| 112 | public function testPropertySetOs() |
||
| 113 | { |
||
| 114 | $this->assertAttributeIsSetTo('os', "linux"); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function testPropertySetEscape() |
||
| 118 | { |
||
| 119 | $this->assertAttributeIsSetTo('escape', true); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function testPropertySetLogoutput() |
||
| 123 | { |
||
| 124 | $this->assertAttributeIsSetTo('logoutput', true, 'logOutput'); |
||
| 125 | } |
||
| 126 | |||
| 127 | public function testPropertySetPassthru() |
||
| 128 | { |
||
| 129 | $this->assertAttributeIsSetTo('passthru', true); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function testPropertySetSpawn() |
||
| 133 | { |
||
| 134 | $this->assertAttributeIsSetTo('spawn', true); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function testPropertySetReturnProperty() |
||
| 138 | { |
||
| 139 | $this->assertAttributeIsSetTo('returnProperty', 'retval'); |
||
| 140 | } |
||
| 141 | |||
| 142 | public function testPropertySetOutputProperty() |
||
| 143 | { |
||
| 144 | $this->assertAttributeIsSetTo('outputProperty', 'outval'); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function testPropertySetCheckReturn() |
||
| 148 | { |
||
| 149 | $this->assertAttributeIsSetTo('checkreturn', true); |
||
| 150 | } |
||
| 151 | |||
| 152 | public function testPropertySetOutput() |
||
| 153 | { |
||
| 154 | $this->assertAttributeIsSetTo( |
||
| 155 | 'output', |
||
| 156 | new PhingFile( |
||
| 157 | realpath(__DIR__ . '/../../../../etc/tasks/system') |
||
| 158 | . '/outputfilename' |
||
| 159 | ) |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function testPropertySetError() |
||
| 164 | { |
||
| 165 | $this->assertAttributeIsSetTo( |
||
| 166 | 'error', |
||
| 167 | new PhingFile( |
||
| 168 | realpath(__DIR__ . '/../../../../etc/tasks/system') |
||
| 169 | . '/errorfilename' |
||
| 170 | ) |
||
| 171 | ); |
||
| 172 | } |
||
| 173 | |||
| 174 | public function testPropertySetLevelError() |
||
| 175 | { |
||
| 176 | $this->assertAttributeIsSetTo('levelError', Project::MSG_ERR, 'logLevel'); |
||
| 177 | } |
||
| 178 | |||
| 179 | public function testPropertySetLevelWarning() |
||
| 180 | { |
||
| 181 | $this->assertAttributeIsSetTo('levelWarning', Project::MSG_WARN, 'logLevel'); |
||
| 182 | } |
||
| 183 | |||
| 184 | public function testPropertySetLevelInfo() |
||
| 185 | { |
||
| 186 | $this->assertAttributeIsSetTo('levelInfo', Project::MSG_INFO, 'logLevel'); |
||
| 187 | } |
||
| 188 | |||
| 189 | public function testPropertySetLevelVerbose() |
||
| 190 | { |
||
| 191 | $this->assertAttributeIsSetTo('levelVerbose', Project::MSG_VERBOSE, 'logLevel'); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function testPropertySetLevelDebug() |
||
| 195 | { |
||
| 196 | $this->assertAttributeIsSetTo('levelDebug', Project::MSG_DEBUG, 'logLevel'); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @expectedException BuildException |
||
| 201 | * @expectedExceptionMessage Unknown log level "unknown" |
||
| 202 | */ |
||
| 203 | public function testPropertySetLevelUnknown() |
||
| 204 | { |
||
| 205 | $this->getConfiguredTask('testPropertySetLevelUnknown', 'ExecTask'); |
||
| 206 | } |
||
| 207 | |||
| 208 | public function testDoNotExecuteOnWrongOs() |
||
| 209 | { |
||
| 210 | $this->executeTarget(__FUNCTION__); |
||
| 211 | $this->assertInLogs('not found in the specified list of valid OSes: unknownos'); |
||
| 212 | $this->assertNotContains( |
||
| 213 | 'this should not be executed', |
||
| 214 | $this->getOutput() |
||
| 215 | ); |
||
| 216 | } |
||
| 217 | |||
| 218 | public function testDoNotExecuteOnWrongOsFamily() |
||
| 219 | { |
||
| 220 | $this->expectBuildException(__FUNCTION__, "Don't know how to detect os family 'unknownos'"); |
||
| 221 | } |
||
| 222 | |||
| 223 | public function testExecuteOnCorrectOs() |
||
| 224 | { |
||
| 225 | $this->executeTarget(__FUNCTION__); |
||
| 226 | $this->assertInLogs('this should be executed'); |
||
| 227 | } |
||
| 228 | |||
| 229 | public function testExecuteOnCorrectOsFamily() |
||
| 230 | { |
||
| 231 | $this->executeTarget(__FUNCTION__); |
||
| 232 | $this->assertInLogs('this should be executed'); |
||
| 233 | } |
||
| 234 | |||
| 235 | public function testFailOnNonExistingDir() |
||
| 236 | { |
||
| 237 | try { |
||
| 238 | $this->executeTarget(__FUNCTION__); |
||
| 239 | $this->fail('Expected BuildException was not thrown'); |
||
| 240 | } catch (BuildException $e) { |
||
| 241 | $this->assertContains( |
||
| 242 | str_replace('/', DIRECTORY_SEPARATOR, "'/this/dir/does/not/exist' does not exist"), |
||
| 243 | $e->getMessage() |
||
| 244 | ); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | public function testChangeToDir() |
||
| 249 | { |
||
| 250 | if ($this->windows) { |
||
| 251 | $this->markTestSkipped("Windows does not have 'ls'"); |
||
| 252 | } |
||
| 253 | $this->executeTarget(__FUNCTION__); |
||
| 254 | $this->assertInLogs('ExecTaskTest.php'); |
||
| 255 | } |
||
| 256 | |||
| 257 | public function testCheckreturnTrue() |
||
| 258 | { |
||
| 259 | if (FileSystem::getFileSystem()->which('true') === false) { |
||
| 260 | $this->markTestSkipped("'true' not found."); |
||
| 261 | } |
||
| 262 | $this->executeTarget(__FUNCTION__); |
||
| 263 | $this->assertTrue(true); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @expectedException BuildException |
||
| 268 | * @expectedExceptionMessage exec returned: 1 |
||
| 269 | */ |
||
| 270 | public function testCheckreturnFalse() |
||
| 271 | { |
||
| 272 | if (FileSystem::getFileSystem()->which('false') === false) { |
||
| 273 | $this->markTestSkipped("'false' not found."); |
||
| 274 | } |
||
| 275 | $this->executeTarget(__FUNCTION__); |
||
| 276 | } |
||
| 277 | |||
| 278 | public function testOutputProperty() |
||
| 282 | } |
||
| 283 | |||
| 284 | public function testReturnProperty() |
||
| 285 | { |
||
| 286 | $this->executeTarget(__FUNCTION__); |
||
| 287 | $this->assertInLogs('The return property\'s value is: "1"'); |
||
| 288 | } |
||
| 289 | |||
| 290 | public function testEscape() |
||
| 291 | { |
||
| 292 | $this->executeTarget(__FUNCTION__); |
||
| 293 | $this->assertInLogs($this->windows ? '"foo" "|" "cat"' : 'foo | cat'); |
||
| 294 | } |
||
| 295 | |||
| 296 | public function testPassthru() |
||
| 297 | { |
||
| 298 | ob_start(); |
||
| 299 | $this->executeTarget(__FUNCTION__); |
||
| 300 | $out = ob_get_clean(); |
||
| 301 | $this->assertEquals("foo", rtrim($out, " \r\n")); |
||
| 302 | //foo should not be in logs, except for the logged command |
||
| 303 | $this->assertInLogs('echo foo'); |
||
| 304 | $this->assertNotContains('foo', $this->logBuffer); |
||
| 305 | } |
||
| 306 | |||
| 307 | public function testOutput() |
||
| 308 | { |
||
| 309 | $file = tempnam(sys_get_temp_dir(), 'phing-exectest-'); |
||
| 310 | $this->project->setProperty('execTmpFile', $file); |
||
| 311 | $this->executeTarget(__FUNCTION__); |
||
| 312 | $this->assertContains('outfoo', file_get_contents($file)); |
||
| 313 | unlink($file); |
||
| 314 | } |
||
| 315 | |||
| 316 | public function testError() |
||
| 317 | { |
||
| 318 | $file = tempnam(sys_get_temp_dir(), 'phing-exectest-'); |
||
| 319 | $this->project->setProperty('execTmpFile', $file); |
||
| 320 | $this->executeTarget(__FUNCTION__); |
||
| 321 | $this->assertContains('errfoo', file_get_contents($file)); |
||
| 322 | unlink($file); |
||
| 323 | } |
||
| 324 | |||
| 325 | public function testSpawn() |
||
| 326 | { |
||
| 327 | $start = time(); |
||
| 328 | $this->executeTarget(__FUNCTION__); |
||
| 329 | $end = time(); |
||
| 330 | $this->assertLessThan( |
||
| 331 | 4, |
||
| 332 | $end - $start, |
||
| 333 | 'Time between start and end should be lower than 4 seconds' |
||
| 334 | . ' - otherwise it looks as spawning did not work' |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | |||
| 338 | public function testNestedArg() |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @expectedException BuildException |
||
| 346 | * @expectedExceptionMessage ExecTask: Please provide "executable" |
||
| 347 | */ |
||
| 348 | public function testMissingExecutableAndCommand() |
||
| 349 | { |
||
| 350 | $this->executeTarget(__FUNCTION__); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Inspired by {@link http://www.phing.info/trac/ticket/833} |
||
| 355 | */ |
||
| 356 | public function testEscapedArg() |
||
| 357 | { |
||
| 358 | $this->executeTarget(__FUNCTION__); |
||
| 359 | $this->assertPropertyEquals('outval', $this->windows ? 'abc$b3 SB' : 'abc$b3!SB'); |
||
| 360 | } |
||
| 361 | |||
| 362 | public function testEscapedArgWithoutWhitespace() |
||
| 363 | { |
||
| 364 | $arg = 'foo|bar'; |
||
| 365 | $this->executeTarget(__FUNCTION__); |
||
| 366 | $this->assertInLogs($this->windows ? '"echo" "foo|bar" 2>&1' : '\'echo\' \'foo|bar\' 2>&1'); |
||
| 367 | $this->assertNotInLogs($this->windows ? 'echo " foo|bar " 2>&1' : 'echo \' foo|bar \' 2>&1'); |
||
| 368 | } |
||
| 369 | |||
| 370 | public function testNestedEnv() |
||
| 374 | } |
||
| 375 | } |
||
| 376 |