Completed
Push — master ( 26132b...c9e982 )
by Kamil
31:04 queued 12:47
created

CreatePage::fillRuleOptionForChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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