-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPHPUnitTask.php
More file actions
732 lines (637 loc) · 20.6 KB
/
Copy pathPHPUnitTask.php
File metadata and controls
732 lines (637 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
namespace Phing\Task\Ext\PhpUnit;
use Exception;
use Phing\Exception\BuildException;
use Phing\Io\FileWriter;
use Phing\Io\LogWriter;
use Phing\Io\File;
use Phing\Task;
use PHPUnit\Framework\TestSuite;
use PHPUnit\TextUI\XmlConfiguration\Loader;
use ReflectionException;
use ReflectionClass;
/**
* Runs PHPUnit tests.
*
* @author Michiel Rook <mrook@php.net>
* @package phing.tasks.ext.phpunit
* @see BatchTest
* @since 2.1.0
*/
class PHPUnitTask extends Task
{
private $batchtests = [];
/**
* @var FormatterElement[] $formatters
*/
private $formatters = [];
private $bootstrap = "";
private $haltonerror = false;
private $haltonfailure = false;
private $haltonincomplete = false;
private $haltonskipped = false;
private $haltondefect = false;
private $haltonwarning = false;
private $haltonrisky = false;
private $errorproperty;
private $failureproperty;
private $incompleteproperty;
private $skippedproperty;
private $warningproperty;
private $riskyproperty;
private $printsummary = false;
private $testfailed = false;
private $testfailuremessage = "";
private $codecoverage = null;
private $groups = [];
private $excludeGroups = [];
private $processIsolation = false;
private $usecustomerrorhandler = true;
private $listeners = [];
/**
* @var string
*/
private $pharLocation = "";
/**
* @var File
*/
private $configuration = null;
/**
* @var \PHPUnit\TextUI\XmlConfiguration\CodeCoverage\CodeCoverage
*/
private $codeCoverageConfig = null;
/**
* Initialize Task.
* This method includes any necessary PHPUnit libraries and triggers
* appropriate error if they cannot be found. This is not done in header
* because we may want this class to be loaded w/o triggering an error.
*/
public function init()
{
}
private function loadPHPUnit()
{
if (!empty($this->pharLocation)) {
// nasty but necessary: reorder the autoloaders so the one in the PHAR gets priority
$autoloadFunctions = spl_autoload_functions();
$composerAutoloader = null;
if (get_class($autoloadFunctions[0][0]) === 'Composer\Autoload\ClassLoader') {
$composerAutoloader = $autoloadFunctions[0];
spl_autoload_unregister($composerAutoloader);
}
$GLOBALS['_SERVER']['SCRIPT_NAME'] = '-';
ob_start();
@include $this->pharLocation;
ob_end_clean();
if ($composerAutoloader !== null) {
spl_autoload_register($composerAutoloader);
}
}
if (!class_exists('PHPUnit\Runner\Version')) {
throw new BuildException("PHPUnitTask requires PHPUnit to be installed", $this->getLocation());
}
if (
class_exists('\PHPUnit\Runner\Version', false) &&
version_compare(\PHPUnit\Runner\Version::id(), '9.0.0', '<')
) {
throw new BuildException("Phing only supports PHPUnit 9+");
}
}
/**
* Sets the name of a bootstrap file that is run before
* executing the tests
*
* @param string $bootstrap the name of the bootstrap file
*/
public function setBootstrap($bootstrap)
{
$this->bootstrap = $bootstrap;
}
/**
* @param $value
*/
public function setErrorproperty($value)
{
$this->errorproperty = $value;
}
/**
* @param $value
*/
public function setFailureproperty($value)
{
$this->failureproperty = $value;
}
/**
* @param $value
*/
public function setIncompleteproperty($value)
{
$this->incompleteproperty = $value;
}
/**
* @param $value
*/
public function setSkippedproperty($value)
{
$this->skippedproperty = $value;
}
/**
* @param $value
*/
public function setRiskyproperty($value)
{
$this->riskyproperty = $value;
}
/**
* @param $value
*/
public function setWarningproperty($value)
{
$this->riskyproperty = $value;
}
/**
* @return bool
*/
public function getHaltondefect(): bool
{
return $this->haltondefect;
}
/**
* @param bool $haltondefect
*/
public function setHaltondefect(bool $haltondefect): void
{
$this->haltondefect = $haltondefect;
}
/**
* @return bool
*/
public function getHaltonwarning(): bool
{
return $this->haltonwarning;
}
/**
* @param bool $haltonwarning
*/
public function setHaltonwarning(bool $haltonwarning): void
{
$this->haltonwarning = $haltonwarning;
}
/**
* @return bool
*/
public function getHaltonrisky(): bool
{
return $this->haltonrisky;
}
/**
* @param bool $haltonrisky
*/
public function setHaltonrisky(bool $haltonrisky): void
{
$this->haltonrisky = $haltonrisky;
}
/**
* @param $value
*/
public function setHaltonerror($value)
{
$this->haltonerror = $value;
}
/**
* @param $value
*/
public function setHaltonfailure($value)
{
$this->haltonfailure = $value;
}
/**
* @return bool
*/
public function getHaltonfailure()
{
return $this->haltonfailure;
}
/**
* @param $value
*/
public function setHaltonincomplete($value)
{
$this->haltonincomplete = $value;
}
/**
* @return bool
*/
public function getHaltonincomplete()
{
return $this->haltonincomplete;
}
/**
* @param $value
*/
public function setHaltonskipped($value)
{
$this->haltonskipped = $value;
}
/**
* @return bool
*/
public function getHaltonskipped()
{
return $this->haltonskipped;
}
/**
* @param $printsummary
*/
public function setPrintsummary($printsummary)
{
$this->printsummary = $printsummary;
}
/**
* @param $codecoverage
*/
public function setCodecoverage($codecoverage)
{
$this->codecoverage = $codecoverage;
}
/**
* @param $processIsolation
*/
public function setProcessIsolation($processIsolation)
{
$this->processIsolation = $processIsolation;
}
/**
* @param $usecustomerrorhandler
*/
public function setUseCustomErrorHandler($usecustomerrorhandler)
{
$this->usecustomerrorhandler = $usecustomerrorhandler;
}
/**
* @param $groups
*/
public function setGroups($groups)
{
$token = ' ,;';
$this->groups = [];
$tok = strtok($groups, $token);
while ($tok !== false) {
$this->groups[] = $tok;
$tok = strtok($token);
}
}
/**
* @param $excludeGroups
*/
public function setExcludeGroups($excludeGroups)
{
$token = ' ,;';
$this->excludeGroups = [];
$tok = strtok($excludeGroups, $token);
while ($tok !== false) {
$this->excludeGroups[] = $tok;
$tok = strtok($token);
}
}
/**
* Add a new formatter to all tests of this task.
*
* @param FormatterElement $fe formatter element
*/
public function addFormatter(FormatterElement $fe)
{
$fe->setParent($this);
$this->formatters[] = $fe;
}
/**
* Add a new listener to all tests of this taks
*
* @param $listener
*/
private function addListener($listener)
{
$this->listeners[] = $listener;
}
/**
* @param File $configuration
*/
public function setConfiguration(File $configuration)
{
$this->configuration = $configuration;
}
/**
* @param string $pharLocation
*/
public function setPharLocation($pharLocation)
{
$this->pharLocation = $pharLocation;
}
/**
* Load and processes the PHPUnit configuration
*
* @param $configuration
* @return mixed
* @throws ReflectionException
* @throws BuildException
*/
protected function handlePHPUnitConfiguration(File $configuration)
{
if (!$configuration->exists()) {
throw new BuildException("Unable to find PHPUnit configuration file '" . (string) $configuration . "'");
}
$config = (new Loader())->load($configuration->getAbsolutePath());
$phpunit = $config->phpunit();
if ($phpunit->hasBootstrap()) {
$this->setBootstrap($phpunit->bootstrap());
}
$this->setHaltonfailure($phpunit->stopOnFailure());
$this->setHaltonerror($phpunit->stopOnError());
$this->setHaltonskipped($phpunit->stopOnSkipped());
$this->setHaltonincomplete($phpunit->stopOnIncomplete());
$this->setHaltondefect($phpunit->stopOnDefect());
$this->setHaltonwarning($phpunit->stopOnWarning());
$this->setHaltonrisky($phpunit->stopOnRisky());
$this->setProcessIsolation($phpunit->processIsolation());
foreach ($config->listeners() as $listener) {
if (
!class_exists($listener->className(), false)
&& $listener->hasSourceFile()
) {
include_once $listener->sourceFile();
}
if (class_exists($listener->className())) {
if ($listener->hasArguments()) {
$listener = (new $listener->className())();
} else {
$listenerClass = new ReflectionClass(
$listener->className()
);
$listener = $listenerClass->newInstanceArgs(
$listener->arguments()
);
}
if ($listener instanceof \PHPUnit\Framework\TestListener) {
$this->addListener($listener);
}
}
}
$this->codeCoverageConfig = $config->codeCoverage();
return $phpunit;
}
/**
* The main entry point
*
* @throws BuildException
*/
public function main()
{
if ($this->codecoverage && !extension_loaded('xdebug')) {
throw new BuildException("PHPUnitTask depends on Xdebug being installed to gather code coverage information.");
}
$this->loadPHPUnit();
$suite = new \PHPUnit\Framework\TestSuite('AllTests');
$autoloadSave = spl_autoload_functions();
if ($this->bootstrap) {
include $this->bootstrap;
}
if ($this->configuration) {
$phpunit = $this->handlePHPUnitConfiguration($this->configuration);
if ($phpunit->backupGlobals() === false) {
$suite->setBackupGlobals(false);
}
if ($phpunit->backupStaticAttributes() === true) {
$suite->setBackupStaticAttributes(true);
}
}
if ($this->printsummary) {
$fe = new FormatterElement();
$fe->setParent($this);
$fe->setType("summary");
$fe->setUseFile(false);
$this->formatters[] = $fe;
}
foreach ($this->batchtests as $batchTest) {
$this->appendBatchTestToTestSuite($batchTest, $suite);
}
$this->execute($suite);
if ($this->testfailed) {
throw new BuildException("Test(s) failed: " . $this->testfailuremessage);
}
$autoloadNew = spl_autoload_functions();
if (is_array($autoloadNew)) {
foreach ($autoloadNew as $autoload) {
spl_autoload_unregister($autoload);
}
}
if (is_array($autoloadSave)) {
foreach ($autoloadSave as $autoload) {
spl_autoload_register($autoload);
}
}
}
/**
* @param $suite
* @throws BuildException
* @throws ReflectionException
*/
protected function execute($suite)
{
$runner = new PHPUnitTestRunner9(
$this->project,
$this->groups,
$this->excludeGroups,
$this->processIsolation
);
if ($this->codecoverage) {
/**
* Add some defaults to the PHPUnit filter
*/
$pwd = __DIR__;
$path = realpath($pwd . '/../../../');
if (class_exists('\SebastianBergmann\CodeCoverage\Filter')) {
$filter = new \SebastianBergmann\CodeCoverage\Filter();
if (method_exists($filter, 'addDirectoryToBlacklist')) {
$filter->addDirectoryToBlacklist($path);
}
if (class_exists('\SebastianBergmann\CodeCoverage\CodeCoverage')) {
if (null !== $this->codeCoverageConfig) {
// Update filters
foreach ($this->codeCoverageConfig->files()->asArray() as $file) {
$filter->includeFile($file->path());
}
foreach ($this->codeCoverageConfig->directories()->asArray() as $dir) {
$filter->includeDirectory($dir->path(), $dir->suffix(), $dir->prefix());
}
foreach ($this->codeCoverageConfig->excludeFiles()->asArray() as $file) {
$filter->excludeFile($file->path());
}
foreach ($this->codeCoverageConfig->excludeDirectories()->asArray() as $dir) {
$filter->excludeDirectory($dir->path(), $dir->suffix(), $dir->prefix());
}
}
if (null !== $this->codeCoverageConfig && $this->codeCoverageConfig->pathCoverage()) {
$driver = (new \SebastianBergmann\CodeCoverage\Driver\Selector())->forLineAndPathCoverage($filter);
} else {
$driver = (new \SebastianBergmann\CodeCoverage\Driver\Selector())->forLineCoverage($filter);
}
$driver = (new \SebastianBergmann\CodeCoverage\Driver\Selector())->forLineCoverage($filter);
$codeCoverage = new \SebastianBergmann\CodeCoverage\CodeCoverage($driver, $filter);
if (null !== $this->codeCoverageConfig) {
// Set code coverage configuration
if ($this->codeCoverageConfig->hasCacheDirectory()) {
$codeCoverage->cacheStaticAnalysis($this->codeCoverageConfig->cacheDirectory()->path());
}
if ($this->codeCoverageConfig->ignoreDeprecatedCodeUnits()) {
$codeCoverage->ignoreDeprecatedCode();
} else {
$codeCoverage->doNotIgnoreDeprecatedCode();
}
if ($this->codeCoverageConfig->includeUncoveredFiles()) {
$codeCoverage->includeUncoveredFiles();
} else {
$codeCoverage->doNotProcessUncoveredFiles();
}
}
$runner->setCodecoverage($codeCoverage);
}
}
}
$runner->setUseCustomErrorHandler($this->usecustomerrorhandler);
foreach ($this->listeners as $listener) {
$runner->addListener($listener);
}
foreach ($this->formatters as $fe) {
$formatter = $fe->getFormatter();
if ($fe->getUseFile()) {
try {
$destFile = new File($fe->getToDir(), $fe->getOutfile());
} catch (Exception $e) {
throw new BuildException('Unable to create destination.', $e);
}
$writer = new FileWriter($destFile->getAbsolutePath());
$formatter->setOutput($writer);
} else {
$formatter->setOutput($this->getDefaultOutput());
}
$runner->addFormatter($formatter);
$formatter->startTestRun();
}
$runner->run($suite);
foreach ($this->formatters as $fe) {
$formatter = $fe->getFormatter();
$formatter->endTestRun();
}
if ($runner->hasErrors()) {
if ($this->errorproperty) {
$this->project->setNewProperty($this->errorproperty, true);
}
if ($this->haltonerror || $this->haltondefect) {
$this->testfailed = true;
$this->testfailuremessage = $runner->getLastErrorMessage();
}
}
if ($runner->hasFailures()) {
if ($this->failureproperty) {
$this->project->setNewProperty($this->failureproperty, true);
}
if ($this->haltonfailure || $this->haltondefect) {
$this->testfailed = true;
$this->testfailuremessage = $runner->getLastFailureMessage();
}
}
if ($runner->hasIncomplete()) {
if ($this->incompleteproperty) {
$this->project->setNewProperty($this->incompleteproperty, true);
}
if ($this->haltonincomplete) {
$this->testfailed = true;
$this->testfailuremessage = $runner->getLastIncompleteMessage();
}
}
if ($runner->hasSkipped()) {
if ($this->skippedproperty) {
$this->project->setNewProperty($this->skippedproperty, true);
}
if ($this->haltonskipped) {
$this->testfailed = true;
$this->testfailuremessage = $runner->getLastSkippedMessage();
}
}
if ($runner->hasWarnings()) {
if ($this->warningproperty) {
$this->project->setNewProperty($this->warningproperty, true);
}
if ($this->haltonwarning || $this->haltondefect) {
$this->testfailed = true;
$this->testfailuremessage = $runner->getLastWarningMessage();
}
}
if ($runner->hasRisky()) {
if ($this->riskyproperty) {
$this->project->setNewProperty($this->riskyproperty, true);
}
if ($this->haltonrisky) {
$this->testfailed = true;
$this->testfailuremessage = $runner->getLastRiskyMessage();
}
}
}
/**
* Add the tests in this batchtest to a test suite
*
* @param BatchTest $batchTest
* @param TestSuite $suite
* @throws BuildException
* @throws ReflectionException
*/
protected function appendBatchTestToTestSuite(BatchTest $batchTest, $suite)
{
foreach ($batchTest->elements() as $element) {
$testClass = new $element();
if (!($testClass instanceof TestSuite)) {
$testClass = new ReflectionClass($element);
}
try {
$suite->addTestSuite($testClass);
} catch (\PHPUnit\Framework\Exception $e) {
throw new BuildException('Unable to add TestSuite ' . get_class($testClass), $e);
}
}
}
/**
* @return LogWriter
*/
protected function getDefaultOutput()
{
return new LogWriter($this);
}
/**
* Adds a set of tests based on pattern matching.
*
* @return BatchTest a new instance of a batch test.
*/
public function createBatchTest()
{
$batchtest = new BatchTest($this->getProject());
$this->batchtests[] = $batchtest;
return $batchtest;
}
}