Completed
Push — master ( c4e93d...f0fa26 )
by
unknown
79:49 queued 61:06
created

ConsoleContext   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 194
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 4

14 Methods

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