1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the doyo/behat-coverage-extension project. |
5
|
|
|
* |
6
|
|
|
* (c) Anthonius Munthi <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Doyo\Behat\Coverage\Controller\Cli; |
15
|
|
|
|
16
|
|
|
use Behat\Testwork\Cli\Controller; |
17
|
|
|
use Doyo\Behat\Coverage\Bridge\Symfony\Event; |
18
|
|
|
use Doyo\Behat\Coverage\Event\CoverageEvent; |
19
|
|
|
use Doyo\Behat\Coverage\Event\ReportEvent; |
20
|
|
|
use Symfony\Component\Console\Command\Command; |
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
24
|
|
|
use Symfony\Component\Console\Style\StyleInterface; |
25
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Code Coverage Cli Controller. |
29
|
|
|
* |
30
|
|
|
* @author Anthonius Munthi <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class CoverageController implements Controller, EventSubscriberInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var StyleInterface|null |
36
|
|
|
*/ |
37
|
|
|
private $style; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private $coverageEnabled = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* CoverageController constructor. |
46
|
|
|
* |
47
|
|
|
* @param StyleInterface|null $style |
48
|
|
|
*/ |
49
|
9 |
|
public function __construct(StyleInterface $style) |
50
|
|
|
{ |
51
|
9 |
|
$this->style = $style; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
1 |
|
public function configure(Command $command) |
58
|
|
|
{ |
59
|
1 |
|
$command->addOption('coverage', null, InputOption::VALUE_NONE, 'Collecting code coverage'); |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
public static function getSubscribedEvents() |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
ReportEvent::BEFORE_PROCESS => [ |
66
|
3 |
|
['validateEvent', 1000], |
67
|
|
|
['onBeforeReportProcess', 100], |
68
|
|
|
], |
69
|
3 |
|
ReportEvent::AFTER_PROCESS => 'onAfterReportProcess', |
70
|
3 |
|
CoverageEvent::BEFORE_REFRESH => ['validateEvent', 1000], |
71
|
3 |
|
CoverageEvent::BEFORE_START => ['validateEvent', 1000], |
72
|
3 |
|
CoverageEvent::BEFORE_STOP => ['validateEvent', 1000], |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
1 |
|
public function execute(InputInterface $input, OutputInterface $output) |
80
|
|
|
{ |
81
|
1 |
|
if ($input->hasParameterOption(['--coverage'])) { |
82
|
1 |
|
$this->coverageEnabled = true; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
6 |
|
public function validateEvent(Event $event) |
87
|
|
|
{ |
88
|
6 |
|
if (!$this->coverageEnabled) { |
89
|
1 |
|
$event->stopPropagation(); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function onBeforeReportProcess(ReportEvent $event) |
94
|
|
|
{ |
95
|
1 |
|
$io = $this->style; |
96
|
1 |
|
$io->section('behat coverage reports process started'); |
|
|
|
|
97
|
1 |
|
$event->setIO($io); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
public function onAfterReportProcess(ReportEvent $event) |
101
|
|
|
{ |
102
|
2 |
|
$exceptions = $event->getExceptions(); |
103
|
2 |
|
$io = $event->getIO(); |
104
|
2 |
|
if (0 === \count($exceptions)) { |
105
|
1 |
|
$io->success('behat coverage reports process completed'); |
106
|
|
|
|
107
|
1 |
|
return; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$io->newLine(2); |
111
|
1 |
|
$io->section('behat coverage reports process failed'); |
112
|
1 |
|
foreach ($exceptions as $exception) { |
113
|
1 |
|
$io->error($exception->getMessage()); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.