1 | <?php |
||
14 | class CodeCoverageGenerator extends Command |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | private $baseDir; |
||
20 | |||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | private $exitCode = 0; |
||
25 | |||
26 | protected function configure() |
||
27 | { |
||
28 | $this->baseDir = realpath(__DIR__.'/..').'/'; |
||
29 | |||
30 | $this |
||
31 | ->setName('code-coverage:generate') |
||
32 | ->setDescription('Generates a code coverage report'); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Builds the code coverage clove report file |
||
37 | * |
||
38 | * @param InputInterface $input |
||
39 | * @param OutputInterface $output |
||
40 | * |
||
41 | * @return int|null|void |
||
42 | */ |
||
43 | protected function execute(InputInterface $input, OutputInterface $output) |
||
44 | { |
||
45 | $coverage = new PHP_CodeCoverage(null, $this->getFilter()); |
||
46 | $coverage->start('<tests>'); |
||
47 | |||
48 | $this->runPhpSpec(); |
||
49 | $this->runBehat(); |
||
50 | |||
51 | $coverage->stop(); |
||
52 | $this->writeReport($coverage); |
||
53 | |||
54 | if ($this->exitCode !== 0) { |
||
55 | exit($this->exitCode); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Writes the clover report |
||
61 | * |
||
62 | * @param PHP_CodeCoverage $coverage |
||
63 | */ |
||
64 | private function writeReport(PHP_CodeCoverage $coverage) |
||
65 | { |
||
66 | $writer = new PHP_CodeCoverage_Report_Clover; |
||
67 | $writer->process($coverage, $this->getPath('clover.xml')); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Runs php spec unit tests |
||
72 | */ |
||
73 | private function runPhpSpec() |
||
74 | { |
||
75 | $input = new ArgvInput(['phpspec', 'run', '--format=pretty']); |
||
76 | $app = new Application(null); |
||
77 | $app->setAutoExit(false); |
||
78 | |||
79 | $this->exitCode = $app->run($input, new ConsoleOutput()); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Runs php spec unit tests |
||
84 | */ |
||
85 | private function runBehat() |
||
86 | { |
||
87 | define('BEHAT_BIN_PATH', realpath(__DIR__.'/../bin/behat')); |
||
88 | $input = new ArgvInput(['behat']); |
||
89 | $factory = new ApplicationFactory(); |
||
90 | $app = $factory->createApplication(); |
||
91 | $app->setAutoExit(false); |
||
92 | |||
93 | $this->exitCode += $app->run($input, new ConsoleOutput()); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return PHP_CodeCoverage_Filter |
||
98 | */ |
||
99 | private function getFilter() |
||
100 | { |
||
101 | $filter = new PHP_CodeCoverage_Filter(); |
||
102 | $filter->addDirectoryToBlacklist($this->getPath('console')); |
||
103 | $filter->addDirectoryToBlacklist($this->getPath('features')); |
||
104 | $filter->addDirectoryToBlacklist($this->getPath('spec')); |
||
105 | $filter->addDirectoryToBlacklist($this->getPath('vendor')); |
||
106 | |||
107 | return $filter; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param string $path |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | private function getPath($path) |
||
116 | { |
||
117 | return $this->baseDir.$path; |
||
118 | } |
||
119 | } |