Completed
Push — master ( df4d09...dfc47a )
by Ciaran
08:37
created

testMergeRowsFromTableWrongHeaderOrderExceptionThrown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
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
    public function testMergeRowsFromTablePassSeveralTablesShouldBeMerged()
272
    {
273
        $table = new TableNode(array(
274
            5  => array('id', 'username', 'password'),
275
            10 => array('42', 'everzet', 'qwerty'),
276
            13 => array('2', 'antono', 'pa$sword')
277
        ));
278
279
        $new = new TableNode(array(
280
            25  => array('id', 'username', 'password'),
281
            210 => array('242', '2everzet', '2qwerty'),
282
            213 => array('22', '2antono', '2pa$sword')
283
        ));
284
285
        $new2 = new TableNode(array(
286
            35  => array('id', 'username', 'password'),
287
            310 => array('342', '3everzet', '3qwerty'),
288
            313 => array('32', '3antono', '3pa$sword')
289
        ));
290
291
        $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...
292
        $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...
293
294
        $this->assertEquals(array('id', 'username', 'password'), $table->getRow(0));
295
        $this->assertEquals(array('2', 'antono', 'pa$sword'), $table->getRow(2));
296
        $this->assertEquals(array('242', '2everzet', '2qwerty'), $table->getRow(3));
297
        $this->assertEquals(array('32', '3antono', '3pa$sword'), $table->getRow(6));
298
    }
299
300
    /**
301
     * @expectedException \Behat\Gherkin\Exception\NodeException
302
     */
303
    public function testMergeRowsFromTableWrongHeaderNameExceptionThrown()
304
    {
305
        $table = new TableNode(array(
306
            5  => array('id', 'username', 'password'),
307
            10 => array('42', 'everzet', 'qwerty'),
308
            13 => array('2', 'antono', 'pa$sword')
309
        ));
310
311
        $new = new TableNode(array(
312
            25  => array('id', 'QWE', 'password'),
313
            210 => array('242', '2everzet', '2qwerty')
314
        ));
315
316
        $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...
317
    }
318
319
    /**
320
     * @expectedException \Behat\Gherkin\Exception\NodeException
321
     */
322
    public function testGetTableFromListWithMultidimensionalArrayArgument()
323
    {
324
        TableNode::fromList(array(
325
            array(1, 2, 3),
326
            array(4, 5, 6)
327
        ));
328
    }
329
330
    /**
331
     * @expectedException \Behat\Gherkin\Exception\NodeException
332
     */
333
    public function testMergeRowsFromTableWrongHeaderOrderExceptionThrown()
334
    {
335
        $table = new TableNode(array(
336
            5  => array('id', 'username', 'password'),
337
            10 => array('42', 'everzet', 'qwerty'),
338
            13 => array('2', 'antono', 'pa$sword')
339
        ));
340
341
        $new = new TableNode(array(
342
            25  => array('id', 'password', 'username'),
343
            210 => array('242', '2everzet', '2qwerty')
344
        ));
345
346
        $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...
347
    }
348
349
    /**
350
     * @expectedException \Behat\Gherkin\Exception\NodeException
351
     */
352
    public function testMergeRowsFromTableWrongHeaderSizeExceptionThrown()
353
    {
354
        $table = new TableNode(array(
355
            5  => array('id', 'username', 'password'),
356
            10 => array('42', 'everzet', 'qwerty'),
357
            13 => array('2', 'antono', 'pa$sword')
358
        ));
359
360
        $new = new TableNode(array(
361
            25  => array('id', 'username'),
362
            210 => array('242', '2everzet')
363
        ));
364
365
        $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...
366
    }
367
}
368