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