Completed
Push — master ( cd607a...5ed365 )
by Christophe
12:54 queued 11:29
created

testConstructorExpectsEqualRowLengths()   A

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 0
1
<?php
2
3
namespace Tests\Behat\Gherkin\Node;
4
5
use Behat\Gherkin\Node\TableNode;
6
use PHPUnit\Framework\TestCase;
7
8
class TableNodeTest extends TestCase
9
{
10
    /**
11
     * @expectedException \Behat\Gherkin\Exception\NodeException
12
     */
13
    public function testConstructorExpectsSameNumberOfColumnsInEachRow()
14
    {
15
        new TableNode(array(
16
            array('username', 'password'),
17
            array('everzet'),
18
            array('antono', 'pa$sword')
19
        ));
20
    }
21
22
    /**
23
     * @expectedException \Behat\Gherkin\Exception\NodeException
24
     * @expectedExceptionMessage Table row '0' is expected to be array, got string
25
     */
26
    public function testConstructorExpectsTwoDimensionalArray()
27
    {
28
        new TableNode(array(
29
            'everzet', 'antono'
30
        ));
31
    }
32
33
    /**
34
     * @expectedException \Behat\Gherkin\Exception\NodeException
35
     * @expectedExceptionMessage Table cell at row '0', col '0' is expected to be scalar, got array
36
     */
37
    public function testConstructorExpectsScalarCellValue()
38
    {
39
        new TableNode(array(
40
            array(array('everzet', 'antono'))
41
        ));
42
    }
43
44
    /**
45
     * @expectedException \Behat\Gherkin\Exception\NodeException
46
     * @expectedExceptionMessage Table row '1' is expected to have 2 columns, got 1
47
     */
48
    public function testConstructorExpectsEqualRowLengths()
49
    {
50
        new TableNode(array(
51
            array('everzet', 'antono'),
52
            array('everzet'),
53
        ));
54
    }
55
56
    public function testHashTable()
57
    {
58
        $table = new TableNode(array(
59
            array('username', 'password'),
60
            array('everzet', 'qwerty'),
61
            array('antono', 'pa$sword')
62
        ));
63
64
        $this->assertEquals(
65
            array(
66
                array('username' => 'everzet', 'password' => 'qwerty')
67
              , array('username' => 'antono', 'password' => 'pa$sword')
68
            ),
69
            $table->getHash()
70
        );
71
72
        $table = new TableNode(array(
73
            array('username', 'password'),
74
            array('', 'qwerty'),
75
            array('antono', ''),
76
            array('', '')
77
        ));
78
79
        $this->assertEquals(
80
            array(
81
                array('username' => '', 'password' => 'qwerty'),
82
                array('username' => 'antono', 'password' => ''),
83
                array('username' => '', 'password' => ''),
84
            ),
85
            $table->getHash()
86
        );
87
    }
88
89
    public function testIterator()
90
    {
91
        $table = new TableNode(array(
92
            array('username', 'password'),
93
            array('', 'qwerty'),
94
            array('antono', ''),
95
            array('', ''),
96
        ));
97
98
        $this->assertEquals(
99
            array(
100
                array('username' => '', 'password' => 'qwerty'),
101
                array('username' => 'antono', 'password' => ''),
102
                array('username' => '', 'password' => ''),
103
            ),
104
            iterator_to_array($table)
105
        );
106
    }
107
108
    public function testRowsHashTable()
109
    {
110
        $table = new TableNode(array(
111
            array('username', 'everzet'),
112
            array('password', 'qwerty'),
113
            array('uid', '35'),
114
        ));
115
116
        $this->assertEquals(
117
            array('username' => 'everzet', 'password' => 'qwerty', 'uid' => '35'),
118
            $table->getRowsHash()
119
        );
120
    }
121
122
    public function testLongRowsHashTable()
123
    {
124
        $table = new TableNode(array(
125
            array('username', 'everzet', 'marcello'),
126
            array('password', 'qwerty', '12345'),
127
            array('uid', '35', '22')
128
        ));
129
130
        $this->assertEquals(array(
131
            'username' => array('everzet', 'marcello'),
132
            'password' => array('qwerty', '12345'),
133
            'uid'      => array('35', '22')
134
        ), $table->getRowsHash());
135
    }
136
137
    public function testGetRows()
138
    {
139
        $table = new TableNode(array(
140
            array('username', 'password'),
141
            array('everzet', 'qwerty'),
142
            array('antono', 'pa$sword')
143
        ));
144
145
        $this->assertEquals(array(
146
            array('username', 'password'),
147
            array('everzet', 'qwerty'),
148
            array('antono', 'pa$sword')
149
        ), $table->getRows());
150
    }
151
152
    public function testGetLines()
153
    {
154
        $table = new TableNode(array(
155
            5  => array('username', 'password'),
156
            10 => array('everzet', 'qwerty'),
157
            13 => array('antono', 'pa$sword')
158
        ));
159
160
        $this->assertEquals(array(5, 10, 13), $table->getLines());
161
    }
162
163
    public function testGetRow()
164
    {
165
        $table = new TableNode(array(
166
            array('username', 'password'),
167
            array('everzet', 'qwerty'),
168
            array('antono', 'pa$sword')
169
        ));
170
171
        $this->assertEquals(array('username', 'password'), $table->getRow(0));
172
        $this->assertEquals(array('antono', 'pa$sword'), $table->getRow(2));
173
    }
174
175
    public function testGetColumn()
176
    {
177
        $table = new TableNode(array(
178
            array('username', 'password'),
179
            array('everzet', 'qwerty'),
180
            array('antono', 'pa$sword')
181
        ));
182
183
        $this->assertEquals(array('username', 'everzet', 'antono'), $table->getColumn(0));
184
        $this->assertEquals(array('password', 'qwerty', 'pa$sword'), $table->getColumn(1));
185
186
        $table = new TableNode(array(
187
            array('username'),
188
            array('everzet'),
189
            array('antono')
190
        ));
191
192
        $this->assertEquals(array('username', 'everzet', 'antono'), $table->getColumn(0));
193
    }
194
195
    public function testGetRowWithLineNumbers()
196
    {
197
        $table = new TableNode(array(
198
            5  => array('username', 'password'),
199
            10 => array('everzet', 'qwerty'),
200
            13 => array('antono', 'pa$sword')
201
        ));
202
203
        $this->assertEquals(array('username', 'password'), $table->getRow(0));
204
        $this->assertEquals(array('antono', 'pa$sword'), $table->getRow(2));
205
    }
206
207
    public function testGetTable()
208
    {
209
        $table = new TableNode($a = array(
210
            5  => array('username', 'password'),
211
            10 => array('everzet', 'qwerty'),
212
            13 => array('antono', 'pa$sword')
213
        ));
214
215
        $this->assertEquals($a, $table->getTable());
216
    }
217
218
    public function testGetRowLine()
219
    {
220
        $table = new TableNode(array(
221
            5  => array('username', 'password'),
222
            10 => array('everzet', 'qwerty'),
223
            13 => array('antono', 'pa$sword')
224
        ));
225
226
        $this->assertEquals(5, $table->getRowLine(0));
227
        $this->assertEquals(13, $table->getRowLine(2));
228
    }
229
230
    public function testGetRowAsString()
231
    {
232
        $table = new TableNode(array(
233
            5  => array('username', 'password'),
234
            10 => array('everzet', 'qwerty'),
235
            13 => array('antono', 'pa$sword')
236
        ));
237
238
        $this->assertEquals('| username | password |', $table->getRowAsString(0));
239
        $this->assertEquals('| antono   | pa$sword |', $table->getRowAsString(2));
240
    }
241
242
    public function testGetTableAsString()
243
    {
244
        $table = new TableNode(array(
245
            5  => array('id', 'username', 'password'),
246
            10 => array('42', 'everzet', 'qwerty'),
247
            13 => array('2', 'antono', 'pa$sword')
248
        ));
249
250
        $expected = <<<TABLE
251
| id | username | password |
252
| 42 | everzet  | qwerty   |
253
| 2  | antono   | pa\$sword |
254
TABLE;
255
        $this->assertEquals($expected, $table->getTableAsString());
256
    }
257
258
    public function testFromList()
259
    {
260
        $table = TableNode::fromList(array(
261
            'everzet',
262
            'antono'
263
        ));
264
265
        $expected = new TableNode(array(
266
            array('everzet'),
267
            array('antono'),
268
        ));
269
        $this->assertEquals($expected, $table);
270
    }
271
272
    /**
273
     * @expectedException \Behat\Gherkin\Exception\NodeException
274
     */
275
    public function testGetTableFromListWithMultidimensionalArrayArgument()
276
    {
277
        TableNode::fromList(array(
278
            array(1, 2, 3),
279
            array(4, 5, 6)
280
        ));
281
    }
282
283
}
284