Completed
Pull Request — master (#161)
by
unknown
02:39
created

TableNodeTest::testGetLines()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
278
        $table->mergeRowsFromTable($new2);
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Gherkin\Node\TableNode::mergeRowsFromTable() has been deprecated with message: remove together with OutlineNode::getExampleTable

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
279
280
        $this->assertEquals(array('id', 'username', 'password'), $table->getRow(0));
281
        $this->assertEquals(array('2', 'antono', 'pa$sword'), $table->getRow(2));
282
        $this->assertEquals(array('242', '2everzet', '2qwerty'), $table->getRow(3));
283
        $this->assertEquals(array('32', '3antono', '3pa$sword'), $table->getRow(6));
284
    }
285
286
    /**
287
     * @expectedException \Behat\Gherkin\Exception\NodeException
288
     */
289
    public function testMergeRowsFromTableWrongHeaderNameExceptionThrown()
290
    {
291
        $table = new TableNode(array(
292
            5  => array('id', 'username', 'password'),
293
            10 => array('42', 'everzet', 'qwerty'),
294
            13 => array('2', 'antono', 'pa$sword')
295
        ));
296
297
        $new = new TableNode(array(
298
            25  => array('id', 'QWE', 'password'),
299
            210 => array('242', '2everzet', '2qwerty')
300
        ));
301
302
        $table->mergeRowsFromTable($new);
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Gherkin\Node\TableNode::mergeRowsFromTable() has been deprecated with message: remove together with OutlineNode::getExampleTable

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
303
    }
304
305
    /**
306
     * @expectedException \Behat\Gherkin\Exception\NodeException
307
     */
308
    public function testGetTableFromListWithMultidimensionalArrayArgument()
309
    {
310
        TableNode::fromList(array(
311
            array(1, 2, 3),
312
            array(4, 5, 6)
313
        ));
314
    }
315
316
    /**
317
     * @expectedException \Behat\Gherkin\Exception\NodeException
318
     */
319
    public function testMergeRowsFromTableWrongHeaderOrderExceptionThrown()
320
    {
321
        $table = new TableNode(array(
322
            5  => array('id', 'username', 'password'),
323
            10 => array('42', 'everzet', 'qwerty'),
324
            13 => array('2', 'antono', 'pa$sword')
325
        ));
326
327
        $new = new TableNode(array(
328
            25  => array('id', 'password', 'username'),
329
            210 => array('242', '2everzet', '2qwerty')
330
        ));
331
332
        $table->mergeRowsFromTable($new);
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Gherkin\Node\TableNode::mergeRowsFromTable() has been deprecated with message: remove together with OutlineNode::getExampleTable

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
333
    }
334
335
    /**
336
     * @expectedException \Behat\Gherkin\Exception\NodeException
337
     */
338
    public function testMergeRowsFromTableWrongHeaderSizeExceptionThrown()
339
    {
340
        $table = new TableNode(array(
341
            5  => array('id', 'username', 'password'),
342
            10 => array('42', 'everzet', 'qwerty'),
343
            13 => array('2', 'antono', 'pa$sword')
344
        ));
345
346
        $new = new TableNode(array(
347
            25  => array('id', 'username'),
348
            210 => array('242', '2everzet')
349
        ));
350
351
        $table->mergeRowsFromTable($new);
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Gherkin\Node\TableNode::mergeRowsFromTable() has been deprecated with message: remove together with OutlineNode::getExampleTable

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
352
    }
353
}
354