Completed
Push — symfony5 ( 9d1480...20e047 )
by
unknown
82:15 queued 68:26
created

ConsoleContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
namespace eZ\Bundle\EzPublishCoreBundle\Features\Context;
3
4
use Behat\Behat\Context\Context;
5
use Behat\Symfony2Extension\Context\KernelDictionary;
6
use eZ\Publish\Core\MVC\ConfigResolverInterface;
7
use Symfony\Component\Process\PhpExecutableFinder;
8
use Symfony\Component\Process\Process;
9
use PHPUnit\Framework\Assert as Assertion;
10
11
class ConsoleContext implements Context
12
{
13
    /** @var ConfigResolverInterface */
14
    private $configResolver;
15
16
    /** @var string[] */
17
    private $siteaccessList;
18
19
    /** @var string */
20
    private $defaultSiteaccess;
21
22
    private $scriptOutput = null;
23
24
    /**
25
     * Elements referenced by 'it' in sentences.
26
     * @var array
27
     */
28
    private $it = [];
29
30
    /**
31
     * @param ConfigResolverInterface $configResolver
32
     * @param string[] $siteaccessList
33
     * @param string $defaultSiteaccess
34
     */
35
    public function __construct(
36
        ConfigResolverInterface $configResolver,
37
        array $siteaccessList,
38
        string $defaultSiteaccess
39
    ) {
40
        $this->configResolver = $configResolver;
41
        $this->siteaccessList = $siteaccessList;
42
        $this->defaultSiteaccess = $defaultSiteaccess;
43
    }
44
45
    /**
46
     * @When I run a console script without specifying a siteaccess
47
     */
48
    public function iRunAConsoleScript()
49
    {
50
        $this->iRunTheCommand('ez:behat:siteaccess');
51
    }
52
53
    /**
54
     * @When I run a console script with the siteaccess option :siteaccessOption
55
     */
56
    public function iRunAConsoleScriptWithSiteaccess($siteaccessOption)
57
    {
58
        $this->iRunTheCommand('ez:behat:siteaccess', $siteaccessOption);
59
    }
60
61
    /**
62
     * @Then It is executed with the siteaccess :siteaccess
63
     */
64
    public function iExpectItToBeExecutedWithTheSiteaccess($siteaccess)
65
    {
66
        $actualSiteaccess = trim($this->scriptOutput);
67
        Assertion::assertEquals(
68
            $siteaccess,
69
            $actualSiteaccess,
70
            "The command was expected to be executed with the siteaccess \"$siteaccess\", but was executed with \"$actualSiteaccess\""
71
        );
72
    }
73
74
    /**
75
     * @Then it is executed with the default one
76
     *
77
     * default one: default siteaccess.
78
     */
79
    public function iExpectItToBeExecutedWithTheDefaultOne()
80
    {
81
        $this->iExpectItToBeExecutedWithTheSiteaccess($this->getDefaultSiteaccessName());
82
    }
83
84
    /**
85
     * @Given /^that there is a "([^"]*)" siteaccess$/
86
     */
87
    public function thereIsASiteaccess($expectedSiteaccessName, $default = false)
88
    {
89
        $found = false;
90
91
        $siteaccessList = $this->getConfigResolver()->getParameter('siteaccess.list');
92
        foreach ($siteaccessList as $siteaccessName) {
93
            if ($siteaccessName === $expectedSiteaccessName) {
94
                $found = $default === false || $siteaccessName !== $this->getDefaultSiteaccessName();
95
            }
96
        }
97
98
        Assertion::assertTrue($found, "No siteaccess named $expectedSiteaccessName was found");
99
        $this->it['siteaccess'] = $expectedSiteaccessName;
100
    }
101
102
    /**
103
     * @Given /^that there is a default "([^"]*)" siteaccess$/
104
     */
105
    public function thereIsADefaultSiteaccess($expectedSiteaccessName)
106
    {
107
        $this->thereIsASiteaccess($expectedSiteaccessName, true);
108
        Assertion::assertEquals(
109
            $expectedSiteaccessName,
110
            $siteaccessList = $this->getConfigResolver()->getParameter('siteaccess.default_siteaccess')
111
        );
112
    }
113
114
    /**
115
     * @When I run a console script with it
116
     *
117
     * it: the siteaccess referenced above.
118
     */
119
    public function iRunAConsoleScriptWithIt()
120
    {
121
        $this->iRunTheCommand(
122
            'ez:behat:siteaccess',
123
            $this->it['siteaccess']
124
        );
125
        $this->it['siteaccess'] = $this->scriptOutput;
126
    }
127
128
    private function iRunTheCommand($command, $siteaccess = null)
129
    {
130
        $phpFinder = new PhpExecutableFinder();
131
        if (!$phpPath = $phpFinder->find(false)) {
132
            throw new \RuntimeException('The php executable could not be found. Add it to your PATH environment variable and try again');
133
        }
134
        $arguments = $phpFinder->findArguments();
135
        if (false !== ($ini = php_ini_loaded_file())) {
136
            $arguments[] = '--php-ini=' . $ini;
137
        }
138
        $php = escapeshellarg($phpPath);
139
        $phpArgs = implode(' ', array_map('escapeshellarg', $arguments));
140
        $console = escapeshellarg('bin/console');
141
        $cmd = escapeshellarg($command);
142
143
        $console .= ' --env=' . escapeshellarg('behat');
144
        if ($siteaccess !== null) {
145
            $console .= ' --siteaccess=' . escapeshellarg($siteaccess);
146
        }
147
148
        $commandLine = $php . ($phpArgs ? ' ' . $phpArgs : '') . ' ' . $console . ' ' . $cmd;
149
        $process = Process::fromShellCommandline($commandLine);
150
        $process->run();
151
        if (!$process->isSuccessful()) {
152
            throw new \RuntimeException(sprintf('An error occurred when executing the "%s" command. %s', escapeshellarg($cmd), $process->getErrorOutput()));
153
        }
154
155
        $this->scriptOutput = $process->getOutput();
156
    }
157
158
    /**
159
     * @Given /^that there is a siteaccess that is not the default one$/
160
     */
161
    public function thereIsASiteaccessThatIsNotTheDefaultOne()
162
    {
163
        $siteaccessName = $this->getNonDefaultSiteaccessName();
164
        Assertion::assertNotNull($siteaccessName, 'There is no siteaccess other than the default one');
165
        $this->it['siteaccess'] = $siteaccessName;
166
    }
167
168
    /**
169
     * @Then /^I expect it to be executed with it$/
170
     */
171
    public function iExpectItToBeExecutedWithIt()
172
    {
173
        Assertion::assertEquals($this->it['siteaccess'], $this->scriptOutput);
174
    }
175
176
    /**
177
     * Returns the name of an existing siteaccess that isn't the default one.
178
     * @return string|null The siteaccess name, or null if there isn't one
179
     */
180
    private function getNonDefaultSiteaccessName()
181
    {
182
        $defaultSiteaccessName = $this->getDefaultSiteaccessName();
183
        foreach ($this->siteaccessList as $siteaccessName) {
184
            if ($siteaccessName !== $defaultSiteaccessName) {
185
                return $siteaccessName;
186
            }
187
        }
188
189
        return null;
190
    }
191
192
    /**
193
     * @return ConfigResolverInterface
194
     */
195
    private function getConfigResolver()
196
    {
197
        return $this->configResolver;
198
    }
199
200
    private function getDefaultSiteaccessName()
201
    {
202
        return $this->defaultSiteaccess;
203
    }
204
}
205