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
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @expectedException \Behat\Gherkin\Exception\NodeException |
260
|
|
|
*/ |
261
|
|
|
public function testGetTableFromListWithMultidimensionalArrayArgument() |
262
|
|
|
{ |
263
|
|
|
TableNode::fromList(array( |
264
|
|
|
array(1, 2, 3), |
265
|
|
|
array(4, 5, 6) |
266
|
|
|
)); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
} |
270
|
|
|
|