Completed
Pull Request — 3.1 (#348)
by Piotr
06:19 queued 04:50
created

FiltersContext   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 33
lcom 2
cbo 6
dl 0
loc 245
rs 9.76
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A elementDatasourceMaxResultsIsSet() 0 5 1
A elementHaveDatasourceWithFields() 0 7 1
A elementHaveDatasourceWithoutFilters() 0 15 3
A bothSortingButtonsInColumnHeaderShouldBeActive() 0 4 1
A iPressButtonInColumnHeader() 0 4 1
A buttonInColumnHeaderShouldBeDisabled() 0 15 3
A buttonInColumnHeaderShouldBeActive() 0 15 3
A iChangeElementsPerPageTo() 0 4 1
A iShouldNotSeeAnyFilters() 0 4 1
A iShouldSeeSimpleTextFilter() 0 4 1
A iShouldSeeBetweenFilterWithAndSimpleTextFields() 0 4 1
A iShouldSeeChoiceFilter() 0 4 1
A iFillSimpleTextFilterWithValue() 0 4 1
A iSelectInChoiceFilter() 0 4 1
A iPressSearchButton() 0 4 1
A simpleTextFilterShouldBeFilledWithValue() 0 4 1
A choiceFilterShouldHaveValueSelected() 0 6 1
A iShouldSeeActionsDropdownWithFollowingOptions() 0 8 2
A iShouldSeeConfirmationButton() 0 4 1
A getDataSource() 0 8 2
A clearDataSource() 0 4 1
A getFiltersElement() 0 4 1
A getListElement() 0 4 1
A getListResultsElement() 0 4 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\Behat\Context;
13
14
use Behat\Gherkin\Node\TableNode;
15
use FSi\Bundle\AdminBundle\Admin\CRUD\ListElement as AdminListElement;
16
use FSi\Bundle\AdminBundle\Behat\Element\Filters;
17
use FSi\Bundle\AdminBundle\Behat\Element\ListElement;
18
use FSi\Bundle\AdminBundle\Behat\Element\ListResultsElement;
19
use FSi\Bundle\AdminBundle\Behat\Page\DefaultPage;
20
use FSi\Component\DataSource\DataSourceInterface;
21
22
class FiltersContext extends AbstractContext
23
{
24
    /**
25
     * @var DefaultPage
26
     */
27
    private $defaultPage;
28
29
    /**
30
     * @var DataSourceInterface[]
31
     */
32
    private $datasources = [];
33
34
    public function __construct(DefaultPage $defaultPage)
35
    {
36
        $this->defaultPage = $defaultPage;
37
    }
38
39
    /**
40
     * @Given /^("[^"]*" element) datasource max results is set (\d+)$/
41
     */
42
    public function elementDatasourceMaxResultsIsSet(AdminListElement $adminElement, $maxResults)
43
    {
44
        expect($this->getDataSource($adminElement)->getMaxResults())->toBe($maxResults);
45
        $this->clearDataSource($adminElement);
46
    }
47
48
    /**
49
     * @Given /^("[^"]*" element) has datasource with fields$/
50
     */
51
    public function elementHaveDatasourceWithFields(AdminListElement $adminElement)
52
    {
53
        $dataSource = $this->getDataSource($adminElement);
54
55
        expect(count($dataSource->getFields()) > 0)->toBe(true);
56
        $this->clearDataSource($adminElement);
57
    }
58
59
    /**
60
     * @Given /^("[^"]*" element) has datasource without filters$/
61
     */
62
    public function elementHaveDatasourceWithoutFilters(AdminListElement $adminElement)
63
    {
64
        $dataSource = $this->getDataSource($adminElement);
65
66
        $filters = false;
67
        foreach ($dataSource->getFields() as $field) {
68
            if ($field->getOption('form_filter')) {
69
                $filters = true;
70
                break;
71
            }
72
        }
73
        expect($filters)->toBe(false);
74
75
        $this->clearDataSource($adminElement);
76
    }
77
78
    /**
79
     * @Then /^both sorting buttons in column header "([^"]*)" should be active$/
80
     */
81
    public function bothSortingButtonsInColumnHeaderShouldBeActive($column)
82
    {
83
        expect($this->getListElement()->isColumnAscSortActive($column))->toBe(true);
84
    }
85
86
    /**
87
     * @When /^I press "([^"]*)" button in "([^"]*)" column header$/
88
     */
89
    public function iPressButtonInColumnHeader($sort, $column)
90
    {
91
        $this->getListElement()->pressSortButton($column, $sort);
92
    }
93
94
    /**
95
     * @Then /^"([^"]*)" button in "([^"]*)" column header should be disabled$/
96
     */
97
    public function buttonInColumnHeaderShouldBeDisabled($sort, $column)
98
    {
99
        $list = $this->getListElement();
100
101
        switch (strtolower($sort)) {
102
            case 'sort asc':
103
                expect($list->isColumnAscSortActive($column))->toBe(false);
104
                break;
105
            case 'sort desc':
106
                expect($list->isColumnDescSortActive($column))->toBe(false);
107
                break;
108
            default :
109
                throw new \LogicException(sprintf('Unknown sorting type %s', $sort));
110
        }
111
    }
112
113
    /**
114
     * @Given /^"([^"]*)" button in "([^"]*)" column header should be active$/
115
     */
116
    public function buttonInColumnHeaderShouldBeActive($sort, $column)
117
    {
118
        $list = $this->getListElement();
119
120
        switch (strtolower($sort)) {
121
            case 'sort asc':
122
                expect($list->isColumnAscSortActive($column))->toBe(true);
123
                break;
124
            case 'sort desc':
125
                expect($list->isColumnDescSortActive($column))->toBe(true);
126
                break;
127
            default :
128
                throw new \LogicException(sprintf('Unknown sorting type %s', $sort));
129
        }
130
    }
131
132
    /**
133
     * @When /^I change elements per page to (\d+)$/
134
     */
135
    public function iChangeElementsPerPageTo($elementsCount)
136
    {
137
        $this->getListResultsElement()->setElementsPerPage((int) $elementsCount);
138
    }
139
140
    /**
141
     * @Then /^I should not see any filters$/
142
     */
143
    public function iShouldNotSeeAnyFilters()
144
    {
145
        expect($this->defaultPage->find('css', 'form.filters') === null)->toBe(true);
146
    }
147
148
    /**
149
     * @Then /^I should see simple text filter "([^"]*)"$/
150
     */
151
    public function iShouldSeeSimpleTextFilter($filterName)
152
    {
153
        expect($this->getFiltersElement()->hasField($filterName))->toBe(true);
154
    }
155
156
    /**
157
     * @Given /^I should see between filter "([^"]*)" with "([^"]*)" and "([^"]*)" simple text fields$/
158
     */
159
    public function iShouldSeeBetweenFilterWithAndSimpleTextFields($filterName, $fromName, $toName)
160
    {
161
        expect($this->getFiltersElement()->hasBetweenFilter($filterName, $fromName, $toName))->toBe(true);
162
    }
163
164
    /**
165
     * @Given /^I should see choice filter "([^"]*)"$/
166
     */
167
    public function iShouldSeeChoiceFilter($filterName)
168
    {
169
        expect($this->getFiltersElement()->hasChoiceFilter($filterName))->toBe(true);
170
    }
171
172
    /**
173
     * @Given /^I fill simple text filter "([^"]*)" with value "([^"]*)"$/
174
     */
175
    public function iFillSimpleTextFilterWithValue($filterName, $filterValue)
176
    {
177
        $this->getFiltersElement()->fillField($filterName, $filterValue);
178
    }
179
180
    /**
181
     * @When /^I select "([^"]*)" in choice filter "([^"]*)"$/
182
     */
183
    public function iSelectInChoiceFilter($filterValue, $filterName)
184
    {
185
        $this->getFiltersElement()->findField($filterName)->selectOption($filterValue);
186
    }
187
188
    /**
189
     * @Given /^I press "Search" button$/
190
     */
191
    public function iPressSearchButton()
192
    {
193
        $this->getFiltersElement()->pressButton('Search');
194
    }
195
196
    /**
197
     * @Given /^simple text filter "([^"]*)" should be filled with value "([^"]*)"$/
198
     */
199
    public function simpleTextFilterShouldBeFilledWithValue($filterName, $filterValue)
200
    {
201
        expect($this->getFiltersElement()->findField($filterName)->getValue())->toBe($filterValue);
202
    }
203
204
    /**
205
     * @Given /^choice filter "([^"]*)" should have value "([^"]*)" selected$/
206
     */
207
    public function choiceFilterShouldHaveValueSelected($filterName, $choice)
208
    {
209
        $field = $this->getFiltersElement()->findField($filterName);
210
        expect($field->find('css', sprintf('option:contains("%s")', $choice))
211
            ->getAttribute('selected'))->toBe('selected');
212
    }
213
214
    /**
215
     * @Then /^I should see actions dropdown with following options$/
216
     */
217
    public function iShouldSeeActionsDropdownWithFollowingOptions(TableNode $actions)
218
    {
219
        expect($this->defaultPage->hasBatchActionsDropdown())->toBe(true);
220
221
        foreach ($actions->getHash() as $actionRow) {
222
            expect($this->defaultPage->hasBatchAction($actionRow['Option']))->toBe(true);
223
        }
224
    }
225
226
    /**
227
     * @Given /^I should see confirmation button "([^"]*)"$/
228
     */
229
    public function iShouldSeeConfirmationButton($button)
230
    {
231
        $this->defaultPage->hasButton($button);
232
    }
233
234
    /**
235
     * @param AdminListElement $adminElement
236
     * @return DataSourceInterface
237
     */
238
    private function getDataSource(AdminListElement $adminElement)
239
    {
240
        if (!isset($this->datasources[$adminElement->getId()])) {
241
            $this->datasources[$adminElement->getId()] = $adminElement->createDataSource();
242
        }
243
244
        return $this->datasources[$adminElement->getId()];
245
    }
246
247
    private function clearDataSource(AdminListElement $element): void
248
    {
249
        $this->getContainer()->get('test.datasource.factory')->clearDataSource($element->getId());
250
    }
251
252
    private function getFiltersElement(): Filters
253
    {
254
        return $this->defaultPage->getElement('Filters');
255
    }
256
257
    private function getListElement(): ListElement
258
    {
259
        return $this->defaultPage->getElement('ListElement');
260
    }
261
262
    private function getListResultsElement(): ListResultsElement
263
    {
264
        return $this->defaultPage->getElement('ListResultsElement');
265
    }
266
}
267