|
| 1 | +<?php |
| 2 | + |
| 3 | +use cli\Streams; |
| 4 | +use cli\Table; |
| 5 | +use cli\table\Ascii; |
| 6 | +use cli\Colors; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class Test_Table_Ascii |
| 10 | + * |
| 11 | + * Acceptance tests for ASCII table drawing. |
| 12 | + * It will redirect STDOUT to temporary file and check that output matches with expected |
| 13 | + */ |
| 14 | +class Test_Table_Ascii extends PHPUnit_Framework_TestCase { |
| 15 | + /** |
| 16 | + * @var string Path to temporary file, where STDOUT output will be redirected during tests |
| 17 | + */ |
| 18 | + private $_mockFile; |
| 19 | + /** |
| 20 | + * @var \cli\Table Instance |
| 21 | + */ |
| 22 | + private $_instance; |
| 23 | + |
| 24 | + /** |
| 25 | + * Creates instance and redirects STDOUT to temporary file |
| 26 | + */ |
| 27 | + public function setUp() { |
| 28 | + $this->_mockFile = tempnam(sys_get_temp_dir(), 'temp'); |
| 29 | + $resource = fopen($this->_mockFile, 'wb'); |
| 30 | + Streams::setStream('out', $resource); |
| 31 | + |
| 32 | + $this->_instance = new Table(); |
| 33 | + $this->_instance->setRenderer(new Ascii()); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Cleans temporary file |
| 38 | + */ |
| 39 | + public function tearDown() { |
| 40 | + if (file_exists($this->_mockFile)) { |
| 41 | + unlink($this->_mockFile); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Draw simple One column table |
| 47 | + */ |
| 48 | + public function testDrawOneColumnTable() { |
| 49 | + $headers = array('Test Header'); |
| 50 | + $rows = array( |
| 51 | + array('x'), |
| 52 | + ); |
| 53 | + $output = <<<'OUT' |
| 54 | ++-------------+ |
| 55 | +| Test Header | |
| 56 | ++-------------+ |
| 57 | +| x | |
| 58 | ++-------------+ |
| 59 | + |
| 60 | +OUT; |
| 61 | + $this->assertInOutEquals(array($headers, $rows), $output); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Checks that spacing and borders are handled correctly in table |
| 66 | + */ |
| 67 | + public function testSpacingInTable() { |
| 68 | + $headers = array('A', ' ', 'C', ''); |
| 69 | + $rows = array( |
| 70 | + array(' ', 'B1', '', 'D1'), |
| 71 | + array('A2', '', ' C2', null), |
| 72 | + ); |
| 73 | + $output = <<<'OUT' |
| 74 | ++-------+------+-----+----+ |
| 75 | +| A | | C | | |
| 76 | ++-------+------+-----+----+ |
| 77 | +| | B1 | | D1 | |
| 78 | +| A2 | | C2 | | |
| 79 | ++-------+------+-----+----+ |
| 80 | + |
| 81 | +OUT; |
| 82 | + $this->assertInOutEquals(array($headers, $rows), $output); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Test correct table indentation and border positions for multibyte strings |
| 87 | + */ |
| 88 | + public function testTableWithMultibyteStrings() { |
| 89 | + $headers = array('German', 'French', 'Russian', 'Chinese'); |
| 90 | + $rows = array( |
| 91 | + array('Schätzen', 'Apprécier', 'Оценить', '欣賞'), |
| 92 | + ); |
| 93 | + $output = <<<'OUT' |
| 94 | ++----------+-----------+---------+---------+ |
| 95 | +| German | French | Russian | Chinese | |
| 96 | ++----------+-----------+---------+---------+ |
| 97 | +| Schätzen | Apprécier | Оценить | 欣賞 | |
| 98 | ++----------+-----------+---------+---------+ |
| 99 | + |
| 100 | +OUT; |
| 101 | + $this->assertInOutEquals(array($headers, $rows), $output); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Draw wide multiplication Table. |
| 106 | + * Example with many columns, many rows |
| 107 | + */ |
| 108 | + public function testDrawMultiplicationTable() { |
| 109 | + $maxFactor = 16; |
| 110 | + $headers = array_merge(array('x'), range(1, $maxFactor)); |
| 111 | + for ($i = 1, $rows = array(); $i <= $maxFactor; ++$i) { |
| 112 | + $rows[] = array_merge(array($i), range($i, $i * $maxFactor, $i)); |
| 113 | + } |
| 114 | + |
| 115 | + $output = <<<'OUT' |
| 116 | ++----+----+----+----+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ |
| 117 | +| x | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | |
| 118 | ++----+----+----+----+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ |
| 119 | +| 1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | |
| 120 | +| 2 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | |
| 121 | +| 3 | 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 | 33 | 36 | 39 | 42 | 45 | 48 | |
| 122 | +| 4 | 4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 | 40 | 44 | 48 | 52 | 56 | 60 | 64 | |
| 123 | +| 5 | 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | |
| 124 | +| 6 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60 | 66 | 72 | 78 | 84 | 90 | 96 | |
| 125 | +| 7 | 7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 | 70 | 77 | 84 | 91 | 98 | 105 | 112 | |
| 126 | +| 8 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 | 88 | 96 | 104 | 112 | 120 | 128 | |
| 127 | +| 9 | 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | 90 | 99 | 108 | 117 | 126 | 135 | 144 | |
| 128 | +| 10 | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | 110 | 120 | 130 | 140 | 150 | 160 | |
| 129 | +| 11 | 11 | 22 | 33 | 44 | 55 | 66 | 77 | 88 | 99 | 110 | 121 | 132 | 143 | 154 | 165 | 176 | |
| 130 | +| 12 | 12 | 24 | 36 | 48 | 60 | 72 | 84 | 96 | 108 | 120 | 132 | 144 | 156 | 168 | 180 | 192 | |
| 131 | +| 13 | 13 | 26 | 39 | 52 | 65 | 78 | 91 | 104 | 117 | 130 | 143 | 156 | 169 | 182 | 195 | 208 | |
| 132 | +| 14 | 14 | 28 | 42 | 56 | 70 | 84 | 98 | 112 | 126 | 140 | 154 | 168 | 182 | 196 | 210 | 224 | |
| 133 | +| 15 | 15 | 30 | 45 | 60 | 75 | 90 | 105 | 120 | 135 | 150 | 165 | 180 | 195 | 210 | 225 | 240 | |
| 134 | +| 16 | 16 | 32 | 48 | 64 | 80 | 96 | 112 | 128 | 144 | 160 | 176 | 192 | 208 | 224 | 240 | 256 | |
| 135 | ++----+----+----+----+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ |
| 136 | + |
| 137 | +OUT; |
| 138 | + $this->assertInOutEquals(array($headers, $rows), $output); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Verifies that Input and Output equals, |
| 143 | + * Sugar method for fast access from tests |
| 144 | + * |
| 145 | + * @param array $input First element is header array, second element is rows array |
| 146 | + * @param mixed $output Expected output |
| 147 | + */ |
| 148 | + private function assertInOutEquals(array $input, $output) { |
| 149 | + $this->_instance->setHeaders($input[0]); |
| 150 | + $this->_instance->setRows($input[1]); |
| 151 | + $this->_instance->display(); |
| 152 | + $this->assertOutFileEqualsWith($output); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Checks that contents of input string and temporary file match |
| 157 | + * |
| 158 | + * @param mixed $expected Expected output |
| 159 | + */ |
| 160 | + private function assertOutFileEqualsWith($expected) { |
| 161 | + $this->assertStringEqualsFile($this->_mockFile, $expected); |
| 162 | + } |
| 163 | +} |
0 commit comments