Passed
Push — master ( 8f0a1e...89cf34 )
by Loïc
05:43 queued 11s
created

AbstractIndexPage::deleteResourceOnPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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