Completed
Push — master ( b05eaa...0a1951 )
by Paweł
16:26 queued 07:46
created

IndexPage::filter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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