TableTest.php 8.5 KB

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