|
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\OutputInterface; |
|
19
|
|
|
use Symfony\Component\Console\Output\StreamOutput; |
|
20
|
|
|
use Symfony\Component\Process\Process; |
|
21
|
|
|
use Webmozart\Assert\Assert; |
|
22
|
|
|
|
|
23
|
|
|
class CommandContext implements Context |
|
24
|
|
|
{ |
|
25
|
|
|
private $commandPrefix; |
|
26
|
|
|
|
|
27
|
|
|
private $output; |
|
28
|
|
|
|
|
29
|
|
|
private $stream; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(string $commandPrefix) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->commandPrefix = $commandPrefix; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @BeforeSuite |
|
38
|
|
|
* |
|
39
|
|
|
* @throws \Exception when not in docker environment |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function beforeSuite(): void |
|
42
|
|
|
{ |
|
43
|
|
|
if (!is_file('/.dockerenv')) { |
|
44
|
|
|
throw new \Exception('You must run behat test in docker-environment'); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @Given I execute add command with :exec argument |
|
50
|
|
|
* @Given I execute add command with :exec |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $path |
|
53
|
|
|
*/ |
|
54
|
|
|
public function iExecuteAddCommand(string $path): void |
|
55
|
|
|
{ |
|
56
|
|
|
$this->resetStream(); |
|
57
|
|
|
$this->runCommand('add '.$path); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @Given I execute restore command |
|
62
|
|
|
*/ |
|
63
|
|
|
public function iExecuteRestoreCommand(): void |
|
64
|
|
|
{ |
|
65
|
|
|
$this->resetStream(); |
|
66
|
|
|
$this->runCommand('restore'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @Then I should see :text |
|
71
|
|
|
*/ |
|
72
|
|
|
public function iShouldSee($text): void |
|
73
|
|
|
{ |
|
74
|
|
|
rewind($this->output->getStream()); |
|
75
|
|
|
$display = stream_get_contents($this->output->getStream()); |
|
76
|
|
|
//$display = str_replace(PHP_EOL, "\n", $display); |
|
77
|
|
|
Assert::contains($display, $text); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function resetStream(): void |
|
81
|
|
|
{ |
|
82
|
|
|
if (is_resource($this->stream)) { |
|
83
|
|
|
fclose($this->stream); |
|
84
|
|
|
} |
|
85
|
|
|
$this->stream = fopen('php://memory', 'w'); |
|
86
|
|
|
$this->output = new StreamOutput($this->stream); |
|
|
|
|
|
|
87
|
|
|
$this->output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function runCommand($command): void |
|
91
|
|
|
{ |
|
92
|
|
|
$cmd = $this->commandPrefix.' -vvv '.$command; |
|
93
|
|
|
$output = $this->output; |
|
94
|
|
|
$helper = new DebugFormatterHelper(); |
|
95
|
|
|
|
|
96
|
|
|
$process = new Process($cmd, getcwd()); |
|
97
|
|
|
$process->run(function ($type, $buffer) use ($output,$helper,$process): void { |
|
98
|
|
|
$contents = $helper->start( |
|
99
|
|
|
spl_object_hash($process), |
|
100
|
|
|
$buffer, |
|
101
|
|
|
Process::ERR === $type |
|
102
|
|
|
); |
|
103
|
|
|
$output->writeln($contents); |
|
104
|
|
|
}); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|