Completed
Pull Request — master (#335)
by Simon
07:27 queued 03:11
created

CommandTestCaseTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A runCommand() 0 13 1
A readConsoleOutput() 0 10 2
1
<?php
2
3
namespace ShopwarePlugins\Connect\Tests;
4
5
use Shopware\Components\Console\Application;
6
use Symfony\Component\Console\Input\StringInput;
7
use Symfony\Component\Console\Output\StreamOutput;
8
9
trait CommandTestCaseTrait
10
{
11
    use DatabaseTestCaseTrait;
12
13
    /**
14
     * @param string $command
15
     * @return array
16
     */
17
    protected function runCommand($command)
18
    {
19
        $application = new Application(Shopware()->Container()->get('kernel'));
20
        $application->setAutoExit(true);
21
22
        $fp = tmpfile();
23
        $input = new StringInput($command);
24
        $output = new StreamOutput($fp);
25
        $application->doRun($input, $output);
26
27
        $consoleOutput = $this->readConsoleOutput($fp);
28
        return explode(PHP_EOL, $consoleOutput);
29
    }
30
31
    /**
32
     * @param $fp
33
     * @return string
34
     */
35
    private function readConsoleOutput($fp)
36
    {
37
        fseek($fp, 0);
38
        $output = '';
39
        while (!feof($fp)) {
40
            $output = fread($fp, 4096);
41
        }
42
        fclose($fp);
43
        return $output;
44
    }
45
}