Completed
Push — master ( 6edc1d...e0dfb5 )
by Yaroslav
28:21
created

SonataListContext::getCellIndex()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
use Behat\Behat\Context\Context;
4
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
5
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
6
use Behat\Mink\Element\NodeElement;
7
8
class SonataListContext implements Context
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
    /**
11
     * @var MinkContext
12
     */
13
    private $minkContext;
14
15
    /**
16
     * @BeforeScenario
17
     * @param BeforeScenarioScope $scope
18
     */
19
    public function gatherContexts(BeforeScenarioScope $scope): void
20
    {
21
        /** @var InitializedContextEnvironment $environment */
22
        $environment = $scope->getEnvironment();
23
        $this->minkContext = $environment->getContext(MinkContext::class);
24
    }
25
26
    /**
27
     * @Given /^I click on "([^"]*)" action for entity with "([^"]*)" "([^"]*)"$/
28
     * @param string $actionLocator
29
     * @param string $cellName
30
     * @param string $cellValue
31
     */
32
    public function iClickForEntityWith(string $actionLocator, string $cellName, string $cellValue): void
33
    {
34
        $rowElement = $this->getRowByCellValue($cellName, $cellValue);
35
        $actionColumnIndex = $this->getCellIndex('Action');
36
        $cellElement = $this->getCellInRowByIndex($rowElement, $actionColumnIndex);
37
        $actionElement = $cellElement->find('named', ['link', $actionLocator]);
38
39
        if (!$actionElement) {
40
            throw new \InvalidArgumentException(sprintf("Action '%s' not found", $actionLocator));
41
        }
42
43
        $actionElement->click();
44
    }
45
46
    /**
47
     * @param string $cellName
48
     * @param string $cellValue
49
     * @return NodeElement
50
     */
51
    private function getRowByCellValue(string $cellName, string $cellValue): NodeElement
52
    {
53
        $index = $this->getCellIndex($cellName);
54
        $tableRowElements = $this->minkContext->getSession()->getPage()->findAll('css', 'table tr');
55
        // Skipping header
56
        array_shift($tableRowElements);
57
58
        /** @var NodeElement $tableRowElement */
59
        foreach ($tableRowElements as $tableRowElement) {
60
            $cellElement = $this->getCellInRowByIndex($tableRowElement, $index);
61
62
            if (trim($cellElement->getText()) === $cellValue) {
63
                return $tableRowElement;
64
            }
65
        }
66
67
        throw new InvalidArgumentException(sprintf("Row with column '%s' value '%s' not found", $cellName, $cellValue));
68
    }
69
70
    /**
71
     * @param NodeElement $rowElement
72
     * @param int $index
73
     * @return NodeElement
74
     */
75
    private function getCellInRowByIndex(NodeElement $rowElement, int $index): NodeElement
76
    {
77
        $cellElements = $rowElement->findAll('css', 'td');
78
        $i = 0;
79
80
        foreach ($cellElements as $cellElement) {
81
            $i++;
82
83
            if ($i === $index) {
84
                return $cellElement;
85
            }
86
        }
87
88
        throw new InvalidArgumentException(sprintf("Cell with index '%d' not found", $index));
89
    }
90
91
    /**
92
     * @param string $cellName
93
     * @return int|null
94
     */
95
    private function getCellIndex(string $cellName): ?int
96
    {
97
        $columnNameElements = $this->minkContext->getSession()->getPage()->findAll('css', 'table th');
98
        $index = 0;
99
100
        /** @var NodeElement $columnNameElement */
101
        foreach ($columnNameElements as $columnNameElement) {
102
            $index++;
103
104
            if (trim($columnNameElement->getText()) === $cellName) {
105
                return $index;
106
            }
107
        }
108
109
        throw new \InvalidArgumentException(sprintf("Index for column '%s' not found"));
110
    }
111
}
112