|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHP_Parallel_Lint\PhpConsoleHighlighter\Test; |
| 4 | + |
| 5 | +use PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor; |
| 6 | +use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter; |
| 7 | +use ReflectionMethod; |
| 8 | + |
| 9 | +/** |
| 10 | + * @covers PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter::colorLines |
| 11 | + */ |
| 12 | +class ColorLinesTest extends HighlighterTestCase |
| 13 | +{ |
| 14 | + /** @var string */ |
| 15 | + private $input = array( |
| 16 | + 7 => array ( |
| 17 | + 0 => array ( |
| 18 | + 0 => 'token_keyword', |
| 19 | + 1 => 'function ', |
| 20 | + ), |
| 21 | + 1 => array ( |
| 22 | + 0 => 'token_default', |
| 23 | + 1 => 'bar', |
| 24 | + ), |
| 25 | + 2 => array ( |
| 26 | + 0 => 'token_keyword', |
| 27 | + 1 => '(', |
| 28 | + ), |
| 29 | + 3 => array ( |
| 30 | + 0 => 'token_default', |
| 31 | + 1 => '$param', |
| 32 | + ), |
| 33 | + 4 => array ( |
| 34 | + 0 => 'token_unknown', |
| 35 | + 1 => ') ', |
| 36 | + ), |
| 37 | + 5 => array ( |
| 38 | + 0 => 'token_keyword', |
| 39 | + 1 => '{}', |
| 40 | + ), |
| 41 | + ), |
| 42 | + ); |
| 43 | + |
| 44 | + /** |
| 45 | + * Test the (private) `colorlines()` method. |
| 46 | + * |
| 47 | + * @dataProvider dataColorLines |
| 48 | + * |
| 49 | + * @param array $input Tokenized code lines. |
| 50 | + * @param array $output Expected function output. |
| 51 | + * @param bool $withTheme Whether or not the mock should act as if themes |
| 52 | + * have been registered or not. |
| 53 | + * |
| 54 | + * @return array |
| 55 | + */ |
| 56 | + public function testColorLines($input, $expected, $withTheme = true) |
| 57 | + { |
| 58 | + $method = new ReflectionMethod('PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter', 'colorLines'); |
| 59 | + $method->setAccessible(true); |
| 60 | + |
| 61 | + $highlighter = new Highlighter($this->getConsoleColorMock($withTheme)); |
| 62 | + $output = $method->invoke($highlighter, $input); |
| 63 | + $method->setAccessible(false); |
| 64 | + |
| 65 | + $this->assertSame($expected, $output); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Data provider. |
| 70 | + * |
| 71 | + * @return array |
| 72 | + */ |
| 73 | + public function dataColorLines() |
| 74 | + { |
| 75 | + return array( |
| 76 | + 'No lines' => array( |
| 77 | + 'input' => array(), |
| 78 | + 'expected' => array(), |
| 79 | + ), |
| 80 | + 'With theme' => array( |
| 81 | + 'input' => $this->input, |
| 82 | + 'expected' => array( |
| 83 | + 7 => '<token_keyword>function </token_keyword><token_default>bar</token_default><token_keyword>(</token_keyword><token_default>$param</token_default><token_unknown>) </token_unknown><token_keyword>{}</token_keyword>', |
| 84 | + ), |
| 85 | + 'withTheme' => true, |
| 86 | + ), |
| 87 | + 'Without theme' => array( |
| 88 | + 'input' => $this->input, |
| 89 | + 'expected' => array( |
| 90 | + 7 => 'function bar($param) {}', |
| 91 | + ), |
| 92 | + 'withTheme' => false, |
| 93 | + ), |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Integration test for the colorLines() method. |
| 99 | + */ |
| 100 | + public function testColorLinesWithRealColors() |
| 101 | + { |
| 102 | + $expected = array( |
| 103 | + 7 => "\033[32mfunction \033[0m\033[39mbar\033[0m\033[32m(\033[0m\033[39m\$param\033[0m) \033[32m{}\033[0m", |
| 104 | + ); |
| 105 | + |
| 106 | + $color = new ConsoleColor(); |
| 107 | + $color->setForceStyle(true); |
| 108 | + |
| 109 | + $method = new ReflectionMethod('PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter', 'colorLines'); |
| 110 | + $method->setAccessible(true); |
| 111 | + |
| 112 | + $highlighter = new Highlighter($color); |
| 113 | + $output = $method->invoke($highlighter, $this->input); |
| 114 | + $method->setAccessible(false); |
| 115 | + |
| 116 | + $this->assertSame($expected, $output); |
| 117 | + } |
| 118 | +} |
0 commit comments