Completed
Push — 1.6 ( e858fe...5f8889 )
by Kamil
16:37
created

IndexPage   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 5
dl 0
loc 143
rs 10
c 0
b 0
f 0

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