Completed
Push — sf4-moar ( 100d6e )
by Kamil
14:34
created

InstallerContext::getInputStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Behat\Context\Cli;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Bundle\CoreBundle\Command\InstallSampleDataCommand;
18
use Sylius\Bundle\CoreBundle\Command\SetupCommand;
19
use Symfony\Bundle\FrameworkBundle\Console\Application;
20
use Symfony\Component\Console\Helper\QuestionHelper;
21
use Symfony\Component\Console\Tester\CommandTester;
22
use Symfony\Component\HttpKernel\KernelInterface;
23
use Webmozart\Assert\Assert;
24
25
final class InstallerContext implements Context
26
{
27
    /**
28
     * @var KernelInterface
29
     */
30
    private $kernel;
31
32
    /**
33
     * @var Application
34
     */
35
    private $application;
36
37
    /**
38
     * @var CommandTester
39
     */
40
    private $tester;
41
    
42
    /**
43
     * @var SetupCommand
44
     */
45
    private $command;
46
47
    /**
48
     * @var array
49
     */
50
    private $inputChoices = [
51
        'currency' => 'USD',
52
        'e-mail' => '[email protected]',
53
        'username' => 'test',
54
        'password' => 'pswd',
55
        'confirmation' => 'pswd',
56
    ];
57
58
    /**
59
     * @param KernelInterface $kernel
60
     */
61
    public function __construct(KernelInterface $kernel)
62
    {
63
        $this->kernel = $kernel;
64
    }
65
66
    /**
67
     * @When I run Sylius CLI installer
68
     */
69
    public function iRunSyliusCommandLineInstaller(): void
70
    {
71
        $this->application = new Application($this->kernel);
72
        $this->application->add(new SetupCommand());
73
74
        $this->command = $this->application->find('sylius:install:setup');
75
        $this->tester = new CommandTester($this->command);
76
77
        $this->iExecuteCommandWithInputChoices('sylius:install:setup');
78
    }
79
80
    /**
81
     * @Given I run Sylius Install Load Sample Data command
82
     */
83
    public function iRunSyliusInstallSampleDataCommand(): void
84
    {
85
        $this->application = new Application($this->kernel);
86
        $this->application->add(new InstallSampleDataCommand());
87
        $this->command = $this->application->find('sylius:install:sample-data');
88
        $this->tester = new CommandTester($this->command);
89
    }
90
91
    /**
92
     * @Given I confirm loading sample data
93
     */
94
    public function iConfirmLoadingData(): void
95
    {
96
        $this->iExecuteCommandAndConfirm('sylius:install:sample-data');
97
    }
98
99
    /**
100
     * @Then the command should finish successfully
101
     */
102
    public function commandSuccess(): void
103
    {
104
        Assert::same($this->tester->getStatusCode(), 0);
105
    }
106
107
    /**
108
     * @Then I should see output :text
109
     */
110
    public function iShouldSeeOutput(string $text): void
111
    {
112
        Assert::contains($this->tester->getDisplay(), $text);
113
    }
114
115
    /**
116
     * @Given I do not provide an email
117
     */
118
    public function iDoNotProvideEmail(): void
119
    {
120
        $this->inputChoices['e-mail'] = '';
121
    }
122
123
    /**
124
     * @Given I do not provide a correct email
125
     */
126
    public function iDoNotProvideCorrectEmail(): void
127
    {
128
        $this->inputChoices['e-mail'] = 'janusz';
129
    }
130
131
    /**
132
     * @Given I provide full administrator data
133
     */
134
    public function iProvideFullAdministratorData(): void
135
    {
136
        $this->inputChoices['e-mail'] = '[email protected]';
137
        $this->inputChoices['username'] = 'test';
138
        $this->inputChoices['password'] = 'pswd1$';
139
        $this->inputChoices['confirmation'] = $this->inputChoices['password'];
140
    }
141
142
    /**
143
     * @param string $name
144
     */
145
    private function iExecuteCommandWithInputChoices(string $name): void
146
    {
147
        try {
148
            $this->tester->setInputs($this->inputChoices);
149
            $this->tester->execute(['command' => $name]);
150
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
151
        }
152
    }
153
154
    /**
155
     * @param string $name
156
     */
157
    private function iExecuteCommandAndConfirm(string $name): void
158
    {
159
        try {
160
            $this->tester->setInputs(['y']);
161
            $this->tester->execute(['command' => $name]);
162
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
163
        }
164
    }
165
}
166