TableNodeTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace Tests\Behat\Gherkin\Node;
  3. use Behat\Gherkin\Node\TableNode;
  4. class TableNodeTest extends \PHPUnit_Framework_TestCase
  5. {
  6. /**
  7. * @expectedException \Behat\Gherkin\Exception\NodeException
  8. */
  9. public function testConstructorExpectsSameNumberOfColumnsInEachRow()
  10. {
  11. new TableNode(array(
  12. array('username', 'password'),
  13. array('everzet'),
  14. array('antono', 'pa$sword')
  15. ));
  16. }
  17. public function constructorTestDataProvider() {
  18. return array(
  19. 'One-dimensional array' => array(
  20. array('everzet', 'antono')
  21. ),
  22. 'Three-dimensional array' => array(
  23. array(array(array('everzet', 'antono')))
  24. )
  25. );
  26. }
  27. /**
  28. * @dataProvider constructorTestDataProvider
  29. * @expectedException \Behat\Gherkin\Exception\NodeException
  30. * @expectedExceptionMessage Table is not two-dimensional.
  31. */
  32. public function testConstructorExpectsTwoDimensionalArrays($table)
  33. {
  34. new TableNode($table);
  35. }
  36. public function testHashTable()
  37. {
  38. $table = new TableNode(array(
  39. array('username', 'password'),
  40. array('everzet', 'qwerty'),
  41. array('antono', 'pa$sword')
  42. ));
  43. $this->assertEquals(
  44. array(
  45. array('username' => 'everzet', 'password' => 'qwerty')
  46. , array('username' => 'antono', 'password' => 'pa$sword')
  47. ),
  48. $table->getHash()
  49. );
  50. $table = new TableNode(array(
  51. array('username', 'password'),
  52. array('', 'qwerty'),
  53. array('antono', ''),
  54. array('', '')
  55. ));
  56. $this->assertEquals(
  57. array(
  58. array('username' => '', 'password' => 'qwerty'),
  59. array('username' => 'antono', 'password' => ''),
  60. array('username' => '', 'password' => ''),
  61. ),
  62. $table->getHash()
  63. );
  64. }
  65. public function testIterator()
  66. {
  67. $table = new TableNode(array(
  68. array('username', 'password'),
  69. array('', 'qwerty'),
  70. array('antono', ''),
  71. array('', ''),
  72. ));
  73. $this->assertEquals(
  74. array(
  75. array('username' => '', 'password' => 'qwerty'),
  76. array('username' => 'antono', 'password' => ''),
  77. array('username' => '', 'password' => ''),
  78. ),
  79. iterator_to_array($table)
  80. );
  81. }
  82. public function testRowsHashTable()
  83. {
  84. $table = new TableNode(array(
  85. array('username', 'everzet'),
  86. array('password', 'qwerty'),
  87. array('uid', '35'),
  88. ));
  89. $this->assertEquals(
  90. array('username' => 'everzet', 'password' => 'qwerty', 'uid' => '35'),
  91. $table->getRowsHash()
  92. );
  93. }
  94. public function testLongRowsHashTable()
  95. {
  96. $table = new TableNode(array(
  97. array('username', 'everzet', 'marcello'),
  98. array('password', 'qwerty', '12345'),
  99. array('uid', '35', '22')
  100. ));
  101. $this->assertEquals(array(
  102. 'username' => array('everzet', 'marcello'),
  103. 'password' => array('qwerty', '12345'),
  104. 'uid' => array('35', '22')
  105. ), $table->getRowsHash());
  106. }
  107. public function testGetRows()
  108. {
  109. $table = new TableNode(array(
  110. array('username', 'password'),
  111. array('everzet', 'qwerty'),
  112. array('antono', 'pa$sword')
  113. ));
  114. $this->assertEquals(array(
  115. array('username', 'password'),
  116. array('everzet', 'qwerty'),
  117. array('antono', 'pa$sword')
  118. ), $table->getRows());
  119. }
  120. public function testGetLines()
  121. {
  122. $table = new TableNode(array(
  123. 5 => array('username', 'password'),
  124. 10 => array('everzet', 'qwerty'),
  125. 13 => array('antono', 'pa$sword')
  126. ));
  127. $this->assertEquals(array(5, 10, 13), $table->getLines());
  128. }
  129. public function testGetRow()
  130. {
  131. $table = new TableNode(array(
  132. array('username', 'password'),
  133. array('everzet', 'qwerty'),
  134. array('antono', 'pa$sword')
  135. ));
  136. $this->assertEquals(array('username', 'password'), $table->getRow(0));
  137. $this->assertEquals(array('antono', 'pa$sword'), $table->getRow(2));
  138. }
  139. public function testGetColumn()
  140. {
  141. $table = new TableNode(array(
  142. array('username', 'password'),
  143. array('everzet', 'qwerty'),
  144. array('antono', 'pa$sword')
  145. ));
  146. $this->assertEquals(array('username', 'everzet', 'antono'), $table->getColumn(0));
  147. $this->assertEquals(array('password', 'qwerty', 'pa$sword'), $table->getColumn(1));
  148. $table = new TableNode(array(
  149. array('username'),
  150. array('everzet'),
  151. array('antono')
  152. ));
  153. $this->assertEquals(array('username', 'everzet', 'antono'), $table->getColumn(0));
  154. }
  155. public function testGetRowWithLineNumbers()
  156. {
  157. $table = new TableNode(array(
  158. 5 => array('username', 'password'),
  159. 10 => array('everzet', 'qwerty'),
  160. 13 => array('antono', 'pa$sword')
  161. ));
  162. $this->assertEquals(array('username', 'password'), $table->getRow(0));
  163. $this->assertEquals(array('antono', 'pa$sword'), $table->getRow(2));
  164. }
  165. public function testGetTable()
  166. {
  167. $table = new TableNode($a = array(
  168. 5 => array('username', 'password'),
  169. 10 => array('everzet', 'qwerty'),
  170. 13 => array('antono', 'pa$sword')
  171. ));
  172. $this->assertEquals($a, $table->getTable());
  173. }
  174. public function testGetRowLine()
  175. {
  176. $table = new TableNode(array(
  177. 5 => array('username', 'password'),
  178. 10 => array('everzet', 'qwerty'),
  179. 13 => array('antono', 'pa$sword')
  180. ));
  181. $this->assertEquals(5, $table->getRowLine(0));
  182. $this->assertEquals(13, $table->getRowLine(2));
  183. }
  184. public function testGetRowAsString()
  185. {
  186. $table = new TableNode(array(
  187. 5 => array('username', 'password'),
  188. 10 => array('everzet', 'qwerty'),
  189. 13 => array('antono', 'pa$sword')
  190. ));
  191. $this->assertEquals('| username | password |', $table->getRowAsString(0));
  192. $this->assertEquals('| antono | pa$sword |', $table->getRowAsString(2));
  193. }
  194. public function testGetTableAsString()
  195. {
  196. $table = new TableNode(array(
  197. 5 => array('id', 'username', 'password'),
  198. 10 => array('42', 'everzet', 'qwerty'),
  199. 13 => array('2', 'antono', 'pa$sword')
  200. ));
  201. $expected = <<<TABLE
  202. | id | username | password |
  203. | 42 | everzet | qwerty |
  204. | 2 | antono | pa\$sword |
  205. TABLE;
  206. $this->assertEquals($expected, $table->getTableAsString());
  207. }
  208. public function testFromList()
  209. {
  210. $table = TableNode::fromList(array(
  211. 'everzet',
  212. 'antono'
  213. ));
  214. $expected = new TableNode(array(
  215. array('everzet'),
  216. array('antono'),
  217. ));
  218. $this->assertEquals($expected, $table);
  219. }
  220. /**
  221. * @expectedException \Behat\Gherkin\Exception\NodeException
  222. */
  223. public function testGetTableFromListWithMultidimensionalArrayArgument()
  224. {
  225. TableNode::fromList(array(
  226. array(1, 2, 3),
  227. array(4, 5, 6)
  228. ));
  229. }
  230. }