Completed
Push — master ( 84981d...080a22 )
by Kamil
05:57
created

IndexPage::sort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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
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) {
0 ignored issues
show
Bug introduced by
The class Behat\Mink\Exception\ElementNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The class Behat\Mink\Exception\ElementNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The class Behat\Mink\Exception\ElementNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
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