|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Code Coverage Controller for Behat |
| 4 | + * |
| 5 | + * @copyright 2018 Danny Lewis |
| 6 | + * @license BSD-2-Clause |
| 7 | + */ |
| 8 | + |
| 9 | +namespace LeanPHP\Behat\CodeCoverage; |
| 10 | + |
| 11 | +use Behat\Testwork\Cli\Controller; |
| 12 | +use LeanPHP\Behat\CodeCoverage\Service\ReportService; |
| 13 | +use SebastianBergmann\CodeCoverage\CodeCoverage; |
| 14 | +use Symfony\Component\Console\Command\Command; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Input\InputOption; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 19 | + |
| 20 | +final class CoverageController implements Controller |
| 21 | +{ |
| 22 | + |
| 23 | + private $container; |
| 24 | + private $coverage; |
| 25 | + private $reportService; |
| 26 | + private $compilerPasses; |
| 27 | + |
| 28 | + public function __construct(ContainerBuilder $container, CodeCoverage $coverage, ReportService $reportService, $compilerPasses) |
| 29 | + { |
| 30 | + $this->container = $container; |
| 31 | + $this->coverage = $coverage; |
| 32 | + $this->reportService = $reportService; |
| 33 | + $this->compilerPasses = $compilerPasses; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + public function configure(Command $command) |
| 40 | + { |
| 41 | + $command->addOption('coverage', null, InputOption::VALUE_NONE, 'Generate coverage report(s)'); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * {@inheritdoc} |
| 46 | + */ |
| 47 | + public function execute(InputInterface $input, OutputInterface $output) |
| 48 | + { |
| 49 | + |
| 50 | + if($input->getOption('coverage')) { |
| 51 | + |
| 52 | + $this->container->get('event_dispatcher')->addSubscriber(new Listener\EventListener($this->coverage, $this->reportService)); |
| 53 | + |
| 54 | + foreach ($this->compilerPasses as $pass) { |
| 55 | + $pass->process($this->container); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + }else{ |
| 60 | + |
| 61 | + $this->container->get('event_dispatcher')->addSubscriber(new Listener\NoCoverage($this->container->get('cli.output'))); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | +} |
0 commit comments