Passed
Branch master (baa65a)
by Oliver
09:35
created

ValidateCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 59
dl 0
loc 118
rs 10
c 2
b 0
f 0
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 59 9
A configure() 0 18 1
A writeValidity() 0 15 4
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
            return 0;
64
        }
65
66
        $failedCount = 0;
67
        /** @var TestCase $suite */
68
        foreach ($testCollection as $suite) {
69
            if ($suite instanceof WarningTestCase) {
70
                continue;
71
            }
72
73
            $testClass = get_class($suite);
74
            $testMethod = $suite->getName(false);
75
            $testSignature = $testClass . '::' . $suite->getName();
76
77
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
78
                $this->writeValidity($output, 'Validating ' . $testSignature . '...');
79
            }
80
81
            $isValid = Validator::isValidMethod(
82
                $testClass,
83
                $testMethod
84
            );
85
86
            if (!$isValid) {
87
                $failedCount++;
88
                $this->writeValidity($output, $testSignature, false);
89
            } elseif ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
90
                $this->writeValidity($output, $testSignature, true);
91
            }
92
        }
93
94
        $output->writeln('');
95
96
        if ($failedCount > 0) {
97
            $output->writeln(
98
                "There were {$failedCount} test(s) with invalid @covers tags."
99
            );
100
101
            return 1;
102
        }
103
104
        $output->writeln('Validation complete. All @covers tags are valid.');
105
106
        return 0;
107
    }
108
109
    /**
110
     * Write validity message for tests
111
     * The purpose of this method is to write a new line for the first
112
     * validity related message
113
     *
114
     * @param OutputInterface $output
115
     * @param string $message
116
     * @param bool|null $isValid
117
     */
118
    protected function writeValidity($output, $message, $isValid = null) {
119
        if ($this->firstValidityWrite) {
120
            $output->writeln('');
121
            $this->firstValidityWrite = false;
122
        }
123
124
        if (is_bool($isValid)) {
125
            $message = sprintf(
126
                '%s - %s',
127
                $isValid ? '<fg=green>Valid</>' : '<fg=red>Invalid</>',
128
                $message
129
            );
130
        }
131
132
        $output->writeln($message);
133
    }
134
}
135