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