Completed
Push — promotions-actions-rules-fixes ( ae1eb5 )
by Kamil
31:28 queued 10:28
created

CreatePage::getLastCollectionItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
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
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('rule'));
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 addAction($actionName)
66
    {
67
        $count = count($this->getCollectionItems('actions'));
68
69
        $this->getDocument()->clickLink('Add action');
70
71
        $this->getDocument()->waitFor(5, function () use ($count) {
72
            return $count + 1 === count($this->getCollectionItems('actions'));
73
        });
74
75
        $this->selectActionOption('Type', $actionName);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function selectActionOption($option, $value, $multiple = false)
82
    {
83
        $this->getLastCollectionItem('actions')->find('named', array('select', $option))->selectOption($value, $multiple);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function fillActionOption($option, $value)
90
    {
91
        $this->getLastCollectionItem('actions')->fillField($option, $value);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function fillUsageLimit($limit)
98
    {
99
        $this->getDocument()->fillField('Usage limit', $limit);
100
    }
101
102
    public function makeExclusive()
103
    {
104
        $this->getDocument()->checkField('Exclusive');
105
    }
106
107
    public function checkCouponBased()
108
    {
109
        $this->getDocument()->checkField('Coupon based');
110
    }
111
112
    public function checkChannel($name)
113
    {
114
        $this->getDocument()->checkField($name);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function setStartsAt(\DateTime $dateTime)
121
    {
122
        $timestamp = $dateTime->getTimestamp();
123
124
        $this->getDocument()->fillField('sylius_promotion_startsAt_date', date('Y-m-d', $timestamp));
125
        $this->getDocument()->fillField('sylius_promotion_startsAt_time', date('H:i', $timestamp));
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function setEndsAt(\DateTime $dateTime)
132
    {
133
        $timestamp = $dateTime->getTimestamp();
134
135
        $this->getDocument()->fillField('sylius_promotion_endsAt_date', date('Y-m-d', $timestamp));
136
        $this->getDocument()->fillField('sylius_promotion_endsAt_time', date('H:i', $timestamp));
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function getValidationMessageForAction()
143
    {
144
        $actionForm = $this->getLastCollectionItem('actions');
145
146
        $foundElement = $actionForm->find('css', '.sylius-validation-error');
147
        if (null === $foundElement) {
148
            throw new ElementNotFoundException($this->getSession(), 'Tag', 'css', '.sylius-validation-error');
149
        }
150
151
        return $foundElement->getText();
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function selectFilterOption($option, $value, $multiple = false)
158
    {
159
        $this->getLastCollectionItem('actions')->find('named', array('select', $option))->selectOption($value, $multiple);
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    protected function getDefinedElements()
166
    {
167
        return [
168
            'actions' => '#sylius_promotion_actions',
169
            'code' => '#sylius_promotion_code',
170
            'ends_at' => '#sylius_promotion_endsAt',
171
            'minimum' => '#sylius_promotion_actions_0_configuration_filters_price_range_filter_min',
172
            'maximum' => '#sylius_promotion_actions_0_configuration_filters_price_range_filter_max',
173
            'name' => '#sylius_promotion_name',
174
            'rules' => '#sylius_promotion_rules',
175
            'starts_at' => '#sylius_promotion_startsAt',
176
        ];
177
    }
178
179
    /**
180
     * @param string $collection
181
     *
182
     * @return NodeElement
183
     */
184
    private function getLastCollectionItem($collection)
185
    {
186
        $items = $this->getCollectionItems($collection);
187
188
        Assert::notEmpty($items);
189
190
        return end($items);
191
    }
192
193
    /**
194
     * @param string $collection
195
     *
196
     * @return NodeElement[]
0 ignored issues
show
Documentation introduced by
Should the return type not be NodeElement|false?

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...
197
     */
198
    private function getCollectionItems($collection)
199
    {
200
        $items = $this->getElement($collection)->findAll('css', 'div[data-form-collection="item"]');
201
202
        Assert::notEmpty($items);
203
204
        return end($items);
205
    }
206
}
207