| 1 | <?php |
| 2 | |
| 3 | namespace Liuch\DmarcSrg; |
| 4 | |
| 5 | class TextTableTest extends \PHPUnit\Framework\TestCase |
| 6 | { |
| 7 | private $table = null; |
| 8 | private $rlines = [ |
| 9 | '+--------------------------+------+--------+', |
| 10 | '| t1 | ttl2 | title3 |', |
| 11 | '| 2. Long long long string | ss1 | 1111 |', |
| 12 | '| 1. Some string | ss2 | 9 |', |
| 13 | '+--------------------------+------------+--------+', |
| 14 | '| t1 | ttl2 | title3 |', |
| 15 | '| 2. Long long long string | ss1 | 1111 |', |
| 16 | '| 1. Some string | ss2 | 9 |', |
| 17 | '*==========================*======*========*', |
| 18 | '! t1 ! ttl2 ! title3 !', |
| 19 | '! 2. Long long long string ! ss1 ! 1111 !', |
| 20 | '! 1. Some string ! ss2 ! 9 !', |
| 21 | 't1 ttl2 title3', |
| 22 | '2. Long long long string ss1 1111', |
| 23 | '1. Some string ss2 9', |
| 24 | '| t1 | ttl2 | title3 |', |
| 25 | '| 2. Long long long string | ss1 | 1111 |', |
| 26 | '| 1. Some string | ss2 | 9 |', |
| 27 | ]; |
| 28 | |
| 29 | public function setUp(): void |
| 30 | { |
| 31 | $this->table = new TextTable([ 't1', 'ttl2', 'title3' ]); |
| 32 | $this->table->appendRow([ '2. Long long long string', 'ss1', 1111 ]); |
| 33 | $this->table->appendRow([ '1. Some string', 'ss2', 9 ]); |
| 34 | } |
| 35 | |
| 36 | public function testSimple(): void |
| 37 | { |
| 38 | ob_start(); |
| 39 | $this->table->output(); |
| 40 | $output = ob_get_contents(); |
| 41 | ob_end_clean(); |
| 42 | $this->assertEquals($this->genResultText(0, 1, 0, 2, 3, 0), $output); |
| 43 | } |
| 44 | |
| 45 | public function testMinColumnWidth(): void |
| 46 | { |
| 47 | ob_start(); |
| 48 | $this->table->setMinColumnWidth(1, 10)->output(); |
| 49 | $output = ob_get_contents(); |
| 50 | ob_end_clean(); |
| 51 | $this->assertEquals($this->genResultText(4, 5, 4, 6, 7, 4), $output); |
| 52 | } |
| 53 | |
| 54 | public function testMimColumnsWidth(): void |
| 55 | { |
| 56 | ob_start(); |
| 57 | $this->table->setMinColumnsWidth([ 1, 10, 2 ])->output(); |
| 58 | $output = ob_get_contents(); |
| 59 | ob_end_clean(); |
| 60 | $this->assertEquals($this->genResultText(4, 5, 4, 6, 7, 4), $output); |
| 61 | } |
| 62 | |
| 63 | public function testSortByString(): void |
| 64 | { |
| 65 | ob_start(); |
| 66 | $this->table->sortBy(0)->output(); |
| 67 | $output = ob_get_contents(); |
| 68 | ob_end_clean(); |
| 69 | $this->assertEquals($this->genResultText(0, 1, 0, 3, 2, 0), $output); |
| 70 | } |
| 71 | |
| 72 | public function testSortByInt(): void |
| 73 | { |
| 74 | ob_start(); |
| 75 | $this->table->sortBy(2)->output(); |
| 76 | $output = ob_get_contents(); |
| 77 | ob_end_clean(); |
| 78 | $this->assertEquals($this->genResultText(0, 1, 0, 3, 2, 0), $output); |
| 79 | } |
| 80 | |
| 81 | public function testMinColumnWidthAndSortBy(): void |
| 82 | { |
| 83 | ob_start(); |
| 84 | $this->table->setMinColumnWidth(1, 10)->sortBy(2)->output(); |
| 85 | $output = ob_get_contents(); |
| 86 | ob_end_clean(); |
| 87 | $this->assertEquals($this->genResultText(4, 5, 4, 7, 6, 4), $output); |
| 88 | } |
| 89 | |
| 90 | public function testSetColumnAlignment(): void |
| 91 | { |
| 92 | ob_start(); |
| 93 | $this->table->setColumnAlignment(0, 'right')->setColumnAlignment(1, 'right') |
| 94 | ->setColumnAlignment(2, 'left')->output(); |
| 95 | $output = ob_get_contents(); |
| 96 | ob_end_clean(); |
| 97 | $this->assertEquals($this->genResultText(0, 15, 0, 16, 17, 0), $output); |
| 98 | } |
| 99 | |
| 100 | public function testToArray(): void |
| 101 | { |
| 102 | $this->assertEquals($this->genResultArray(0, 1, 0, 2, 3, 0), $this->table->toArray()); |
| 103 | } |
| 104 | |
| 105 | public function testSetBorders(): void |
| 106 | { |
| 107 | ob_start(); |
| 108 | $this->table->setBorders('=', '!', '*')->output(); |
| 109 | $output = ob_get_contents(); |
| 110 | ob_end_clean(); |
| 111 | $this->assertEquals($this->genResultText(8, 9, 8, 10, 11, 8), $output); |
| 112 | } |
| 113 | |
| 114 | public function testSetEmptyBorders(): void |
| 115 | { |
| 116 | ob_start(); |
| 117 | $this->table->setBorders('', '', '')->output(); |
| 118 | $output = ob_get_contents(); |
| 119 | ob_end_clean(); |
| 120 | $this->assertEquals($this->genResultText(12, 13, 14), $output); |
| 121 | } |
| 122 | |
| 123 | private function genResultArray(...$lines): array |
| 124 | { |
| 125 | $res = []; |
| 126 | foreach ($lines as $ln) { |
| 127 | $res[] = $this->rlines[$ln]; |
| 128 | } |
| 129 | return $res; |
| 130 | } |
| 131 | |
| 132 | private function genResultText(...$lines): string |
| 133 | { |
| 134 | return implode(PHP_EOL, $this->genResultArray(...$lines)) . PHP_EOL; |
| 135 | } |
| 136 | } |