Completed
Push — master ( 284884...b70f30 )
by Kamil
20:25
created

InstallerContext::iDoNotProvideSurname()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
namespace Sylius\Behat\Context\Cli;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Bundle\InstallerBundle\Command\InstallSampleDataCommand;
16
use Sylius\Bundle\InstallerBundle\Command\SetupCommand;
17
use Symfony\Bundle\FrameworkBundle\Console\Application;
18
use Symfony\Component\Console\Helper\DialogHelper;
19
use Symfony\Component\Console\Tester\CommandTester;
20
use Symfony\Component\EventDispatcher\EventDispatcher;
21
use Symfony\Component\HttpKernel\KernelInterface;
22
23
/**
24
 * @author Magdalena Banasiak <[email protected]>
25
 */
26
final class InstallerContext implements Context
27
{
28
    /**
29
     * @var KernelInterface
30
     */
31
    private $kernel;
32
33
    /**
34
     * @var Application
35
     */
36
    private $application;
37
38
    /**
39
     * @var CommandTester
40
     */
41
    private $tester;
42
43
    /**
44
     * @var DialogHelper
45
     */
46
    private $dialog;
47
48
    /**
49
     * @var SetupCommand
50
     */
51
    private $command;
52
53
    /**
54
     * @var array
55
     */
56
    private $inputChoices = [
57
        'currency' => '',
58
        'name' => ' Name',
59
        'surname' => ' Surname',
60
        'e-mail' => ' [email protected]',
61
        'password' => ' pswd',
62
        'confirmation' => ' pswd',
63
    ];
64
65
    /**
66
     * @param KernelInterface $kernel
67
     */
68
    public function __construct(KernelInterface $kernel)
69
    {
70
        $this->kernel = $kernel;
71
    }
72
73
    /**
74
     * @When I run Sylius CLI installer
75
     */
76
    public function iRunSyliusCommandLineInstaller()
77
    {
78
        $this->application = new Application($this->kernel);
79
        $this->application->add(new SetupCommand());
80
81
        $this->command = $this->application->find('sylius:install:setup');
82
        $this->tester = new CommandTester($this->command);
83
84
        $this->iExecuteCommandWithInputChoices('sylius:install:setup');
85
    }
86
87
    /**
88
     * @Given I run Sylius Install Load Sample Data command
89
     */
90
    public function iRunSyliusInstallSampleDataCommand()
91
    {
92
        $this->application = new Application($this->kernel);
93
        $this->application->add(new InstallSampleDataCommand());
94
        $this->command = $this->application->find('sylius:install:sample-data');
95
        $this->tester = new CommandTester($this->command);
96
    }
97
98
    /**
99
     * @Given I confirm loading sample data
100
     */
101
    public function iConfirmLoadingData()
102
    {
103
        $this->iExecuteCommandAndConfirm('sylius:install:sample-data');
104
    }
105
106
    /**
107
     * @Then the command should finish successfully
108
     */
109
    public function commandSuccess()
110
    {
111
        expect($this->tester->getStatusCode())->toBe(0);
112
    }
113
114
    /**
115
     * @Then I should see output :text
116
     */
117
    public function iShouldSeeOutput($text)
118
    {
119
        \PHPUnit_Framework_Assert::assertContains($text, $this->tester->getDisplay());
120
    }
121
122
    /**
123
     * @Given I do not provide a currency
124
     */
125
    public function iDoNotProvideCurrency()
126
    {
127
        $this->inputChoices['currency'] = '';
128
    }
129
130
    /**
131
     * @Given I do not provide a name
132
     */
133
    public function iDoNotProvideName()
134
    {
135
        array_splice($this->inputChoices, 1, 0, '');
136
    }
137
138
    /**
139
     * @Given I do not provide a surname
140
     */
141
    public function iDoNotProvideSurname()
142
    {
143
        array_splice($this->inputChoices, 2, 0, '');
144
    }
145
146
    /**
147
     * @Given I do not provide an email
148
     */
149
    public function iDoNotProvideEmail()
150
    {
151
        array_splice($this->inputChoices, 3, 0, '');
152
    }
153
154
    /**
155
     * @Given I do not provide a correct email
156
     */
157
    public function iDoNotProvideCorrectEmail()
158
    {
159
        array_splice($this->inputChoices, 3, 0, 'email');
160
    }
161
162
    /**
163
     * @Given I provide currency :code
164
     */
165
    public function iProvideCurrency($code)
166
    {
167
        $this->inputChoices['currency'] = $code;
168
    }
169
170
    /**
171
     * @Given I provide full administrator data
172
     */
173
    public function iProvideFullAdministratorData()
174
    {
175
        $this->inputChoices['name'] = 'AdminName';
176
        $this->inputChoices['surname'] = 'AdminSurname';
177
        $this->inputChoices['e-mail'] = '[email protected]';
178
        $this->inputChoices['password'] = 'pswd1$';
179
        $this->inputChoices['confirmation'] = $this->inputChoices['password'];
180
    }
181
182
    /**
183
     * @param string $input
184
     *
185
     * @return resource
186
     */
187
    protected function getInputStream($input)
188
    {
189
        $stream = fopen('php://memory', 'r+', false);
190
        fputs($stream, $input);
191
        rewind($stream);
192
193
        return $stream;
194
    }
195
196
    /**
197
     * @param string $name
198
     */
199
    private function iExecuteCommandWithInputChoices($name)
200
    {
201
        $this->dialog = $this->command->getHelper('dialog');
202
        $inputString = join(PHP_EOL, $this->inputChoices);
203
        $this->dialog->setInputStream($this->getInputStream($inputString.PHP_EOL));
204
205
        $this->tester->execute(['command' => $name]);
206
    }
207
208
    /**
209
     * @param string $name
210
     */
211
    private function iExecuteCommandAndConfirm($name)
212
    {
213
        $this->dialog = $this->command->getHelper('dialog');
214
        $inputString = 'y'.PHP_EOL;
215
        $this->dialog->setInputStream($this->getInputStream($inputString));
216
217
        $this->tester->execute(['command' => $name]);
218
    }
219
}
220