FeatureChooser::getFeatures()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 0
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 FeatureChooser 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
        $features = $this->getFeatures();
47
48
        if ($this->shouldAskQuestion($features)) {
49
            $answer = $this->askQuestion('Choose feature:', array_merge(['All'], array_keys($features)), 'All');
50
51
            if ($answer !== 'All') {
52
                $this->input->setArgument('paths', $features[$answer]);
53
            }
54
        }
55
    }
56
57
    private function shouldAskQuestion(array $features = [])
58
    {
59
        $selectedFeature = $this->input->getArgument('paths');
60
        $numberOfFeatures = count($features);
61
62
        return empty($selectedFeature) && ($numberOfFeatures > 1);
63
    }
64
65
    private function getFeatures()
66
    {
67
        $features = [];
68
69
        $specificationIterators = $this->specificationFinder->findSuitesSpecifications($this->suiteRepository->getSuites());
70
71
        foreach ($specificationIterators as $iterator) {
72
            foreach ($iterator as $specification) {
73
                if ($specification instanceof FeatureNode) {
74
                    $value = $specification->getFile();
75
                    $key = sprintf('%s (%s)', $specification->getTitle(), $value);
76
                    $features[$key] = $value;
77
                }
78
            }
79
        }
80
81
        return $features;
82
    }
83
}
84