Completed
Pull Request — master (#161)
by Phil
06:54 queued 03:51
created

testMergeRowsFromTablePassSeveralTablesShouldBeMerged()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
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
    public function constructorTestDataProvider() {
23
        return array(
24
            'One-dimensional array' => array(
25
                array('everzet', 'antono')
26
            ),
27
            'Three-dimensional array' => array(
28
                array(array(array('everzet', 'antono')))
29
            )
30
        );
31
    }
32
33
    /**
34
     * @dataProvider constructorTestDataProvider
35
     * @expectedException \Behat\Gherkin\Exception\NodeException
36
     * @expectedExceptionMessage Table is not two-dimensional.
37
     */
38
    public function testConstructorExpectsTwoDimensionalArrays($table)
39
    {
40
        new TableNode($table);
41
    }
42
43
    public function testHashTable()
44
    {
45
        $table = new TableNode(array(
46
            array('username', 'password'),
47
            array('everzet', 'qwerty'),
48
            array('antono', 'pa$sword')
49
        ));
50
51
        $this->assertEquals(
52
            array(
53
                array('username' => 'everzet', 'password' => 'qwerty')
54
              , array('username' => 'antono', 'password' => 'pa$sword')
55
            ),
56
            $table->getHash()
57
        );
58
59
        $table = new TableNode(array(
60
            array('username', 'password'),
61
            array('', 'qwerty'),
62
            array('antono', ''),
63
            array('', '')
64
        ));
65
66
        $this->assertEquals(
67
            array(
68
                array('username' => '', 'password' => 'qwerty'),
69
                array('username' => 'antono', 'password' => ''),
70
                array('username' => '', 'password' => ''),
71
            ),
72
            $table->getHash()
73
        );
74
    }
75
76
    public function testIterator()
77
    {
78
        $table = new TableNode(array(
79
            array('username', 'password'),
80
            array('', 'qwerty'),
81
            array('antono', ''),
82
            array('', ''),
83
        ));
84
85
        $this->assertEquals(
86
            array(
87
                array('username' => '', 'password' => 'qwerty'),
88
                array('username' => 'antono', 'password' => ''),
89
                array('username' => '', 'password' => ''),
90
            ),
91
            iterator_to_array($table)
92
        );
93
    }
94
95
    public function testRowsHashTable()
96
    {
97
        $table = new TableNode(array(
98
            array('username', 'everzet'),
99
            array('password', 'qwerty'),
100
            array('uid', '35'),
101
        ));
102
103
        $this->assertEquals(
104
            array('username' => 'everzet', 'password' => 'qwerty', 'uid' => '35'),
105
            $table->getRowsHash()
106
        );
107
    }
108
109
    public function testLongRowsHashTable()
110
    {
111
        $table = new TableNode(array(
112
            array('username', 'everzet', 'marcello'),
113
            array('password', 'qwerty', '12345'),
114
            array('uid', '35', '22')
115
        ));
116
117
        $this->assertEquals(array(
118
            'username' => array('everzet', 'marcello'),
119
            'password' => array('qwerty', '12345'),
120
            'uid'      => array('35', '22')
121
        ), $table->getRowsHash());
122
    }
123
124
    public function testGetRows()
125
    {
126
        $table = new TableNode(array(
127
            array('username', 'password'),
128
            array('everzet', 'qwerty'),
129
            array('antono', 'pa$sword')
130
        ));
131
132
        $this->assertEquals(array(
133
            array('username', 'password'),
134
            array('everzet', 'qwerty'),
135
            array('antono', 'pa$sword')
136
        ), $table->getRows());
137
    }
138
139
    public function testGetLines()
140
    {
141
        $table = new TableNode(array(
142
            5  => array('username', 'password'),
143
            10 => array('everzet', 'qwerty'),
144
            13 => array('antono', 'pa$sword')
145
        ));
146
147
        $this->assertEquals(array(5, 10, 13), $table->getLines());
148
    }
149
150
    public function testGetRow()
151
    {
152
        $table = new TableNode(array(
153
            array('username', 'password'),
154
            array('everzet', 'qwerty'),
155
            array('antono', 'pa$sword')
156
        ));
157
158
        $this->assertEquals(array('username', 'password'), $table->getRow(0));
159
        $this->assertEquals(array('antono', 'pa$sword'), $table->getRow(2));
160
    }
161
162
    public function testGetColumn()
163
    {
164
        $table = new TableNode(array(
165
            array('username', 'password'),
166
            array('everzet', 'qwerty'),
167
            array('antono', 'pa$sword')
168
        ));
169
170
        $this->assertEquals(array('username', 'everzet', 'antono'), $table->getColumn(0));
171
        $this->assertEquals(array('password', 'qwerty', 'pa$sword'), $table->getColumn(1));
172
173
        $table = new TableNode(array(
174
            array('username'),
175
            array('everzet'),
176
            array('antono')
177
        ));
178
179
        $this->assertEquals(array('username', 'everzet', 'antono'), $table->getColumn(0));
180
    }
181
182
    public function testGetRowWithLineNumbers()
183
    {
184
        $table = new TableNode(array(
185
            5  => array('username', 'password'),
186
            10 => array('everzet', 'qwerty'),
187
            13 => array('antono', 'pa$sword')
188
        ));
189
190
        $this->assertEquals(array('username', 'password'), $table->getRow(0));
191
        $this->assertEquals(array('antono', 'pa$sword'), $table->getRow(2));
192
    }
193
194
    public function testGetTable()
195
    {
196
        $table = new TableNode($a = array(
197
            5  => array('username', 'password'),
198
            10 => array('everzet', 'qwerty'),
199
            13 => array('antono', 'pa$sword')
200
        ));
201
202
        $this->assertEquals($a, $table->getTable());
203
    }
204
205
    public function testGetRowLine()
206
    {
207
        $table = new TableNode(array(
208
            5  => array('username', 'password'),
209
            10 => array('everzet', 'qwerty'),
210
            13 => array('antono', 'pa$sword')
211
        ));
212
213
        $this->assertEquals(5, $table->getRowLine(0));
214
        $this->assertEquals(13, $table->getRowLine(2));
215
    }
216
217
    public function testGetRowAsString()
218
    {
219
        $table = new TableNode(array(
220
            5  => array('username', 'password'),
221
            10 => array('everzet', 'qwerty'),
222
            13 => array('antono', 'pa$sword')
223
        ));
224
225
        $this->assertEquals('| username | password |', $table->getRowAsString(0));
226
        $this->assertEquals('| antono   | pa$sword |', $table->getRowAsString(2));
227
    }
228
229
    public function testGetTableAsString()
230
    {
231
        $table = new TableNode(array(
232
            5  => array('id', 'username', 'password'),
233
            10 => array('42', 'everzet', 'qwerty'),
234
            13 => array('2', 'antono', 'pa$sword')
235
        ));
236
237
        $expected = <<<TABLE
238
| id | username | password |
239
| 42 | everzet  | qwerty   |
240
| 2  | antono   | pa\$sword |
241
TABLE;
242
        $this->assertEquals($expected, $table->getTableAsString());
243
    }
244
245
    public function testFromList()
246
    {
247
        $table = TableNode::fromList(array(
248
            'everzet',
249
            'antono'
250
        ));
251
252
        $expected = new TableNode(array(
253
            array('everzet'),
254
            array('antono'),
255
        ));
256
        $this->assertEquals($expected, $table);
257
    }
258
    public function testMergeRowsFromTablePassSeveralTablesShouldBeMerged()
259
    {
260
        $table = new TableNode(array(
261
            5  => array('id', 'username', 'password'),
262
            10 => array('42', 'everzet', 'qwerty'),
263
            13 => array('2', 'antono', 'pa$sword')
264
        ));
265
266
        $new = new TableNode(array(
267
            25  => array('id', 'username', 'password'),
268
            210 => array('242', '2everzet', '2qwerty'),
269
            213 => array('22', '2antono', '2pa$sword')
270
        ));
271
272
        $new2 = new TableNode(array(
273
            35  => array('id', 'username', 'password'),
274
            310 => array('342', '3everzet', '3qwerty'),
275
            313 => array('32', '3antono', '3pa$sword')
276
        ));
277
278
        $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...
279
        $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...
280
281
        $this->assertEquals(array('id', 'username', 'password'), $table->getRow(0));
282
        $this->assertEquals(array('2', 'antono', 'pa$sword'), $table->getRow(2));
283
        $this->assertEquals(array('242', '2everzet', '2qwerty'), $table->getRow(3));
284
        $this->assertEquals(array('32', '3antono', '3pa$sword'), $table->getRow(6));
285
    }
286
287
    /**
288
     * @expectedException \Behat\Gherkin\Exception\NodeException
289
     */
290
    public function testMergeRowsFromTableWrongHeaderNameExceptionThrown()
291
    {
292
        $table = new TableNode(array(
293
            5  => array('id', 'username', 'password'),
294
            10 => array('42', 'everzet', 'qwerty'),
295
            13 => array('2', 'antono', 'pa$sword')
296
        ));
297
298
        $new = new TableNode(array(
299
            25  => array('id', 'QWE', 'password'),
300
            210 => array('242', '2everzet', '2qwerty')
301
        ));
302
303
        $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...
304
    }
305
306
    /**
307
     * @expectedException \Behat\Gherkin\Exception\NodeException
308
     */
309
    public function testGetTableFromListWithMultidimensionalArrayArgument()
310
    {
311
        TableNode::fromList(array(
312
            array(1, 2, 3),
313
            array(4, 5, 6)
314
        ));
315
    }
316
317
    /**
318
     * @expectedException \Behat\Gherkin\Exception\NodeException
319
     */
320
    public function testMergeRowsFromTableWrongHeaderOrderExceptionThrown()
321
    {
322
        $table = new TableNode(array(
323
            5  => array('id', 'username', 'password'),
324
            10 => array('42', 'everzet', 'qwerty'),
325
            13 => array('2', 'antono', 'pa$sword')
326
        ));
327
328
        $new = new TableNode(array(
329
            25  => array('id', 'password', 'username'),
330
            210 => array('242', '2everzet', '2qwerty')
331
        ));
332
333
        $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...
334
    }
335
336
    /**
337
     * @expectedException \Behat\Gherkin\Exception\NodeException
338
     */
339
    public function testMergeRowsFromTableWrongHeaderSizeExceptionThrown()
340
    {
341
        $table = new TableNode(array(
342
            5  => array('id', 'username', 'password'),
343
            10 => array('42', 'everzet', 'qwerty'),
344
            13 => array('2', 'antono', 'pa$sword')
345
        ));
346
347
        $new = new TableNode(array(
348
            25  => array('id', 'username'),
349
            210 => array('242', '2everzet')
350
        ));
351
352
        $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...
353
    }
354
}
355