1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OckCyp\CoversValidator\Command; |
4
|
|
|
|
5
|
|
|
use OckCyp\CoversValidator\Handler\InputHandler; |
6
|
|
|
use OckCyp\CoversValidator\Loader\TestSuiteLoader; |
7
|
|
|
use OckCyp\CoversValidator\Validator\Validator; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use PHPUnit\Framework\WarningTestCase; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
class ValidateCommand extends Command |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
protected $firstValidityWrite = true; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
protected function configure() |
26
|
|
|
{ |
27
|
|
|
$this->setName('validate') |
28
|
|
|
->addOption( |
29
|
|
|
'configuration', |
30
|
|
|
'c', |
31
|
|
|
InputOption::VALUE_REQUIRED, |
32
|
|
|
'Read PHPUnit configuration from XML file.' |
33
|
|
|
) |
34
|
|
|
->addUsage('-c app') |
35
|
|
|
->addUsage('-c tests/configuration.xml') |
36
|
|
|
->addOption( |
37
|
|
|
'bootstrap', |
38
|
|
|
null, |
39
|
|
|
InputOption::VALUE_REQUIRED, |
40
|
|
|
'A "bootstrap" PHP file that is run before the validation.' |
41
|
|
|
) |
42
|
|
|
->addUsage('--bootstrap=tests/bootstrap.php'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
49
|
|
|
{ |
50
|
|
|
$output->writeln($this->getApplication()->getLongVersion()); |
51
|
|
|
|
52
|
|
|
$configurationHolder = InputHandler::handleInput($input); |
53
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) { |
54
|
|
|
$output->writeln(PHP_EOL.sprintf( |
55
|
|
|
'Configuration file loaded: %s', |
56
|
|
|
$configurationHolder->getFilename() |
57
|
|
|
)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$testCollection = TestSuiteLoader::loadSuite($configurationHolder); |
61
|
|
|
if ($testCollection->isEmpty()) { |
62
|
|
|
$output->writeln(PHP_EOL.'No tests found to validate.'); |
63
|
|
|
|
64
|
|
|
return 0; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$failedCount = 0; |
68
|
|
|
/** @var TestCase $suite */ |
69
|
|
|
foreach ($testCollection as $suite) { |
70
|
|
|
if ($suite instanceof WarningTestCase) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$testClass = get_class($suite); |
75
|
|
|
$testMethod = $suite->getName(false); |
76
|
|
|
$testSignature = $testClass.'::'.$suite->getName(); |
77
|
|
|
|
78
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) { |
79
|
|
|
$this->writeValidity($output, 'Validating '.$testSignature.'...'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$isValid = Validator::isValidMethod( |
83
|
|
|
$testClass, |
84
|
|
|
$testMethod |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
if (!$isValid) { |
88
|
|
|
++$failedCount; |
89
|
|
|
$this->writeValidity($output, $testSignature, false); |
90
|
|
|
} elseif ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
91
|
|
|
$this->writeValidity($output, $testSignature, true); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$output->writeln(''); |
96
|
|
|
|
97
|
|
|
if ($failedCount > 0) { |
98
|
|
|
$output->writeln( |
99
|
|
|
"There were {$failedCount} test(s) with invalid @covers tags." |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
return 1; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$output->writeln('Validation complete. All @covers tags are valid.'); |
106
|
|
|
|
107
|
|
|
return 0; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Write validity message for tests |
112
|
|
|
* The purpose of this method is to write a new line for the first |
113
|
|
|
* validity related message |
114
|
|
|
* |
115
|
|
|
* @param OutputInterface $output |
116
|
|
|
* @param string $message |
117
|
|
|
* @param bool|null $isValid |
118
|
|
|
*/ |
119
|
|
|
protected function writeValidity($output, $message, $isValid = null) |
120
|
|
|
{ |
121
|
|
|
if ($this->firstValidityWrite) { |
122
|
|
|
$output->writeln(''); |
123
|
|
|
$this->firstValidityWrite = false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (is_bool($isValid)) { |
127
|
|
|
$message = sprintf( |
128
|
|
|
'%s - %s', |
129
|
|
|
$isValid ? '<fg=green>Valid</>' : '<fg=red>Invalid</>', |
130
|
|
|
$message |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$output->writeln($message); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|