TableTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace League\CLImate\Tests\TerminalObject\Basic;
  3. use League\CLImate\Tests\TestBase;
  4. use League\CLImate\Exceptions\InvalidArgumentException;
  5. class TableTest extends TestBase
  6. {
  7. /**
  8. * @test
  9. * @doesNotPerformAssertions
  10. */
  11. public function it_can_output_a_basic_table()
  12. {
  13. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  14. $this->shouldWrite("\e[m| Cell 1 | Cell 2 | Cell 3 | Cell 4 |\e[0m");
  15. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  16. $this->shouldHavePersisted();
  17. $this->cli->table([
  18. [
  19. 'Cell 1',
  20. 'Cell 2',
  21. 'Cell 3',
  22. 'Cell 4',
  23. ],
  24. ]);
  25. }
  26. /**
  27. * @test
  28. * @doesNotPerformAssertions
  29. */
  30. public function it_can_output_an_array_of_objects_table()
  31. {
  32. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  33. $this->shouldWrite("\e[m| cell1 | cell2 | cell3 | cell4 |\e[0m");
  34. $this->shouldWrite("\e[m=====================================\e[0m");
  35. $this->shouldWrite("\e[m| Cell 1 | Cell 2 | Cell 3 | Cell 4 |\e[0m");
  36. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  37. $this->shouldHavePersisted();
  38. $this->cli->table([
  39. (object) [
  40. 'cell1' => 'Cell 1',
  41. 'cell2' => 'Cell 2',
  42. 'cell3' => 'Cell 3',
  43. 'cell4' => 'Cell 4',
  44. ],
  45. ]);
  46. }
  47. /**
  48. * @test
  49. * @doesNotPerformAssertions
  50. */
  51. public function it_can_output_an_array_of_associative_arrays_table()
  52. {
  53. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  54. $this->shouldWrite("\e[m| cell1 | cell2 | cell3 | cell4 |\e[0m");
  55. $this->shouldWrite("\e[m=====================================\e[0m");
  56. $this->shouldWrite("\e[m| Cell 1 | Cell 2 | Cell 3 | Cell 4 |\e[0m");
  57. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  58. $this->shouldHavePersisted();
  59. $this->cli->table([
  60. [
  61. 'cell1' => 'Cell 1',
  62. 'cell2' => 'Cell 2',
  63. 'cell3' => 'Cell 3',
  64. 'cell4' => 'Cell 4',
  65. ],
  66. ]);
  67. }
  68. /**
  69. * @test
  70. * @doesNotPerformAssertions
  71. */
  72. public function it_can_persist_a_style_on_the_table()
  73. {
  74. $this->shouldWrite("\e[31m-------------------------------------\e[0m");
  75. $this->shouldWrite("\e[31m| cell1 | cell2 | cell3 | cell4 |\e[0m");
  76. $this->shouldWrite("\e[31m=====================================\e[0m");
  77. $this->shouldWrite("\e[31m| Cell 1 | Cell 2 | Cell 3 | Cell 4 |\e[0m");
  78. $this->shouldWrite("\e[31m-------------------------------------\e[0m");
  79. $this->shouldHavePersisted();
  80. $this->cli->redTable([
  81. [
  82. 'cell1' => 'Cell 1',
  83. 'cell2' => 'Cell 2',
  84. 'cell3' => 'Cell 3',
  85. 'cell4' => 'Cell 4',
  86. ],
  87. ]);
  88. }
  89. /**
  90. * @test
  91. * @doesNotPerformAssertions
  92. */
  93. public function it_can_handle_tags_within_the_data()
  94. {
  95. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  96. $this->shouldWrite("\e[m| cell1 | cell2 | cell3 | cell4 |\e[0m");
  97. $this->shouldWrite("\e[m=====================================\e[0m");
  98. $this->shouldWrite("\e[m| Cell \e[31m1\e[0m | Cell 2 | Cell 3 | Cell 4 |\e[0m");
  99. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  100. $this->shouldHavePersisted();
  101. $this->cli->table([
  102. [
  103. 'cell1' => 'Cell <red>1</red>',
  104. 'cell2' => 'Cell 2',
  105. 'cell3' => 'Cell 3',
  106. 'cell4' => 'Cell 4',
  107. ],
  108. ]);
  109. }
  110. /**
  111. * @test
  112. * @doesNotPerformAssertions
  113. */
  114. public function it_can_handle_multi_byte_characters()
  115. {
  116. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  117. $this->shouldWrite("\e[m| cell1 | cell2 | cell3 | cell4 |\e[0m");
  118. $this->shouldWrite("\e[m=====================================\e[0m");
  119. $this->shouldWrite("\e[m| Cell Ω | Cell 2 | Cell 3 | Cell 4 |\e[0m");
  120. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  121. $this->shouldHavePersisted();
  122. $this->cli->table([
  123. [
  124. 'cell1' => 'Cell Ω',
  125. 'cell2' => 'Cell 2',
  126. 'cell3' => 'Cell 3',
  127. 'cell4' => 'Cell 4',
  128. ],
  129. ]);
  130. }
  131. /**
  132. * @test
  133. * @doesNotPerformAssertions
  134. */
  135. public function it_can_handle_the_same_value_more_than_once()
  136. {
  137. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  138. $this->shouldWrite("\e[m| cell1 | cell2 | cell3 | cell4 |\e[0m");
  139. $this->shouldWrite("\e[m=====================================\e[0m");
  140. $this->shouldWrite("\e[m| Cell 1 | Cell 2 | Cell 3 | Cell 3 |\e[0m");
  141. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  142. $this->shouldHavePersisted();
  143. $this->cli->table([
  144. [
  145. 'cell1' => 'Cell 1',
  146. 'cell2' => 'Cell 2',
  147. 'cell3' => 'Cell 3',
  148. 'cell4' => 'Cell 3',
  149. ],
  150. ]);
  151. }
  152. /**
  153. * @doesNotPerformAssertions
  154. */
  155. public function testTableWithPrefix1()
  156. {
  157. $this->shouldWrite("\e[m\t-----------\e[0m");
  158. $this->shouldWrite("\e[m\t| Field 1 |\e[0m");
  159. $this->shouldWrite("\e[m\t===========\e[0m");
  160. $this->shouldWrite("\e[m\t| Value 1 |\e[0m");
  161. $this->shouldWrite("\e[m\t-----------\e[0m");
  162. $this->shouldHavePersisted();
  163. $this->cli->table([
  164. ["Field 1" => "Value 1"],
  165. ], "\t");
  166. }
  167. /**
  168. * Ensure a sensible error is thrown when invalid items are passed.
  169. */
  170. public function testInvalidData1()
  171. {
  172. $this->expectException(InvalidArgumentException::class);
  173. $this->expectExceptionMessage("Invalid table data, you must pass an array of arrays or objects");
  174. $this->cli->table([
  175. "whoops",
  176. ]);
  177. }
  178. /**
  179. * @doesNotPerformAssertions
  180. */
  181. public function testTableWithNewline()
  182. {
  183. $this->shouldWrite("\e[m-----------------------------------\e[0m");
  184. $this->shouldWrite("\e[m| Cell 1 | Cell | Cell 3 | Cell 4 |\e[0m");
  185. $this->shouldWrite("\e[m| | 2 | | |\e[0m");
  186. $this->shouldWrite("\e[m-----------------------------------\e[0m");
  187. $this->shouldHavePersisted();
  188. $this->cli->table([
  189. [
  190. 'Cell 1',
  191. "Cell\n2",
  192. 'Cell 3',
  193. 'Cell 4',
  194. ],
  195. ]);
  196. }
  197. /**
  198. * @doesNotPerformAssertions
  199. */
  200. public function testTableWithNewlineAndObjects()
  201. {
  202. $this->shouldWrite("\e[m------------------------------------\e[0m");
  203. $this->shouldWrite("\e[m| cell1 | cell2 | cell3 | cell4 |\e[0m");
  204. $this->shouldWrite("\e[m====================================\e[0m");
  205. $this->shouldWrite("\e[m| Cell 1 | Cell | Cell 3 | Cell 4 |\e[0m");
  206. $this->shouldWrite("\e[m| | 2 | | |\e[0m");
  207. $this->shouldWrite("\e[m------------------------------------\e[0m");
  208. $this->shouldHavePersisted();
  209. $this->cli->table([
  210. (object) [
  211. 'cell1' => 'Cell 1',
  212. 'cell2' => "Cell\n2",
  213. 'cell3' => 'Cell 3',
  214. 'cell4' => 'Cell 4',
  215. ],
  216. ]);
  217. }
  218. /**
  219. * @doesNotPerformAssertions
  220. */
  221. public function testTableWithMultipleNewlines()
  222. {
  223. $this->shouldWrite("\e[m---------------------------------\e[0m");
  224. $this->shouldWrite("\e[m| Cell 1 | Cell | Cell | Cell |\e[0m");
  225. $this->shouldWrite("\e[m| | 2 | 3 | 4 |\e[0m");
  226. $this->shouldWrite("\e[m| | | Cell 3 | Cell |\e[0m");
  227. $this->shouldWrite("\e[m| | | | 4 |\e[0m");
  228. $this->shouldWrite("\e[m---------------------------------\e[0m");
  229. $this->shouldHavePersisted();
  230. $this->cli->table([
  231. [
  232. 'Cell 1',
  233. "Cell\n2",
  234. "Cell\n3\nCell 3",
  235. "Cell\n4\nCell\n4",
  236. ],
  237. ]);
  238. }
  239. /**
  240. * @doesNotPerformAssertions
  241. */
  242. public function testTableWithNullAndEmptyValues()
  243. {
  244. $this->shouldWrite("\e[m------------------\e[0m");
  245. $this->shouldWrite("\e[m| Cell 1 | | |\e[0m");
  246. $this->shouldWrite("\e[m------------------\e[0m");
  247. $this->shouldHavePersisted();
  248. $this->cli->table([
  249. [
  250. 'Cell 1',
  251. null,
  252. ''
  253. ],
  254. ]);
  255. }
  256. }