|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @testCase |
| 5 | + */ |
| 6 | + |
| 7 | +require __DIR__ . '/../vendor/autoload.php'; |
| 8 | + |
| 9 | +use JakubOnderka\PhpParallelLint\ErrorFormatter; |
| 10 | +use JakubOnderka\PhpParallelLint\GitLabOutput; |
| 11 | +use JakubOnderka\PhpParallelLint\IWriter; |
| 12 | +use JakubOnderka\PhpParallelLint\Result; |
| 13 | +use JakubOnderka\PhpParallelLint\SyntaxError; |
| 14 | +use Tester\Assert; |
| 15 | + |
| 16 | +class OutputTest extends Tester\TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @dataProvider getGitLabOutputData |
| 20 | + */ |
| 21 | + public function testGitLabOutput($errors) |
| 22 | + { |
| 23 | + $result = new Result($errors, array(), array(), 0); |
| 24 | + $writer = new TestWriter(); |
| 25 | + $output = new GitLabOutput($writer); |
| 26 | + |
| 27 | + $output->writeResult($result, new ErrorFormatter(), true); |
| 28 | + |
| 29 | + $result = (array) json_decode($writer->getLogs()); |
| 30 | + |
| 31 | + for ($i = 0; $i < count($result) && $i < count($errors); $i++) { |
| 32 | + $message = $errors[$i]->getMessage(); |
| 33 | + $filePath = $errors[$i]->getFilePath(); |
| 34 | + $line = 1; |
| 35 | + if ($errors[$i] instanceof SyntaxError) { |
| 36 | + $line = $errors[$i]->getLine(); |
| 37 | + } |
| 38 | + Assert::equal($result[$i]->type, 'issue'); |
| 39 | + Assert::equal($result[$i]->check_name, 'Parse error'); |
| 40 | + Assert::equal($result[$i]->categories, 'Style'); |
| 41 | + Assert::equal($result[$i]->severity, 'minor'); |
| 42 | + Assert::equal($result[$i]->description, $message); |
| 43 | + Assert::equal($result[$i]->fingerprint, md5($filePath . $message . $line)); |
| 44 | + Assert::equal($result[$i]->location->path, $filePath); |
| 45 | + Assert::equal($result[$i]->location->lines->begin, $line); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public function getGitLabOutputData() |
| 50 | + { |
| 51 | + return array( |
| 52 | + array( |
| 53 | + 'errors' => array() |
| 54 | + ), |
| 55 | + array( |
| 56 | + 'errors' => array( |
| 57 | + new SyntaxError('foo/bar.php', "Parse error: syntax error, unexpected in foo/bar.php on line 52") |
| 58 | + ) |
| 59 | + ), |
| 60 | + array( |
| 61 | + 'errors' => array( |
| 62 | + new JakubOnderka\PhpParallelLint\Error('foo/bar.php', "PHP Parse error: syntax error, unexpected ';'") |
| 63 | + ) |
| 64 | + ), |
| 65 | + array( |
| 66 | + 'errors' => array( |
| 67 | + new SyntaxError('foo/bar.php', "Parse error: syntax error, unexpected in foo/bar.php on line 52"), |
| 68 | + new JakubOnderka\PhpParallelLint\Error('foo/bar.php', "PHP Parse error: syntax error, unexpected ';'") |
| 69 | + ) |
| 70 | + ), |
| 71 | + ); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +class TestWriter implements IWriter |
| 76 | +{ |
| 77 | + /** @var string */ |
| 78 | + protected $logs = ""; |
| 79 | + |
| 80 | + /** |
| 81 | + * @param string $string |
| 82 | + */ |
| 83 | + public function write($string) |
| 84 | + { |
| 85 | + $this->logs .= $string; |
| 86 | + } |
| 87 | + |
| 88 | + public function getLogs() |
| 89 | + { |
| 90 | + return $this->logs; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +$testCase = new OutputTest; |
| 95 | +$testCase->run(); |
0 commit comments