Completed
Pull Request — 3.1 (#348)
by Piotr
29:38 queued 15:17
created

Page   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 6
dl 0
loc 110
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeader() 0 4 1
A getCollection() 0 4 1
A getNonEditableCollection() 0 4 1
A openWithoutVerifying() 0 5 1
A getStatusCode() 0 4 1
A hasBatchActionsDropdown() 0 4 1
A hasBatchAction() 0 6 1
A pressBatchCheckboxInRow() 0 5 1
A pressBatchActionConfirmationButton() 0 4 1
A selectBatchAction() 0 4 1
A getColumnPosition() 0 14 4
A getCell() 0 6 1
A getPopover() 0 4 1
A isOpen() 0 6 1
A verifyUrl() 0 12 2
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\Behat\Page;
13
14
use Behat\Mink\Element\NodeElement;
15
use Rize\UriTemplate;
16
use SensioLabs\Behat\PageObjectExtension\PageObject\Exception\UnexpectedPageException;
17
use SensioLabs\Behat\PageObjectExtension\PageObject\Page as BasePage;
18
19
class Page extends BasePage
20
{
21
    const REDIRECT_URI = '/&redirect_uri=.{1,}/';
22
    const QUERY = 'query';
23
24
    protected $elements = [
25
        'page header' => '#page-header',
26
    ];
27
28
    public function getHeader(): string
29
    {
30
        return $this->getElement('page header')->getText();
31
    }
32
33
    public function getCollection(string $label): ?NodeElement
34
    {
35
        return $this->find('xpath', sprintf('//div[@data-prototype]/ancestor::*[@class = "form-group"]/label[text() = "%s"]/..//div[@data-prototype]', $label));
36
    }
37
38
    public function getNonEditableCollection(string $label): ?NodeElement
39
    {
40
        return $this->find('xpath', sprintf('//div[@data-prototype-name]/ancestor::*[@class = "form-group"]/label[text() = "%s"]/..//div[@data-prototype-name]', $label));
41
    }
42
43
    public function openWithoutVerifying(array $urlParameters = []): void
44
    {
45
        $url = $this->getUrl($urlParameters);
46
        $this->getDriver()->visit($url);
47
    }
48
49
    public function getStatusCode(): int
50
    {
51
        return $this->getDriver()->getStatusCode();
52
    }
53
54
    public function hasBatchActionsDropdown(): bool
55
    {
56
        return $this->has('css', 'select[data-datagrid-name]');
57
    }
58
59
    public function hasBatchAction(string $value): bool
60
    {
61
        $select = $this->find('css', 'select[data-datagrid-name]');
62
63
        return $select->has('css', sprintf('option:contains("%s")', $value));
64
    }
65
66
    public function pressBatchCheckboxInRow(int $rowIndex): void
67
    {
68
        $tr = $this->find('xpath', sprintf('descendant-or-self::table/tbody/tr[position() = %d]', $rowIndex));
69
        $tr->find('css', 'input[type="checkbox"]')->check();
70
    }
71
72
    public function pressBatchActionConfirmationButton()
73
    {
74
        $this->find('css', 'button[data-datagrid-name]')->click();
75
    }
76
77
    public function selectBatchAction($action)
78
    {
79
        $this->find('css', 'select[data-datagrid-name]')->selectOption($action);
80
    }
81
82
    public function getColumnPosition(string $columnHeader): int
83
    {
84
        $headers = $this->findAll('css', 'th');
85
        foreach ($headers as $index => $header) {
86
            /** @var NodeElement $header */
87
            if ($header->has('css', 'span')
88
                && $header->find('css', 'span')->getText() === $columnHeader
89
            ) {
90
                return $index + 1;
91
            }
92
        }
93
94
        throw new UnexpectedPageException(sprintf('Can\'t find column %s', $columnHeader));
95
    }
96
97
    public function getCell(string $columnHeader, int $rowNumber): ?NodeElement
98
    {
99
        $columnPos = $this->getColumnPosition($columnHeader);
100
101
        return $this->find('xpath', sprintf('descendant-or-self::table/tbody/tr[%d]/td[%d]', $rowNumber, $columnPos));
102
    }
103
104
    public function getPopover(): ?NodeElement
105
    {
106
        return $this->find('css', '.popover');
107
    }
108
109
    public function isOpen(array $urlParameters = []): bool
110
    {
111
        $this->verify($urlParameters);
112
113
        return true;
114
    }
115
116
    protected function verifyUrl(array $urlParameters = []): void
117
    {
118
        $uriTemplate = new UriTemplate();
119
        $expectedUri = $uriTemplate->expand($this->path, $urlParameters);
120
        if (strpos($this->getDriver()->getCurrentUrl(), $expectedUri) === false) {
121
            throw new UnexpectedPageException(sprintf(
122
                'Expected to be on "%s" but found "%s" instead',
123
                $expectedUri,
124
                $this->getDriver()->getCurrentUrl()
125
            ));
126
        }
127
    }
128
}
129