ColumnTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace League\CLImate\Tests;
  3. class ColumnTest extends TestBase
  4. {
  5. protected function getSingleColumn()
  6. {
  7. return [
  8. 'this',
  9. ];
  10. }
  11. protected function getStandardColumns()
  12. {
  13. return [
  14. 'this',
  15. 'that',
  16. 'other',
  17. 'thing',
  18. 'this',
  19. 'too',
  20. 'and',
  21. 'also',
  22. 'this is much longer',
  23. ];
  24. }
  25. protected function getArrayOfArrayColumns()
  26. {
  27. return [
  28. ['one', 'first one', 'first third column'],
  29. ['two', 'second one', 'second third column'],
  30. ['three', 'third one', 'third third column'],
  31. ['four', 'fourth one', 'fourth third column'],
  32. ['five', 'fifth one', 'fifth third column'],
  33. ];
  34. }
  35. protected function getAssociativeColumns()
  36. {
  37. return [
  38. 'one' => 'first one',
  39. 'two' => 'second one',
  40. 'three' => 'third one',
  41. 'four' => 'fourth one',
  42. 'five' => 'fifth one',
  43. ];
  44. }
  45. /** @test */
  46. public function it_can_output_columns()
  47. {
  48. $this->shouldWrite("\e[mthis thing and\e[0m");
  49. $this->shouldWrite("\e[mthat this also\e[0m");
  50. $this->shouldWrite("\e[mother too this is much longer\e[0m");
  51. $this->shouldHavePersisted();
  52. $this->cli->columns($this->getStandardColumns());
  53. }
  54. /** @test */
  55. public function it_can_output_a_single_item_in_one_column()
  56. {
  57. $this->shouldWrite("\e[mthis\e[0m");
  58. $this->shouldHavePersisted();
  59. $this->cli->columns($this->getSingleColumn(), 1);
  60. }
  61. /** @test */
  62. public function it_can_output_less_items_than_columns()
  63. {
  64. $this->shouldWrite("\e[mthis\e[0m");
  65. $this->shouldHavePersisted();
  66. $this->cli->columns($this->getSingleColumn(), 2);
  67. }
  68. /** @test */
  69. public function it_can_output_a_specific_number_of_columns()
  70. {
  71. $this->shouldWrite("\e[mthis too\e[0m");
  72. $this->shouldWrite("\e[mthat and\e[0m");
  73. $this->shouldWrite("\e[mother also\e[0m");
  74. $this->shouldWrite("\e[mthing this is much longer\e[0m");
  75. $this->shouldWrite("\e[mthis\e[0m");
  76. $this->shouldWrite("\e[mthis\e[0m");
  77. $this->shouldWrite("\e[mthat\e[0m");
  78. $this->shouldWrite("\e[mother\e[0m");
  79. $this->shouldWrite("\e[mthing\e[0m");
  80. $this->shouldWrite("\e[mthis\e[0m");
  81. $this->shouldWrite("\e[mtoo\e[0m");
  82. $this->shouldWrite("\e[mand\e[0m");
  83. $this->shouldWrite("\e[malso\e[0m");
  84. $this->shouldWrite("\e[mthis is much longer\e[0m");
  85. $this->shouldHavePersisted(2);
  86. $this->cli->columns($this->getStandardColumns(), 2);
  87. $this->cli->columns($this->getStandardColumns(), 1);
  88. }
  89. /** @test */
  90. public function it_can_handle_multibyte_strings()
  91. {
  92. $this->shouldWrite("\e[mthis thing and\e[0m");
  93. $this->shouldWrite("\e[mthat this also\e[0m");
  94. $this->shouldWrite("\e[mother too this is much longerø\e[0m");
  95. $this->shouldHavePersisted();
  96. $columns = $this->getStandardColumns();
  97. $columns[8] = $columns[8] . 'ø';
  98. $this->cli->columns($columns);
  99. }
  100. /** @test */
  101. public function it_can_output_an_associative_array_as_columns()
  102. {
  103. $this->shouldWrite("\e[mone first one\e[0m");
  104. $this->shouldWrite("\e[mtwo second one\e[0m");
  105. $this->shouldWrite("\e[mthree third one\e[0m");
  106. $this->shouldWrite("\e[mfour fourth one\e[0m");
  107. $this->shouldWrite("\e[mfive fifth one\e[0m");
  108. $this->shouldHavePersisted();
  109. $this->cli->columns($this->getAssociativeColumns());
  110. }
  111. /** @test */
  112. public function it_can_output_an_array_of_arrays_as_columns()
  113. {
  114. $this->shouldWrite("\e[mone first one first third column\e[0m");
  115. $this->shouldWrite("\e[mtwo second one second third column\e[0m");
  116. $this->shouldWrite("\e[mthree third one third third column\e[0m");
  117. $this->shouldWrite("\e[mfour fourth one fourth third column\e[0m");
  118. $this->shouldWrite("\e[mfive fifth one fifth third column\e[0m");
  119. $this->shouldHavePersisted();
  120. $this->cli->columns($this->getArrayOfArrayColumns());
  121. }
  122. /** @test */
  123. public function it_can_output_an_uneven_array_of_arrays_as_columns()
  124. {
  125. $this->shouldWrite("\e[mone first one first third column\e[0m");
  126. $this->shouldWrite("\e[mtwo second one second third column\e[0m");
  127. $this->shouldWrite("\e[mthree third one third third column\e[0m");
  128. $this->shouldWrite("\e[mfour fourth one fourth third column also this one\e[0m");
  129. $this->shouldWrite("\e[mfive fifth one fifth third column\e[0m");
  130. $this->shouldHavePersisted();
  131. $columns = $this->getArrayOfArrayColumns();
  132. $columns[3][] = 'also this one';
  133. $this->cli->columns($columns);
  134. }
  135. }