tablesUnitTest.php

  1. 8.0.x tests/tablesUnitTest.php
  2. 6.x tests/tablesUnitTest.php
  3. 7.x tests/tablesUnitTest.php
  4. master tests/tablesUnitTest.php

Namespace

Unish

Classes

Namesort descending Description
tablesUnitTest Unit tests for table formatting.

File

tests/tablesUnitTest.php
View source
  1. <?php
  2. namespace Unish;
  3. /**
  4. * Unit tests for table formatting.
  5. *
  6. * @group base
  7. */
  8. class tablesUnitTest extends UnitUnishTestCase {
  9. function setUp() {
  10. // Bootstrap to ensure the auto-loaded is running so that Console_Table is found.
  11. drush_preflight();
  12. $this->original_columns = drush_get_context('DRUSH_COLUMNS');
  13. // Some table data we reuse between tests.
  14. $this->numbers = array(
  15. array('1', '12', '123'),
  16. array('1234', '12345', '123456'),
  17. array('1234567', '12345678', '123456789'),
  18. );
  19. $this->words = array(
  20. array('Drush is a command line shell', 'scripting interface', 'for Drupal'),
  21. array('A veritable', 'Swiss Army knife', 'designed to make life easier for us'),
  22. );
  23. }
  24. function tearDown() {
  25. drush_set_context('DRUSH_COLUMNS', $this->original_columns);
  26. }
  27. /**
  28. * Tests drush_format_table() at various table widths with automatic column
  29. * sizing.
  30. *
  31. * @see drush_format_table().
  32. */
  33. public function testFormatAutoWidths() {
  34. // print "\n'" . str_replace("\n", "' . PHP_EOL . '", $output) . "'\n";
  35. drush_set_context('DRUSH_COLUMNS', 16);
  36. $output = drush_format_table($this->numbers);
  37. $expected = ' 1 12 123 ' . PHP_EOL . ' 123 123 1234 ' . PHP_EOL . ' 4 45 56 ' . PHP_EOL . ' 123 123 1234 ' . PHP_EOL . ' 456 456 5678 ' . PHP_EOL . ' 7 78 9 ' . PHP_EOL;
  38. $this->assertEquals($expected, $output);
  39. drush_set_context('DRUSH_COLUMNS', 22);
  40. $output = drush_format_table($this->numbers);
  41. $expected = ' 1 12 123 ' . PHP_EOL . ' 1234 12345 123456 ' . PHP_EOL . ' 12345 12345 123456 ' . PHP_EOL . ' 67 678 789 ' . PHP_EOL;
  42. $this->assertEquals($expected, $output);
  43. drush_set_context('DRUSH_COLUMNS', 24);
  44. $output = drush_format_table($this->numbers);
  45. $expected = ' 1 12 123 ' . PHP_EOL . ' 1234 12345 123456 ' . PHP_EOL . ' 123456 123456 123456 ' . PHP_EOL . ' 7 78 789 ' . PHP_EOL;
  46. $this->assertEquals($expected, $output);
  47. drush_set_context('DRUSH_COLUMNS', 80);
  48. $output = drush_format_table($this->numbers);
  49. $expected = ' 1 12 123 ' . PHP_EOL . ' 1234 12345 123456 ' . PHP_EOL . ' 1234567 12345678 123456789 ' . PHP_EOL;
  50. $this->assertEquals($expected, $output);
  51. }
  52. /**
  53. * Tests drush_format_table() at various table widths.
  54. *
  55. * @see drush_format_table().
  56. */
  57. public function testFormatWidths() {
  58. // print "\n'" . str_replace("\n", "' . PHP_EOL . '", $output) . "'\n";
  59. drush_set_context('DRUSH_COLUMNS', 22);
  60. $output = drush_format_table($this->numbers, FALSE, array(2));
  61. $expected = ' 1 12 123 ' . PHP_EOL . ' 12 12345 123456 ' . PHP_EOL . ' 34 ' . PHP_EOL . ' 12 1234567 1234567 ' . PHP_EOL . ' 34 8 89 ' . PHP_EOL . ' 56 ' . PHP_EOL . ' 7 ' . PHP_EOL;
  62. $this->assertEquals($expected, $output);
  63. $output = drush_format_table($this->numbers, FALSE, array(10));
  64. $expected = ' 1 12 123 ' . PHP_EOL . ' 1234 123 123 ' . PHP_EOL . ' 45 456 ' . PHP_EOL . ' 1234567 123 123 ' . PHP_EOL . ' 456 456 ' . PHP_EOL . ' 78 789 ' . PHP_EOL;
  65. $this->assertEquals($expected, $output);
  66. $output = drush_format_table($this->numbers, FALSE, array(2, 2));
  67. $expected = ' 1 12 123 ' . PHP_EOL . ' 12 12 123456 ' . PHP_EOL . ' 34 34 ' . PHP_EOL . ' 5 ' . PHP_EOL . ' 12 12 123456789 ' . PHP_EOL . ' 34 34 ' . PHP_EOL . ' 56 56 ' . PHP_EOL . ' 7 78 ' . PHP_EOL;
  68. $this->assertEquals($expected, $output);
  69. $output = drush_format_table($this->numbers, FALSE, array(4, 4, 4));
  70. $expected = ' 1 12 123 ' . PHP_EOL . ' 1234 1234 1234 ' . PHP_EOL . ' 5 56 ' . PHP_EOL . ' 1234 1234 1234 ' . PHP_EOL . ' 567 5678 5678 ' . PHP_EOL . ' 9 ' . PHP_EOL;
  71. $this->assertEquals($expected, $output);
  72. }
  73. /**
  74. * Tests drush_format_table() with a header.
  75. *
  76. * @see drush_format_table().
  77. */
  78. public function testFormatTableHeader() {
  79. drush_set_context('DRUSH_COLUMNS', 16);
  80. $rows = $this->numbers;
  81. array_unshift($rows, array('A', 'B', 'C'));
  82. $output = drush_format_table($rows, TRUE);
  83. $expected = ' A B C ' . PHP_EOL . ' 1 12 123 ' . PHP_EOL . ' 123 123 1234 ' . PHP_EOL . ' 4 45 56 ' . PHP_EOL . ' 123 123 1234 ' . PHP_EOL . ' 456 456 5678 ' . PHP_EOL . ' 7 78 9 ' . PHP_EOL;
  84. $this->assertEquals($expected, $output);
  85. }
  86. /**
  87. * Tests drush_format_table() with word wrapping.
  88. *
  89. * @see drush_format_table().
  90. */
  91. public function testFormatTableWordWrap() {
  92. drush_set_context('DRUSH_COLUMNS', 60);
  93. $output = drush_format_table($this->words);
  94. $expected = ' Drush is a command scripting for Drupal ' . PHP_EOL . ' line shell interface ' . PHP_EOL . ' A veritable Swiss Army knife designed to make ' . PHP_EOL . ' life easier for us ' . PHP_EOL;
  95. $this->assertEquals($expected, $output);
  96. }
  97. }