Completed
Push — missing-product-variant-variab... ( 2e9a80...01bfdb )
by Kamil
22:39
created

CreatePage::selectAutocompleteValue()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 2
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\Page\Admin\Promotion;
13
14
use Behat\Mink\Driver\Selenium2Driver;
15
use Behat\Mink\Element\NodeElement;
16
use Behat\Mink\Exception\ElementNotFoundException;
17
use Sylius\Behat\Behaviour\NamesIt;
18
use Sylius\Behat\Behaviour\SpecifiesItsCode;
19
use Sylius\Behat\Page\Admin\Crud\CreatePage as BaseCreatePage;
20
use Sylius\Behat\Service\AutocompleteHelper;
21
use Webmozart\Assert\Assert;
22
23
/**
24
 * @author Mateusz Zalewski <[email protected]>
25
 * @author Łuksaz Zalewski <[email protected]>
26
 */
27
class CreatePage extends BaseCreatePage implements CreatePageInterface
28
{
29
    use NamesIt;
30
    use SpecifiesItsCode;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function addRule($ruleName)
36
    {
37
        $count = count($this->getCollectionItems('rules'));
38
39
        $this->getDocument()->clickLink('Add rule');
40
41
        $this->getDocument()->waitFor(5, function () use ($count) {
42
            return $count + 1 === count($this->getCollectionItems('rules'));
43
        });
44
45
        $this->selectRuleOption('Type', $ruleName);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function selectRuleOption($option, $value, $multiple = false)
52
    {
53
        $this->getLastCollectionItem('rules')->find('named', array('select', $option))->selectOption($value, $multiple);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function selectAutocompleteRuleOption($option, $value, $multiple = false)
60
    {
61
        $option = strtolower(str_replace(' ', '_', $option));
62
63
        $ruleAutocomplete = $this
64
            ->getLastCollectionItem('rules')
65
            ->find('css', sprintf('input[type="hidden"][name*="[%s]"]', $option))
66
            ->getParent()
67
        ;
68
69
        AutocompleteHelper::chooseValue($this->getSession(), $ruleAutocomplete, $value);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function fillRuleOption($option, $value)
76
    {
77
        $this->getLastCollectionItem('rules')->fillField($option, $value);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function fillRuleOptionForChannel($channelName, $option, $value)
84
    {
85
        $lastAction = $this->getChannelConfigurationOfLastRule($channelName);
86
        $lastAction->fillField($option, $value);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function addAction($actionName)
93
    {
94
        $count = count($this->getCollectionItems('actions'));
95
96
        $this->getDocument()->clickLink('Add action');
97
98
        $this->getDocument()->waitFor(5, function () use ($count) {
99
            return $count + 1 === count($this->getCollectionItems('actions'));
100
        });
101
102
        $this->selectActionOption('Type', $actionName);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function selectActionOption($option, $value, $multiple = false)
109
    {
110
        $this->getLastCollectionItem('actions')->find('named', array('select', $option))->selectOption($value, $multiple);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function fillActionOption($option, $value)
117
    {
118
        $this->getLastCollectionItem('actions')->fillField($option, $value);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function fillActionOptionForChannel($channelName, $option, $value)
125
    {
126
        $lastAction = $this->getChannelConfigurationOfLastAction($channelName);
127
        $lastAction->fillField($option, $value);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function fillUsageLimit($limit)
134
    {
135
        $this->getDocument()->fillField('Usage limit', $limit);
136
    }
137
138
    public function makeExclusive()
139
    {
140
        $this->getDocument()->checkField('Exclusive');
141
    }
142
143
    public function checkCouponBased()
144
    {
145
        $this->getDocument()->checkField('Coupon based');
146
    }
147
148
    public function checkChannel($name)
149
    {
150
        $this->getDocument()->checkField($name);
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function setStartsAt(\DateTime $dateTime)
157
    {
158
        $timestamp = $dateTime->getTimestamp();
159
160
        $this->getDocument()->fillField('sylius_promotion_startsAt_date', date('Y-m-d', $timestamp));
161
        $this->getDocument()->fillField('sylius_promotion_startsAt_time', date('H:i', $timestamp));
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function setEndsAt(\DateTime $dateTime)
168
    {
169
        $timestamp = $dateTime->getTimestamp();
170
171
        $this->getDocument()->fillField('sylius_promotion_endsAt_date', date('Y-m-d', $timestamp));
172
        $this->getDocument()->fillField('sylius_promotion_endsAt_time', date('H:i', $timestamp));
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function getValidationMessageForAction()
179
    {
180
        $actionForm = $this->getLastCollectionItem('actions');
181
182
        $foundElement = $actionForm->find('css', '.sylius-validation-error');
183
        if (null === $foundElement) {
184
            throw new ElementNotFoundException($this->getSession(), 'Tag', 'css', '.sylius-validation-error');
185
        }
186
187
        return $foundElement->getText();
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function selectAutoCompleteFilterOption($option, $value, $multiple = false)
194
    {
195
        $option = strtolower(str_replace(' ', '_', $option));
196
197
        $filterAutocomplete = $this
198
            ->getLastCollectionItem('actions')
199
            ->find('css', sprintf('input[type="hidden"][name*="[%s_filter]"]', $option))
200
            ->getParent()
201
        ;
202
203
        AutocompleteHelper::chooseValue($this->getSession(), $filterAutocomplete, $value);
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    protected function getDefinedElements()
210
    {
211
        return [
212
            'actions' => '#sylius_promotion_actions',
213
            'code' => '#sylius_promotion_code',
214
            'ends_at' => '#sylius_promotion_endsAt',
215
            'minimum' => '#sylius_promotion_actions_0_configuration_WEB-US_filters_price_range_filter_min',
216
            'maximum' => '#sylius_promotion_actions_0_configuration_WEB-US_filters_price_range_filter_max',
217
            'name' => '#sylius_promotion_name',
218
            'rules' => '#sylius_promotion_rules',
219
            'starts_at' => '#sylius_promotion_startsAt',
220
        ];
221
    }
222
223
    /**
224
     * @param string $channelName
225
     *
226
     * @return NodeElement
0 ignored issues
show
Documentation introduced by
Should the return type not be NodeElement|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
227
     */
228
    private function getChannelConfigurationOfLastAction($channelName)
229
    {
230
        return $this
231
            ->getLastCollectionItem('actions')
232
            ->find('css', sprintf('[id$="configuration"] .field:contains("%s")', $channelName))
233
        ;
234
    }
235
236
    /**
237
     * @param string $channelName
238
     *
239
     * @return NodeElement
0 ignored issues
show
Documentation introduced by
Should the return type not be NodeElement|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
240
     */
241
    private function getChannelConfigurationOfLastRule($channelName)
242
    {
243
        return $this
244
            ->getLastCollectionItem('rules')
245
            ->find('css', sprintf('[id$="configuration"] .field:contains("%s")', $channelName))
246
        ;
247
    }
248
249
    /**
250
     * @param string $collection
251
     *
252
     * @return NodeElement
253
     */
254
    private function getLastCollectionItem($collection)
255
    {
256
        $items = $this->getCollectionItems($collection);
257
258
        Assert::notEmpty($items);
259
260
        return end($items);
261
    }
262
263
    /**
264
     * @param string $collection
265
     *
266
     * @return NodeElement[]
267
     */
268
    private function getCollectionItems($collection)
269
    {
270
        $items = $this->getElement($collection)->findAll('css', 'div[data-form-collection="item"]');
271
272
        Assert::isArray($items);
273
274
        return $items;
275
    }
276
}
277