Command::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
namespace Peridot\Console;
3
4
use Evenement\EventEmitterInterface;
5
use Peridot\Configuration;
6
use Peridot\Core\HasEventEmitterTrait;
7
use Peridot\Core\TestResult;
8
use Peridot\Reporter\ReporterFactory;
9
use Peridot\Runner\RunnerInterface;
10
use Peridot\Runner\SuiteLoader;
11
use Peridot\Runner\SuiteLoaderInterface;
12
use Symfony\Component\Console\Command\Command as ConsoleCommand;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * The default Peridot CLI command. Responsible for loading and
18
 * executing tests.
19
 *
20
 * @package Peridot\Console
21
 */
22
class Command extends ConsoleCommand
23
{
24
    use HasEventEmitterTrait;
25
26
    /**
27
     * @var \Peridot\Runner\RunnerInterface
28
     */
29
    protected $runner;
30
31
    /**
32
     * @var \Peridot\Configuration
33
     */
34
    protected $configuration;
35
36
    /**
37
     * @var \Peridot\Reporter\ReporterFactory
38
     */
39
    protected $factory;
40
41
    /**
42
     * @var \Peridot\Runner\SuiteLoaderInterface
43
     */
44
    protected $loader;
45
46
    /**
47
     * @param RunnerInterface $runner
48
     * @param Configuration $configuration
49
     * @param ReporterFactory $factory
50
     * @param EventEmitterInterface $eventEmitter
51
     */
52
    public function __construct(
53
        RunnerInterface $runner,
54
        Configuration $configuration,
55
        ReporterFactory $factory,
56
        EventEmitterInterface $eventEmitter
57
    ) {
58
        parent::__construct('peridot');
59
        $this->runner = $runner;
60
        $this->configuration = $configuration;
61
        $this->factory = $factory;
62
        $this->eventEmitter = $eventEmitter;
63
    }
64
65
    /**
66
     * Set the loader used by the Peridot command
67
     *
68
     * @param SuiteLoaderInterface $loader
69
     * @return $this
70
     */
71
    public function setLoader(SuiteLoaderInterface $loader)
72
    {
73
        $this->loader = $loader;
74
        return $this;
75
    }
76
77
    /**
78
     * Fetch the loader used by the Peridot command. Defaults to
79
     * a glob based loader
80
     *
81
     * @return SuiteLoaderInterface
82
     */
83
    public function getLoader()
84
    {
85
        if ($this->loader === null) {
86
            return new SuiteLoader($this->configuration->getGrep());
87
        }
88
        return $this->loader;
89
    }
90
91
    /**
92
     * Set the suite runner used by the Peridot command.
93
     *
94
     * @param RunnerInterface $runner
95
     * @return $this
96
     */
97
    public function setRunner(RunnerInterface $runner)
98
    {
99
        $this->runner = $runner;
100
        return $this;
101
    }
102
103
    /**
104
     * Return the runner used by the Peridot command. Defaults to
105
     * an instance of Peridot\Runner\Runner.
106
     *
107
     * @return RunnerInterface
108
     */
109
    public function getRunner()
110
    {
111
        return $this->runner;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     *
117
     * @return string
118
     */
119
    public function getSynopsis($short = false)
120
    {
121
        return $this->getName() . ' [options] [files]';
122
    }
123
124
    /**
125
     * Load and run Suites and Tests
126
     *
127
     * @param  InputInterface  $input
128
     * @param  OutputInterface $output
129
     * @return int
130
     */
131
    protected function execute(InputInterface $input, OutputInterface $output)
132
    {
133
        $this->eventEmitter->emit('peridot.execute', [$input, $output]);
134
        $this->eventEmitter->emit('peridot.reporters', [$input, $this->factory]);
135
136
        if ($input->getOption('reporters')) {
137
            $this->listReporters($output);
138
139
            return 0;
140
        }
141
142
        $this->configuration->setReporters($input->getOption('reporter'));
143
        $this->eventEmitter->emit('peridot.load', [$this, $this->configuration]);
144
145
        return $this->getResult();
146
    }
147
148
    /**
149
     * Output available reporters
150
     *
151
     * @param OutputInterface $output
152
     */
153
    protected function listReporters(OutputInterface $output)
154
    {
155
        $output->writeln("");
156
        foreach ($this->factory->getReporters() as $name => $info) {
157
            $output->writeln(sprintf("    %s - %s", $name, $info['description']));
158
        }
159
        $output->writeln("");
160
    }
161
162
    /**
163
     * Return the result as an integer.
164
     *
165
     * @return int
166
     */
167
    protected function getResult()
168
    {
169
        $result = new TestResult($this->eventEmitter);
170
        $this->getLoader()->load($this->configuration->getPath());
171
        $this->factory->createComposite($this->configuration->getReporters());
172
        $this->runner->run($result);
173
174
        if ($result->getFailureCount() > 0) {
175
            return 1;
176
        }
177
178
        if ($result->isFocusedByDsl()) {
179
            return 2;
180
        }
181
182
        return 0;
183
    }
184
}
185