|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SqlParser\Tests\Utils; |
| 4 | + |
| 5 | +use SqlParser\Tests\TestCase; |
| 6 | + |
| 7 | +class CLITest extends TestCase |
| 8 | +{ |
| 9 | + private function getCLI($getopt) |
| 10 | + { |
| 11 | + $cli = $this->getMockBuilder('SqlParser\Utils\CLI')->setMethods(array('getopt'))->getMock(); |
| 12 | + $cli->method('getopt')->willReturn($getopt); |
| 13 | + return $cli; |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * @dataProvider highlightParams |
| 18 | + */ |
| 19 | + public function testRunHighlight($getopt, $output, $result) |
| 20 | + { |
| 21 | + $cli = $this->getCLI($getopt); |
| 22 | + $this->expectOutputString($output); |
| 23 | + $this->assertEquals($result, $cli->runHighlight()); |
| 24 | + } |
| 25 | + |
| 26 | + public function highlightParams() |
| 27 | + { |
| 28 | + return array( |
| 29 | + array( |
| 30 | + array('q' => 'SELECT 1'), |
| 31 | + "\x1b[35mSELECT\n \x1b[92m1\x1b[0m\n", |
| 32 | + 0, |
| 33 | + ), |
| 34 | + array( |
| 35 | + array('q' => 'SELECT 1', 'f' => 'html'), |
| 36 | + '<span class="sql-reserved">SELECT</span>' . "\n" . |
| 37 | + ' <span class="sql-number">1</span>' . "\n", |
| 38 | + 0, |
| 39 | + ), |
| 40 | + array( |
| 41 | + array('h' => true), |
| 42 | + 'Usage: highlight-query --query SQL [--format html|cli|text]' . "\n", |
| 43 | + 0, |
| 44 | + ), |
| 45 | + array( |
| 46 | + array(), |
| 47 | + 'ERROR: Missing parameters!' . "\n" . |
| 48 | + 'Usage: highlight-query --query SQL [--format html|cli|text]' . "\n", |
| 49 | + 1, |
| 50 | + ), |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @dataProvider lintParams |
| 56 | + */ |
| 57 | + public function testRunLint($getopt, $output, $result) |
| 58 | + { |
| 59 | + $cli = $this->getCLI($getopt); |
| 60 | + $this->expectOutputString($output); |
| 61 | + $this->assertEquals($result, $cli->runLint()); |
| 62 | + } |
| 63 | + |
| 64 | + public function lintParams() |
| 65 | + { |
| 66 | + return array( |
| 67 | + array( |
| 68 | + array('q' => 'SELECT 1'), |
| 69 | + '', |
| 70 | + 0, |
| 71 | + ), |
| 72 | + array( |
| 73 | + array('q' => 'SELECT SELECT'), |
| 74 | + '#1: An expression was expected. (near "SELECT" at position 7)' . "\n" . |
| 75 | + '#2: This type of clause was previously parsed. (near "SELECT" at position 7)' . "\n" . |
| 76 | + '#3: An expression was expected. (near "" at position 0)' . "\n", |
| 77 | + 10, |
| 78 | + ), |
| 79 | + array( |
| 80 | + array('h' => true), |
| 81 | + 'Usage: lint-query --query SQL' . "\n", |
| 82 | + 0, |
| 83 | + ), |
| 84 | + array( |
| 85 | + array(), |
| 86 | + 'ERROR: Missing parameters!' . "\n" . |
| 87 | + 'Usage: lint-query --query SQL' . "\n", |
| 88 | + 1, |
| 89 | + ), |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
0 commit comments