1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SWP\Behat\Contexts; |
6
|
|
|
|
7
|
|
|
use Behat\Behat\Context\Context; |
8
|
|
|
use Behat\Gherkin\Node\TableNode; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\ConsoleEvents; |
12
|
|
|
use Symfony\Component\Console\Event\ConsoleCommandEvent; |
13
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
15
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
16
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
19
|
|
|
|
20
|
|
|
final class CommandContext implements Context |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var KernelInterface |
24
|
|
|
*/ |
25
|
|
|
private $kernel; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Application |
29
|
|
|
*/ |
30
|
|
|
private $application; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Command |
34
|
|
|
*/ |
35
|
|
|
private $command; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var CommandTester |
39
|
|
|
*/ |
40
|
|
|
private $commandTester; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $commandException; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $commandExceptionMessage; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
private $options; |
56
|
|
|
|
57
|
|
|
private $eventDispatcher; |
58
|
|
|
|
59
|
|
|
public function __construct(KernelInterface $kernel, EventDispatcherInterface $eventDispatcher) |
60
|
|
|
{ |
61
|
|
|
$this->kernel = $kernel; |
62
|
|
|
$this->eventDispatcher = $eventDispatcher; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @When I run the :commandName command with options: |
67
|
|
|
*/ |
68
|
|
|
public function iRunTheCommandWithOptions(string $commandName, TableNode $tableNode): void |
69
|
|
|
{ |
70
|
|
|
$this->application = new Application($this->kernel); |
71
|
|
|
$this->application->getDefinition()->addOption(new InputOption('--tenant', '-t', InputOption::VALUE_REQUIRED, 'The tenant code')); |
72
|
|
|
$this->command = $this->application->find($commandName); |
73
|
|
|
$this->setOptions($tableNode); |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
$this->command->mergeApplicationDefinition(); |
77
|
|
|
$input = new ArrayInput($this->options); |
78
|
|
|
$input->bind($this->command->getDefinition()); |
79
|
|
|
$this->eventDispatcher->dispatch(ConsoleEvents::COMMAND, new ConsoleCommandEvent($this->command, $input, new ConsoleOutput())); |
80
|
|
|
|
81
|
|
|
$this->getTester($this->command)->execute($this->options); |
82
|
|
|
} catch (\Exception $exception) { |
83
|
|
|
$path = explode('\\', get_class($exception)); |
84
|
|
|
$this->commandException = array_pop($path); |
85
|
|
|
$this->commandExceptionMessage = $exception->getMessage(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $expectedOutput |
91
|
|
|
* |
92
|
|
|
* @Then /^the command output should be "([^"]*)"$/ |
93
|
|
|
*/ |
94
|
|
|
public function theCommandOutputShouldBe($expectedOutput): void |
95
|
|
|
{ |
96
|
|
|
$current = trim($this->commandTester->getDisplay()); |
97
|
|
|
if (false === strpos($current, $expectedOutput)) { |
98
|
|
|
throw new \LogicException(sprintf('Current output is: [%s]', $current)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $expectedException |
104
|
|
|
* |
105
|
|
|
* @Then /^the command exception should be "([^"]*)"$/ |
106
|
|
|
*/ |
107
|
|
|
public function theCommandExceptionShouldBe(string $expectedException) |
108
|
|
|
{ |
109
|
|
|
if (null === $this->commandException) { |
110
|
|
|
throw new \LogicException('Exception was not registered'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($this->commandException != $expectedException) { |
114
|
|
|
throw new \LogicException(sprintf('Current exception is: [%s]', $this->commandException)); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @Then /^the command exception message should be "([^"]*)"$/ |
120
|
|
|
*/ |
121
|
|
|
public function theCommandExceptionMessageShouldBe(string $result) |
122
|
|
|
{ |
123
|
|
|
if (false === strpos($this->commandExceptionMessage, $result)) { |
124
|
|
|
throw new \Exception(sprintf('Could not see "%s" in exception message "%s"', $result, $this->commandExceptionMessage)); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function setOptions(TableNode $tableNode) |
129
|
|
|
{ |
130
|
|
|
$options = []; |
131
|
|
|
foreach ($tableNode->getRowsHash() as $key => $value) { |
132
|
|
|
$options[$key] = $value; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$this->options = $options; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private function getTester(Command $command) |
139
|
|
|
{ |
140
|
|
|
$this->commandTester = new CommandTester($command); |
141
|
|
|
|
142
|
|
|
return $this->commandTester; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|