TableContext::iShouldSeeColumnsInTheTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Behatch\Context;
4
5
use Behat\Gherkin\Node\TableNode;
6
7
class TableContext extends BaseContext
8
{
9
    /**
10
     * Checks that the specified table's columns match the given schema
11
     *
12
     * @Then the columns schema of the :table table should match:
13
     */
14
    public function theColumnsSchemaShouldMatch($table, TableNode $text)
15
    {
16
        $columnsSelector = "$table thead tr th";
17
        $columns = $this->getSession()->getPage()->findAll('css', $columnsSelector);
18
19
        $this->iShouldSeeColumnsInTheTable(count($text->getHash()), $table);
20
21
        foreach ($text->getHash() as $key => $column) {
22
            $this->assertEquals($column['columns'], $columns[$key]->getText());
23
        }
24
    }
25
26
    /**
27
     * Checks that the specified table contains the given number of columns
28
     *
29
     * @Then (I )should see :count column(s) in the :table table
30
     */
31
    public function iShouldSeeColumnsInTheTable($count, $table)
32
    {
33
        $columnsSelector = "$table thead tr th";
34
        $columns = $this->getSession()->getPage()->findAll('css', $columnsSelector);
35
36
        $this->assertEquals($count, count($columns));
37
    }
38
39
    /**
40
     * Checks that the specified table contains the specified number of rows in its body
41
     *
42
     * @Then (I )should see :count rows in the :index :table table
43
     */
44
    public function iShouldSeeRowsInTheNthTable($count, $index, $table)
45
    {
46
        $actual = $this->countElements('tbody tr', $index, $table);
47
        $this->assertEquals($count, $actual);
48
    }
49
50
    /**
51
     * Checks that the specified table contains the specified number of rows in its body
52
     *
53
     * @Then (I )should see :count row(s) in the :table table
54
     */
55
    public function iShouldSeeRowsInTheTable($count, $table)
56
    {
57
        $this->iShouldSeeRowsInTheNthTable($count, 1, $table);
58
    }
59
60
    /**
61
     * Checks that the data of the specified row matches the given schema
62
     *
63
     * @Then the data in the :index row of the :table table should match:
64
     */
65
    public function theDataOfTheRowShouldMatch($index, $table, TableNode $text)
66
    {
67
        $rowsSelector = "$table tbody tr";
68
        $rows = $this->getSession()->getPage()->findAll('css', $rowsSelector);
69
70
        if (!isset($rows[$index - 1])) {
71
            throw new \Exception("The row $index was not found in the '$table' table");
72
        }
73
74
        $cells = (array)$rows[$index - 1]->findAll('css', 'td');
75
        $cells = array_merge((array)$rows[$index - 1]->findAll('css', 'th'), $cells);
76
77
        $hash = current($text->getHash());
78
79
        foreach (array_keys($hash) as $columnName) {
80
            // Extract index from column. ex "col2" -> 2
81
            preg_match('/^col(?P<index>\d+)$/', $columnName, $matches);
82
            $index = (int) $matches['index'] - 1;
83
84
            $this->assertEquals($hash[$columnName], $cells[$index]->getText());
85
        }
86
    }
87
88
    /**
89
     * Checks that the specified cell (column/row) of the table's body contains the specified text
90
     *
91
     * @Then the :colIndex column of the :rowIndex row in the :table table should contain :text
92
     */
93
    public function theStColumnOfTheStRowInTheTableShouldContain($colIndex, $rowIndex, $table, $text)
94
    {
95
        $rowSelector = "$table tbody tr";
96
        $rows = $this->getSession()->getPage()->findAll('css', $rowSelector);
97
98
        if (!isset($rows[$rowIndex - 1])) {
99
            throw new \Exception("The row $rowIndex was not found in the '$table' table");
100
        }
101
102
        $row = $rows[$rowIndex - 1];
103
        $cols = $row->findAll('css', 'td');
104
105
        if (!isset($cols[$colIndex - 1])) {
106
            throw new \Exception("The column $colIndex was not found in the row $rowIndex of the '$table' table");
107
        }
108
109
        $actual = $cols[$colIndex - 1]->getText();
110
111
        $this->assertContains($text, $actual);
112
    }
113
}
114