Total Complexity | 111 |
Total Lines | 689 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PHPUnitTask 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 PHPUnitTask, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class PHPUnitTask extends Task |
||
43 | { |
||
44 | private $batchtests = []; |
||
45 | /** |
||
46 | * @var FormatterElement[] $formatters |
||
47 | */ |
||
48 | private $formatters = []; |
||
49 | private $bootstrap = ""; |
||
50 | private $haltonerror = false; |
||
51 | private $haltonfailure = false; |
||
52 | private $haltonincomplete = false; |
||
53 | private $haltonskipped = false; |
||
54 | private $haltondefect = false; |
||
55 | private $haltonwarning = false; |
||
56 | private $haltonrisky = false; |
||
57 | private $errorproperty; |
||
58 | private $failureproperty; |
||
59 | private $incompleteproperty; |
||
60 | private $skippedproperty; |
||
61 | private $warningproperty; |
||
62 | private $riskyproperty; |
||
63 | private $printsummary = false; |
||
64 | private $testfailed = false; |
||
65 | private $testfailuremessage = ""; |
||
66 | private $codecoverage = null; |
||
67 | private $groups = []; |
||
68 | private $excludeGroups = []; |
||
69 | private $processIsolation = false; |
||
70 | private $usecustomerrorhandler = true; |
||
71 | private $listeners = []; |
||
72 | |||
73 | /** |
||
74 | * @var string |
||
75 | */ |
||
76 | private $pharLocation = ""; |
||
77 | |||
78 | /** |
||
79 | * @var File |
||
80 | */ |
||
81 | private $configuration = null; |
||
82 | |||
83 | /** |
||
84 | * @var \PHPUnit\TextUI\XmlConfiguration\CodeCoverage\CodeCoverage |
||
85 | */ |
||
86 | private $codeCoverageConfig = null; |
||
87 | |||
88 | /** |
||
89 | * Initialize Task. |
||
90 | * This method includes any necessary PHPUnit libraries and triggers |
||
91 | * appropriate error if they cannot be found. This is not done in header |
||
92 | * because we may want this class to be loaded w/o triggering an error. |
||
93 | */ |
||
94 | public function init() |
||
95 | { |
||
96 | } |
||
97 | |||
98 | private function loadPHPUnit() |
||
99 | { |
||
100 | if (!empty($this->pharLocation)) { |
||
101 | // nasty but necessary: reorder the autoloaders so the one in the PHAR gets priority |
||
102 | $autoloadFunctions = spl_autoload_functions(); |
||
103 | $composerAutoloader = null; |
||
104 | if (get_class($autoloadFunctions[0][0]) === 'Composer\Autoload\ClassLoader') { |
||
105 | $composerAutoloader = $autoloadFunctions[0]; |
||
106 | spl_autoload_unregister($composerAutoloader); |
||
107 | } |
||
108 | |||
109 | $GLOBALS['_SERVER']['SCRIPT_NAME'] = '-'; |
||
110 | ob_start(); |
||
111 | @include $this->pharLocation; |
||
112 | ob_end_clean(); |
||
113 | |||
114 | if ($composerAutoloader !== null) { |
||
115 | spl_autoload_register($composerAutoloader); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | if (!class_exists('PHPUnit\Runner\Version')) { |
||
120 | throw new BuildException("PHPUnitTask requires PHPUnit to be installed", $this->getLocation()); |
||
121 | } |
||
122 | |||
123 | if ( |
||
124 | class_exists('\PHPUnit\Runner\Version', false) && |
||
125 | version_compare(\PHPUnit\Runner\Version::id(), '9.0.0', '<') |
||
126 | ) { |
||
127 | throw new BuildException("Phing only supports PHPUnit 9+"); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Sets the name of a bootstrap file that is run before |
||
133 | * executing the tests |
||
134 | * |
||
135 | * @param string $bootstrap the name of the bootstrap file |
||
136 | */ |
||
137 | public function setBootstrap($bootstrap) |
||
138 | { |
||
139 | $this->bootstrap = $bootstrap; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param $value |
||
144 | */ |
||
145 | public function setErrorproperty($value) |
||
146 | { |
||
147 | $this->errorproperty = $value; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param $value |
||
152 | */ |
||
153 | public function setFailureproperty($value) |
||
154 | { |
||
155 | $this->failureproperty = $value; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param $value |
||
160 | */ |
||
161 | public function setIncompleteproperty($value) |
||
162 | { |
||
163 | $this->incompleteproperty = $value; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param $value |
||
168 | */ |
||
169 | public function setSkippedproperty($value) |
||
170 | { |
||
171 | $this->skippedproperty = $value; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param $value |
||
176 | */ |
||
177 | public function setRiskyproperty($value) |
||
178 | { |
||
179 | $this->riskyproperty = $value; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @param $value |
||
184 | */ |
||
185 | public function setWarningproperty($value) |
||
186 | { |
||
187 | $this->riskyproperty = $value; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function getHaltondefect(): bool |
||
194 | { |
||
195 | return $this->haltondefect; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param bool $haltondefect |
||
200 | */ |
||
201 | public function setHaltondefect(bool $haltondefect): void |
||
202 | { |
||
203 | $this->haltondefect = $haltondefect; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @return bool |
||
208 | */ |
||
209 | public function getHaltonwarning(): bool |
||
210 | { |
||
211 | return $this->haltonwarning; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @param bool $haltonwarning |
||
216 | */ |
||
217 | public function setHaltonwarning(bool $haltonwarning): void |
||
218 | { |
||
219 | $this->haltonwarning = $haltonwarning; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function getHaltonrisky(): bool |
||
226 | { |
||
227 | return $this->haltonrisky; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @param bool $haltonrisky |
||
232 | */ |
||
233 | public function setHaltonrisky(bool $haltonrisky): void |
||
234 | { |
||
235 | $this->haltonrisky = $haltonrisky; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param $value |
||
240 | */ |
||
241 | public function setHaltonerror($value) |
||
242 | { |
||
243 | $this->haltonerror = $value; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @param $value |
||
248 | */ |
||
249 | public function setHaltonfailure($value) |
||
250 | { |
||
251 | $this->haltonfailure = $value; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @return bool |
||
256 | */ |
||
257 | public function getHaltonfailure() |
||
258 | { |
||
259 | return $this->haltonfailure; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * @param $value |
||
264 | */ |
||
265 | public function setHaltonincomplete($value) |
||
266 | { |
||
267 | $this->haltonincomplete = $value; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function getHaltonincomplete() |
||
274 | { |
||
275 | return $this->haltonincomplete; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @param $value |
||
280 | */ |
||
281 | public function setHaltonskipped($value) |
||
282 | { |
||
283 | $this->haltonskipped = $value; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @return bool |
||
288 | */ |
||
289 | public function getHaltonskipped() |
||
290 | { |
||
291 | return $this->haltonskipped; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @param $printsummary |
||
296 | */ |
||
297 | public function setPrintsummary($printsummary) |
||
298 | { |
||
299 | $this->printsummary = $printsummary; |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @param $codecoverage |
||
304 | */ |
||
305 | public function setCodecoverage($codecoverage) |
||
306 | { |
||
307 | $this->codecoverage = $codecoverage; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @param $processIsolation |
||
312 | */ |
||
313 | public function setProcessIsolation($processIsolation) |
||
314 | { |
||
315 | $this->processIsolation = $processIsolation; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @param $usecustomerrorhandler |
||
320 | */ |
||
321 | public function setUseCustomErrorHandler($usecustomerrorhandler) |
||
322 | { |
||
323 | $this->usecustomerrorhandler = $usecustomerrorhandler; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * @param $groups |
||
328 | */ |
||
329 | public function setGroups($groups) |
||
330 | { |
||
331 | $token = ' ,;'; |
||
332 | $this->groups = []; |
||
333 | $tok = strtok($groups, $token); |
||
334 | while ($tok !== false) { |
||
335 | $this->groups[] = $tok; |
||
336 | $tok = strtok($token); |
||
337 | } |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @param $excludeGroups |
||
342 | */ |
||
343 | public function setExcludeGroups($excludeGroups) |
||
351 | } |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Add a new formatter to all tests of this task. |
||
356 | * |
||
357 | * @param FormatterElement $fe formatter element |
||
358 | */ |
||
359 | public function addFormatter(FormatterElement $fe) |
||
360 | { |
||
361 | $fe->setParent($this); |
||
362 | $this->formatters[] = $fe; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Add a new listener to all tests of this taks |
||
367 | * |
||
368 | * @param $listener |
||
369 | */ |
||
370 | private function addListener($listener) |
||
371 | { |
||
372 | $this->listeners[] = $listener; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * @param File $configuration |
||
377 | */ |
||
378 | public function setConfiguration(File $configuration) |
||
379 | { |
||
380 | $this->configuration = $configuration; |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * @param string $pharLocation |
||
385 | */ |
||
386 | public function setPharLocation($pharLocation) |
||
387 | { |
||
388 | $this->pharLocation = $pharLocation; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Load and processes the PHPUnit configuration |
||
393 | * |
||
394 | * @param $configuration |
||
395 | * @return mixed |
||
396 | * @throws ReflectionException |
||
397 | * @throws BuildException |
||
398 | */ |
||
399 | protected function handlePHPUnitConfiguration(File $configuration) |
||
400 | { |
||
401 | if (!$configuration->exists()) { |
||
402 | throw new BuildException("Unable to find PHPUnit configuration file '" . (string) $configuration . "'"); |
||
403 | } |
||
404 | |||
405 | $config = (new Loader())->load($configuration->getAbsolutePath()); |
||
406 | $phpunit = $config->phpunit(); |
||
407 | |||
408 | if ($phpunit->hasBootstrap()) { |
||
409 | $this->setBootstrap($phpunit->bootstrap()); |
||
410 | } |
||
411 | $this->setHaltonfailure($phpunit->stopOnFailure()); |
||
412 | $this->setHaltonerror($phpunit->stopOnError()); |
||
413 | $this->setHaltonskipped($phpunit->stopOnSkipped()); |
||
414 | $this->setHaltonincomplete($phpunit->stopOnIncomplete()); |
||
415 | $this->setHaltondefect($phpunit->stopOnDefect()); |
||
416 | $this->setHaltonwarning($phpunit->stopOnWarning()); |
||
417 | $this->setHaltonrisky($phpunit->stopOnRisky()); |
||
418 | $this->setProcessIsolation($phpunit->processIsolation()); |
||
419 | |||
420 | foreach ($config->listeners() as $listener) { |
||
421 | if ( |
||
422 | !class_exists($listener->className(), false) |
||
423 | && $listener->hasSourceFile() |
||
424 | ) { |
||
425 | include_once $listener->sourceFile(); |
||
426 | } |
||
427 | |||
428 | if (class_exists($listener->className())) { |
||
429 | if ($listener->hasArguments()) { |
||
430 | $listener = (new $listener->className())(); |
||
431 | } else { |
||
432 | $listenerClass = new ReflectionClass( |
||
433 | $listener->className() |
||
434 | ); |
||
435 | $listener = $listenerClass->newInstanceArgs( |
||
436 | $listener->arguments() |
||
437 | ); |
||
438 | } |
||
439 | |||
440 | if ($listener instanceof \PHPUnit\Framework\TestListener) { |
||
441 | $this->addListener($listener); |
||
442 | } |
||
443 | } |
||
444 | } |
||
445 | |||
446 | $this->codeCoverageConfig = $config->codeCoverage(); |
||
447 | return $phpunit; |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * The main entry point |
||
452 | * |
||
453 | * @throws BuildException |
||
454 | */ |
||
455 | public function main() |
||
509 | } |
||
510 | } |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * @param $suite |
||
515 | * @throws BuildException |
||
516 | * @throws ReflectionException |
||
517 | */ |
||
518 | protected function execute($suite) |
||
519 | { |
||
520 | $runner = new PHPUnitTestRunner9( |
||
521 | $this->project, |
||
522 | $this->groups, |
||
523 | $this->excludeGroups, |
||
524 | $this->processIsolation |
||
525 | ); |
||
526 | |||
527 | if ($this->codecoverage) { |
||
528 | /** |
||
529 | * Add some defaults to the PHPUnit filter |
||
530 | */ |
||
531 | $pwd = __DIR__; |
||
532 | $path = realpath($pwd . '/../../../'); |
||
533 | |||
534 | if (class_exists('\SebastianBergmann\CodeCoverage\Filter')) { |
||
535 | $filter = new \SebastianBergmann\CodeCoverage\Filter(); |
||
536 | if (method_exists($filter, 'addDirectoryToBlacklist')) { |
||
537 | $filter->addDirectoryToBlacklist($path); |
||
538 | } |
||
539 | if (class_exists('\SebastianBergmann\CodeCoverage\CodeCoverage')) { |
||
540 | if (null !== $this->codeCoverageConfig) { |
||
541 | // Update filters |
||
542 | foreach ($this->codeCoverageConfig->files()->asArray() as $file) { |
||
543 | $filter->includeFile($file->path()); |
||
544 | } |
||
545 | foreach ($this->codeCoverageConfig->directories()->asArray() as $dir) { |
||
546 | $filter->includeDirectory($dir->path(), $dir->suffix(), $dir->prefix()); |
||
547 | } |
||
548 | foreach ($this->codeCoverageConfig->excludeFiles()->asArray() as $file) { |
||
549 | $filter->excludeFile($file->path()); |
||
550 | } |
||
551 | foreach ($this->codeCoverageConfig->excludeDirectories()->asArray() as $dir) { |
||
552 | $filter->excludeDirectory($dir->path(), $dir->suffix(), $dir->prefix()); |
||
553 | } |
||
554 | } |
||
555 | |||
556 | if (null !== $this->codeCoverageConfig && $this->codeCoverageConfig->pathCoverage()) { |
||
557 | $driver = (new \SebastianBergmann\CodeCoverage\Driver\Selector())->forLineAndPathCoverage($filter); |
||
558 | } else { |
||
559 | $driver = (new \SebastianBergmann\CodeCoverage\Driver\Selector())->forLineCoverage($filter); |
||
560 | } |
||
561 | |||
562 | $driver = (new \SebastianBergmann\CodeCoverage\Driver\Selector())->forLineCoverage($filter); |
||
563 | $codeCoverage = new \SebastianBergmann\CodeCoverage\CodeCoverage($driver, $filter); |
||
564 | |||
565 | if (null !== $this->codeCoverageConfig) { |
||
566 | // Set code coverage configuration |
||
567 | if ($this->codeCoverageConfig->hasCacheDirectory()) { |
||
568 | $codeCoverage->cacheStaticAnalysis($this->codeCoverageConfig->cacheDirectory()->path()); |
||
569 | } |
||
570 | if ($this->codeCoverageConfig->ignoreDeprecatedCodeUnits()) { |
||
571 | $codeCoverage->ignoreDeprecatedCode(); |
||
572 | } else { |
||
573 | $codeCoverage->doNotIgnoreDeprecatedCode(); |
||
574 | } |
||
575 | if ($this->codeCoverageConfig->includeUncoveredFiles()) { |
||
576 | $codeCoverage->includeUncoveredFiles(); |
||
577 | } else { |
||
578 | $codeCoverage->doNotProcessUncoveredFiles(); |
||
579 | } |
||
580 | } |
||
581 | |||
582 | $runner->setCodecoverage($codeCoverage); |
||
583 | } |
||
584 | } |
||
585 | } |
||
586 | |||
587 | $runner->setUseCustomErrorHandler($this->usecustomerrorhandler); |
||
588 | |||
589 | foreach ($this->listeners as $listener) { |
||
590 | $runner->addListener($listener); |
||
591 | } |
||
592 | |||
593 | foreach ($this->formatters as $fe) { |
||
594 | $formatter = $fe->getFormatter(); |
||
595 | |||
596 | if ($fe->getUseFile()) { |
||
597 | try { |
||
598 | $destFile = new File($fe->getToDir(), $fe->getOutfile()); |
||
599 | } catch (Exception $e) { |
||
600 | throw new BuildException('Unable to create destination.', $e); |
||
601 | } |
||
602 | |||
603 | $writer = new FileWriter($destFile->getAbsolutePath()); |
||
604 | |||
605 | $formatter->setOutput($writer); |
||
606 | } else { |
||
607 | $formatter->setOutput($this->getDefaultOutput()); |
||
608 | } |
||
609 | |||
610 | $runner->addFormatter($formatter); |
||
611 | |||
612 | $formatter->startTestRun(); |
||
613 | } |
||
614 | |||
615 | $runner->run($suite); |
||
616 | |||
617 | foreach ($this->formatters as $fe) { |
||
618 | $formatter = $fe->getFormatter(); |
||
619 | $formatter->endTestRun(); |
||
620 | } |
||
621 | |||
622 | if ($runner->hasErrors()) { |
||
623 | if ($this->errorproperty) { |
||
624 | $this->project->setNewProperty($this->errorproperty, true); |
||
625 | } |
||
626 | if ($this->haltonerror || $this->haltondefect) { |
||
627 | $this->testfailed = true; |
||
628 | $this->testfailuremessage = $runner->getLastErrorMessage(); |
||
629 | } |
||
630 | } |
||
631 | |||
632 | if ($runner->hasFailures()) { |
||
633 | if ($this->failureproperty) { |
||
634 | $this->project->setNewProperty($this->failureproperty, true); |
||
635 | } |
||
636 | |||
637 | if ($this->haltonfailure || $this->haltondefect) { |
||
638 | $this->testfailed = true; |
||
639 | $this->testfailuremessage = $runner->getLastFailureMessage(); |
||
640 | } |
||
641 | } |
||
642 | |||
643 | if ($runner->hasIncomplete()) { |
||
644 | if ($this->incompleteproperty) { |
||
645 | $this->project->setNewProperty($this->incompleteproperty, true); |
||
646 | } |
||
647 | |||
648 | if ($this->haltonincomplete) { |
||
649 | $this->testfailed = true; |
||
650 | $this->testfailuremessage = $runner->getLastIncompleteMessage(); |
||
651 | } |
||
652 | } |
||
653 | |||
654 | if ($runner->hasSkipped()) { |
||
655 | if ($this->skippedproperty) { |
||
656 | $this->project->setNewProperty($this->skippedproperty, true); |
||
657 | } |
||
658 | |||
659 | if ($this->haltonskipped) { |
||
660 | $this->testfailed = true; |
||
661 | $this->testfailuremessage = $runner->getLastSkippedMessage(); |
||
662 | } |
||
663 | } |
||
664 | |||
665 | if ($runner->hasWarnings()) { |
||
666 | if ($this->warningproperty) { |
||
667 | $this->project->setNewProperty($this->warningproperty, true); |
||
668 | } |
||
669 | |||
670 | if ($this->haltonwarning || $this->haltondefect) { |
||
671 | $this->testfailed = true; |
||
672 | $this->testfailuremessage = $runner->getLastWarningMessage(); |
||
673 | } |
||
674 | } |
||
675 | |||
676 | if ($runner->hasRisky()) { |
||
677 | if ($this->riskyproperty) { |
||
678 | $this->project->setNewProperty($this->riskyproperty, true); |
||
679 | } |
||
680 | |||
681 | if ($this->haltonrisky) { |
||
682 | $this->testfailed = true; |
||
683 | $this->testfailuremessage = $runner->getLastRiskyMessage(); |
||
684 | } |
||
685 | } |
||
686 | } |
||
687 | |||
688 | /** |
||
689 | * Add the tests in this batchtest to a test suite |
||
690 | * |
||
691 | * @param BatchTest $batchTest |
||
692 | * @param TestSuite $suite |
||
693 | * @throws BuildException |
||
694 | * @throws ReflectionException |
||
695 | */ |
||
696 | protected function appendBatchTestToTestSuite(BatchTest $batchTest, $suite) |
||
697 | { |
||
698 | foreach ($batchTest->elements() as $element) { |
||
699 | $testClass = new $element(); |
||
700 | if (!($testClass instanceof TestSuite)) { |
||
701 | $testClass = new ReflectionClass($element); |
||
702 | } |
||
703 | try { |
||
704 | $suite->addTestSuite($testClass); |
||
705 | } catch (\PHPUnit\Framework\Exception $e) { |
||
706 | throw new BuildException('Unable to add TestSuite ' . get_class($testClass), $e); |
||
707 | } |
||
708 | } |
||
709 | } |
||
710 | |||
711 | /** |
||
712 | * @return LogWriter |
||
713 | */ |
||
714 | protected function getDefaultOutput() |
||
717 | } |
||
718 | |||
719 | /** |
||
720 | * Adds a set of tests based on pattern matching. |
||
721 | * |
||
722 | * @return BatchTest a new instance of a batch test. |
||
723 | */ |
||
724 | public function createBatchTest() |
||
731 | } |
||
732 | } |
||
733 |