Completed
Pull Request — master (#8491)
by Stefan
18:30 queued 07:04
created

IndexPage::bulkDelete()   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 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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Page\Admin\Crud;
15
16
use Behat\Mink\Exception\ElementNotFoundException;
17
use Behat\Mink\Session;
18
use Sylius\Behat\Page\SymfonyPage;
19
use Sylius\Behat\Service\Accessor\TableAccessorInterface;
20
use Symfony\Component\Routing\RouterInterface;
21
use Webmozart\Assert\Assert;
22
23
class IndexPage extends SymfonyPage implements IndexPageInterface
24
{
25
    /**
26
     * @var TableAccessorInterface
27
     */
28
    private $tableAccessor;
29
30
    /**
31
     * @var string
32
     */
33
    private $routeName;
34
35
    /**
36
     * @param Session $session
37
     * @param array $parameters
38
     * @param RouterInterface $router
39
     * @param TableAccessorInterface $tableAccessor
40
     * @param string $routeName
41
     */
42
    public function __construct(
43
        Session $session,
44
        array $parameters,
45
        RouterInterface $router,
46
        TableAccessorInterface $tableAccessor,
47
        $routeName
48
    ) {
49
        parent::__construct($session, $parameters, $router);
50
51
        $this->tableAccessor = $tableAccessor;
52
        $this->routeName = $routeName;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function isSingleResourceOnPage(array $parameters)
59
    {
60
        try {
61
            $rows = $this->tableAccessor->getRowsWithFields($this->getElement('table'), $parameters);
62
63
            return 1 === count($rows);
64
        } catch (\InvalidArgumentException $exception) {
65
            return false;
66
        } catch (ElementNotFoundException $exception) {
67
            return false;
68
        }
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getColumnFields($columnName)
75
    {
76
        return $this->tableAccessor->getIndexedColumn($this->getElement('table'), $columnName);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function sortBy($fieldName)
83
    {
84
        $sortableHeaders = $this->tableAccessor->getSortableHeaders($this->getElement('table'));
85
        Assert::keyExists($sortableHeaders, $fieldName, sprintf('Column "%s" is not sortable.', $fieldName));
86
87
        $sortableHeaders[$fieldName]->find('css', 'a')->click();
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function isSingleResourceWithSpecificElementOnPage(array $parameters, $element)
94
    {
95
        try {
96
            $rows = $this->tableAccessor->getRowsWithFields($this->getElement('table'), $parameters);
97
98
            if (1 !== count($rows)) {
99
                return false;
100
            }
101
102
            return null !== $rows[0]->find('css', $element);
103
        } catch (\InvalidArgumentException $exception) {
104
            return false;
105
        } catch (ElementNotFoundException $exception) {
106
            return false;
107
        }
108
    }
109
110
    /**
111
     * @return int
112
     */
113
    public function countItems()
114
    {
115
        try {
116
            return $this->getTableAccessor()->countTableBodyRows($this->getElement('table'));
117
        } catch (ElementNotFoundException $exception) {
118
            return 0;
119
        }
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function deleteResourceOnPage(array $parameters)
126
    {
127
        $tableAccessor = $this->getTableAccessor();
128
        $table = $this->getElement('table');
129
130
        $deletedRow = $tableAccessor->getRowWithFields($table, $parameters);
131
        $actionButtons = $tableAccessor->getFieldFromRow($table, $deletedRow, 'actions');
132
133
        $actionButtons->pressButton('Delete');
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function getActionsForResource(array $parameters)
140
    {
141
        $tableAccessor = $this->getTableAccessor();
142
        $table = $this->getElement('table');
143
144
        $resourceRow = $tableAccessor->getRowWithFields($table, $parameters);
145
146
        return $tableAccessor->getFieldFromRow($table, $resourceRow, 'actions');
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function checkResourceOnPage(array $parameters): void
153
    {
154
        $tableAccessor = $this->getTableAccessor();
155
        $table = $this->getElement('table');
156
157
        $resourceRow = $tableAccessor->getRowWithFields($table, $parameters);
158
        $bulkCheckbox = $resourceRow->find('css', '.bulk-select-checkbox');
159
160
        Assert::notNull($bulkCheckbox);
161
162
        $bulkCheckbox->check();
163
    }
164
165
    public function filter()
166
    {
167
        $this->getElement('filter')->press();
168
    }
169
170
    public function bulkDelete(): void
171
    {
172
        $this->getElement('bulk_actions', ['%text%' => 'Bulk actions'])->pressButton('Delete');
173
        $this->getElement('confirmation_button')->click();
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function getRouteName()
180
    {
181
        return $this->routeName;
182
    }
183
184
    /**
185
     * @return TableAccessorInterface
186
     */
187
    protected function getTableAccessor()
188
    {
189
        return $this->tableAccessor;
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    protected function getDefinedElements()
196
    {
197
        return array_merge(parent::getDefinedElements(), [
198
            'bulk_actions' => '.accordion:contains("%text%")',
199
            'confirmation_button' => '#confirmation-button',
200
            'filter' => 'button:contains("Filter")',
201
            'table' => '.table',
202
        ]);
203
    }
204
}
205