ScenarioChooser   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 75
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A execute() 0 12 3
A shouldAskQuestion() 0 7 3
A getScenarios() 0 23 5
1
<?php
2
3
namespace Bex\Behat\ChooseTestsExtension\Chooser;
4
5
use Behat\Gherkin\Node\FeatureNode;
6
use Behat\Testwork\Specification\SpecificationFinder;
7
use Behat\Testwork\Suite\SuiteRepository;
8
use Bex\Behat\ChooseTestsExtension\Chooser\BaseChooser;
9
use Symfony\Component\Console\Helper\QuestionHelper;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Question\ChoiceQuestion;
13
14
class ScenarioChooser extends BaseChooser
15
{   
16
    /**
17
     * @var SuiteRepository
18
     */
19
    private $suiteRepository;
20
    
21
    /**
22
     * @var SpecificationFinder
23
     */
24
    private $specificationFinder;
25
    
26
    /**
27
     * @param InputInterface      $input
28
     * @param OutputInterface     $output
29
     * @param SuiteRepository     $suiteRepository
30
     * @param SpecificationFinder $specificationFinder
31
     */
32
    public function __construct(
33
        InputInterface $input,
34
        OutputInterface $output,
35
        SuiteRepository $suiteRepository,
36
        SpecificationFinder $specificationFinder
37
    ) {
38
        parent::__construct($input, $output);
39
        
40
        $this->suiteRepository = $suiteRepository;
41
        $this->specificationFinder = $specificationFinder;
42
    }
43
    
44
    public function execute()
45
    {
46
        $scenarios = $this->getScenarios();
47
48
        if ($this->shouldAskQuestion($scenarios)) {
49
            $answer = $this->askQuestion('Choose scenario:', array_merge(['All'], array_keys($scenarios)), 'All');
50
            
51
            if ($answer !== 'All') {
52
                $this->input->setArgument('paths', $scenarios[$answer]);
53
            }
54
        }
55
    }
56
57
    private function shouldAskQuestion(array $scenarios = [])
58
    {
59
        $selectedFeature = $this->input->getArgument('paths');
60
        $numberOfScenarios = count($scenarios);
61
62
        return (empty($selectedFeature) || (strpos($selectedFeature, ':') === false)) && ($numberOfScenarios > 1);
63
    }
64
65
    public function getScenarios()
66
    {
67
        $scenarios = [];
68
69
        $specificationIterators = $this->specificationFinder->findSuitesSpecifications(
70
            $this->suiteRepository->getSuites(),
71
            $this->input->getArgument('paths')
0 ignored issues
show
Bug introduced by
It seems like $this->input->getArgument('paths') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string>; however, Behat\Testwork\Specifica...dSuitesSpecifications() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
72
        );
73
74
        foreach ($specificationIterators as $iterator) {
75
            foreach ($iterator as $specification) {
76
                if ($specification instanceof FeatureNode) {
77
                    foreach ($specification->getScenarios() as $scenario) {
78
                        $value = $specification->getFile() . ':' . $scenario->getLine();
79
                        $key = sprintf('%s (%s)', $scenario->getTitle(), $value);
80
                        $scenarios[$key] = $value;
81
                    }
82
                }
83
            }
84
        }
85
86
        return $scenarios;
87
    }
88
}
89