Completed
Pull Request — master (#168)
by
unknown
04:20
created

testConstructorExpectsScalarCellValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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