Completed
Push — master ( 59db0f...abafe8 )
by Kamil
18:55
created

InstallerContext::iDoNotProvideCurrency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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\SetupCommand;
16
use Symfony\Bundle\FrameworkBundle\Console\Application;
17
use Symfony\Component\Console\Helper\DialogHelper;
18
use Symfony\Component\Console\Tester\CommandTester;
19
use Symfony\Component\EventDispatcher\EventDispatcher;
20
use Symfony\Component\HttpKernel\KernelInterface;
21
22
/**
23
 * @author Magdalena Banasiak <[email protected]>
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 DialogHelper
44
     */
45
    private $dialog;
46
47
    /**
48
     * @var SetupCommand
49
     */
50
    private $command;
51
52
    /**
53
     * @var array
54
     */
55
    private $inputChoices = array(
56
        'currency' => '',
57
        'name' => ' Name',
58
        'surname' => ' Surname',
59
        'e-mail' => ' [email protected]',
60
        'password' => ' pswd',
61
        'confirmation' => ' pswd',
62
    );
63
64
    /**
65
     * @param KernelInterface $kernel
66
     */
67
    public function __construct(KernelInterface $kernel)
68
    {
69
        $this->kernel = $kernel;
70
    }
71
72
    /**
73
     * @When I run Sylius CLI installer
74
     */
75
    public function iRunSyliusCommandLineInstaller()
76
    {
77
        $this->application = new Application($this->kernel);
78
        $this->application->add(new SetupCommand());
79
80
        $this->command = $this->application->find('sylius:install:setup');
81
        $this->tester = new CommandTester($this->command);
82
83
        $this->iExecuteCommandWithInputChoices('sylius:install:setup');
84
    }
85
86
    /**
87
     * @Then I should see output :text
88
     */
89
    public function iShouldSeeOutput($text)
90
    {
91
        \PHPUnit_Framework_Assert::assertContains($text, $this->tester->getDisplay());
92
    }
93
94
    /**
95
     * @Given I do not provide a currency
96
     */
97
    public function iDoNotProvideCurrency()
98
    {
99
        $this->inputChoices['currency'] = '';
100
    }
101
102
    /**
103
     * @Given I do not provide a name
104
     */
105
    public function iDoNotProvideName()
106
    {
107
        array_splice($this->inputChoices, 1, 0, '');
108
    }
109
110
    /**
111
     * @Given I do not provide a surname
112
     */
113
    public function iDoNotProvideSurname()
114
    {
115
        array_splice($this->inputChoices, 2, 0, '');
116
    }
117
118
    /**
119
     * @Given I do not provide an email
120
     */
121
    public function iDoNotProvideEmail()
122
    {
123
        array_splice($this->inputChoices, 3, 0, '');
124
    }
125
126
    /**
127
     * @Given I do not provide a correct email
128
     */
129
    public function iDoNotProvideCorrectEmail()
130
    {
131
        array_splice($this->inputChoices, 3, 0, 'email');
132
    }
133
134
    /**
135
     * @Given I provide currency :code
136
     */
137
    public function iProvideCurrency($code)
138
    {
139
        $this->inputChoices['currency'] = $code;
140
    }
141
142
    /**
143
     * @Given I provide full administrator data
144
     */
145
    public function iProvideFullAdministratorData()
146
    {
147
        $this->inputChoices['name'] = 'AdminName';
148
        $this->inputChoices['surname'] = 'AdminSurname';
149
        $this->inputChoices['e-mail'] = '[email protected]';
150
        $this->inputChoices['password'] = 'pswd1$';
151
        $this->inputChoices['confirmation'] = $this->inputChoices['password'];
152
    }
153
154
    /**
155
     * @param string $input
156
     *
157
     * @return resource
158
     */
159
    protected function getInputStream($input)
160
    {
161
        $stream = fopen('php://memory', 'r+', false);
162
        fputs($stream, $input);
163
        rewind($stream);
164
165
        return $stream;
166
    }
167
168
    /**
169
     * @param string $name
170
     */
171
    private function iExecuteCommandWithInputChoices($name)
172
    {
173
        $fullParameters = array_merge(array('command' => $name));
174
        $this->dialog = $this->command->getHelper('dialog');
175
        $inputString = join(PHP_EOL, $this->inputChoices);
176
        $this->dialog->setInputStream($this->getInputStream($inputString.PHP_EOL));
177
178
        $this->tester->execute($fullParameters);
179
    }
180
}
181