Passed
Pull Request — master (#6)
by ANTHONIUS
02:39
created

MainContext::resetStream()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the dotfiles project.
7
 *
8
 *     (c) Anthonius Munthi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Dotfiles\Behat\Context;
15
16
use Behat\Behat\Context\Context;
17
use Symfony\Component\Console\Helper\DebugFormatterHelper;
18
use Symfony\Component\Console\Output\StreamOutput;
19
use Symfony\Component\Process\Process;
20
use Webmozart\Assert\Assert;
21
22
class MainContext implements Context
23
{
24
    private $commandPrefix;
25
26
    private $output;
27
28
    private $stream;
29
30
    public function __construct(string $commandPrefix)
31
    {
32
        $this->commandPrefix = $commandPrefix;
33
    }
34
35
    /**
36
     * @BeforeSuite
37
     *
38
     * @throws \Exception when not in docker environment
39
     */
40
    public static function beforeSuite(): void
41
    {
42
        if (!is_file('/.dockerenv')) {
43
            throw new \Exception('You must run behat test in docker-environment');
44
        }
45
    }
46
47
    /**
48
     * @Given I execute add command with :exec argument
49
     * @Given I execute add command with :exec
50
     *
51
     * @param string $path
52
     */
53
    public function iExecuteAddCommand(string $path): void
54
    {
55
        $this->resetStream();
56
        $this->runCommand('add '.$path);
57
    }
58
59
    /**
60
     * @Given I execute restore command
61
     */
62
    public function iExecuteRestoreCommand(): void
63
    {
64
        $this->resetStream();
65
        $this->runCommand('restore');
66
    }
67
68
    /**
69
     * @Then I should see :text
70
     */
71
    public function iShouldSee($text): void
72
    {
73
        rewind($this->output->getStream());
74
        $display = stream_get_contents($this->output->getStream());
75
        //$display = str_replace(PHP_EOL, "\n", $display);
76
        Assert::contains($display, $text);
77
    }
78
79
    private function resetStream(): void
80
    {
81
        if (is_resource($this->stream)) {
82
            fclose($this->stream);
83
        }
84
        $this->stream = fopen('php://memory', 'w');
85
        $this->output = new StreamOutput($this->stream);
0 ignored issues
show
Bug introduced by
It seems like $this->stream can also be of type false; however, parameter $stream of Symfony\Component\Consol...amOutput::__construct() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
        $this->output = new StreamOutput(/** @scrutinizer ignore-type */ $this->stream);
Loading history...
86
    }
87
88
    private function runCommand($command): void
89
    {
90
        $cmd = $this->commandPrefix.' '.$command;
91
        $output = $this->output;
92
        $helper = new DebugFormatterHelper();
93
94
        $process = new Process($cmd, getcwd());
95
        $process->run(function ($type, $buffer) use ($output,$helper,$process): void {
96
            $contents = $helper->start(
97
                spl_object_hash($process),
98
                $buffer,
99
                Process::ERR === $type
100
            );
101
            $output->writeln($contents);
102
        });
103
    }
104
}
105