TableTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace League\CLImate\Tests;
  3. class TableTest extends TestBase
  4. {
  5. /** @test */
  6. public function it_can_output_a_basic_table()
  7. {
  8. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  9. $this->shouldWrite("\e[m| Cell 1 | Cell 2 | Cell 3 | Cell 4 |\e[0m");
  10. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  11. $this->shouldHavePersisted();
  12. $this->cli->table([
  13. [
  14. 'Cell 1',
  15. 'Cell 2',
  16. 'Cell 3',
  17. 'Cell 4',
  18. ],
  19. ]);
  20. }
  21. /** @test */
  22. public function it_can_output_an_array_of_objects_table()
  23. {
  24. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  25. $this->shouldWrite("\e[m| cell1 | cell2 | cell3 | cell4 |\e[0m");
  26. $this->shouldWrite("\e[m=====================================\e[0m");
  27. $this->shouldWrite("\e[m| Cell 1 | Cell 2 | Cell 3 | Cell 4 |\e[0m");
  28. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  29. $this->shouldHavePersisted();
  30. $this->cli->table([
  31. (object) [
  32. 'cell1' => 'Cell 1',
  33. 'cell2' => 'Cell 2',
  34. 'cell3' => 'Cell 3',
  35. 'cell4' => 'Cell 4',
  36. ],
  37. ]);
  38. }
  39. /** @test */
  40. public function it_can_output_an_array_of_associative_arrays_table()
  41. {
  42. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  43. $this->shouldWrite("\e[m| cell1 | cell2 | cell3 | cell4 |\e[0m");
  44. $this->shouldWrite("\e[m=====================================\e[0m");
  45. $this->shouldWrite("\e[m| Cell 1 | Cell 2 | Cell 3 | Cell 4 |\e[0m");
  46. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  47. $this->shouldHavePersisted();
  48. $this->cli->table([
  49. [
  50. 'cell1' => 'Cell 1',
  51. 'cell2' => 'Cell 2',
  52. 'cell3' => 'Cell 3',
  53. 'cell4' => 'Cell 4',
  54. ],
  55. ]);
  56. }
  57. /** @test */
  58. public function it_can_persist_a_style_on_the_table()
  59. {
  60. $this->shouldWrite("\e[31m-------------------------------------\e[0m");
  61. $this->shouldWrite("\e[31m| cell1 | cell2 | cell3 | cell4 |\e[0m");
  62. $this->shouldWrite("\e[31m=====================================\e[0m");
  63. $this->shouldWrite("\e[31m| Cell 1 | Cell 2 | Cell 3 | Cell 4 |\e[0m");
  64. $this->shouldWrite("\e[31m-------------------------------------\e[0m");
  65. $this->shouldHavePersisted();
  66. $this->cli->redTable([
  67. [
  68. 'cell1' => 'Cell 1',
  69. 'cell2' => 'Cell 2',
  70. 'cell3' => 'Cell 3',
  71. 'cell4' => 'Cell 4',
  72. ],
  73. ]);
  74. }
  75. /** @test */
  76. public function it_can_handle_tags_within_the_data()
  77. {
  78. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  79. $this->shouldWrite("\e[m| cell1 | cell2 | cell3 | cell4 |\e[0m");
  80. $this->shouldWrite("\e[m=====================================\e[0m");
  81. $this->shouldWrite("\e[m| Cell \e[31m1\e[0m | Cell 2 | Cell 3 | Cell 4 |\e[0m");
  82. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  83. $this->shouldHavePersisted();
  84. $this->cli->table([
  85. [
  86. 'cell1' => 'Cell <red>1</red>',
  87. 'cell2' => 'Cell 2',
  88. 'cell3' => 'Cell 3',
  89. 'cell4' => 'Cell 4',
  90. ],
  91. ]);
  92. }
  93. /** @test */
  94. public function it_can_handle_multi_byte_characters()
  95. {
  96. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  97. $this->shouldWrite("\e[m| cell1 | cell2 | cell3 | cell4 |\e[0m");
  98. $this->shouldWrite("\e[m=====================================\e[0m");
  99. $this->shouldWrite("\e[m| Cell Ω | Cell 2 | Cell 3 | Cell 4 |\e[0m");
  100. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  101. $this->shouldHavePersisted();
  102. $this->cli->table([
  103. [
  104. 'cell1' => 'Cell Ω',
  105. 'cell2' => 'Cell 2',
  106. 'cell3' => 'Cell 3',
  107. 'cell4' => 'Cell 4',
  108. ],
  109. ]);
  110. }
  111. /** @test */
  112. public function it_can_handle_the_same_value_more_than_once()
  113. {
  114. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  115. $this->shouldWrite("\e[m| cell1 | cell2 | cell3 | cell4 |\e[0m");
  116. $this->shouldWrite("\e[m=====================================\e[0m");
  117. $this->shouldWrite("\e[m| Cell 1 | Cell 2 | Cell 3 | Cell 3 |\e[0m");
  118. $this->shouldWrite("\e[m-------------------------------------\e[0m");
  119. $this->shouldHavePersisted();
  120. $this->cli->table([
  121. [
  122. 'cell1' => 'Cell 1',
  123. 'cell2' => 'Cell 2',
  124. 'cell3' => 'Cell 3',
  125. 'cell4' => 'Cell 3',
  126. ],
  127. ]);
  128. }
  129. public function testTableWithPrefix1()
  130. {
  131. $this->shouldWrite("\e[m\t-----------\e[0m");
  132. $this->shouldWrite("\e[m\t| Field 1 |\e[0m");
  133. $this->shouldWrite("\e[m\t===========\e[0m");
  134. $this->shouldWrite("\e[m\t| Value 1 |\e[0m");
  135. $this->shouldWrite("\e[m\t-----------\e[0m");
  136. $this->shouldHavePersisted();
  137. $this->cli->table([
  138. ["Field 1" => "Value 1"],
  139. ], "\t");
  140. }
  141. }