Completed
Push — example/managing-articles ( a6c159...27a4e5 )
by Loïc
03:23 queued 51s
created

IndexPage   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 0
loc 182
rs 10
c 0
b 0
f 0

14 Methods

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