forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathErrorFormatterTestCase.php
More file actions
97 lines (77 loc) · 2.7 KB
/
Copy pathErrorFormatterTestCase.php
File metadata and controls
97 lines (77 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php declare(strict_types = 1);
namespace PHPStan\Testing;
use PHPStan\Analyser\Error;
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\ErrorsConsoleStyle;
use PHPStan\Command\Output;
use PHPStan\Command\Symfony\SymfonyOutput;
use PHPStan\Command\Symfony\SymfonyStyle;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\StreamOutput;
abstract class ErrorFormatterTestCase extends \PHPStan\Testing\PHPStanTestCase
{
protected const DIRECTORY_PATH = '/data/folder/with space/and unicode 😃/project';
private ?StreamOutput $outputStream = null;
private ?Output $output = null;
private function getOutputStream(): StreamOutput
{
if ($this->outputStream === null) {
$resource = fopen('php://memory', 'w', false);
if ($resource === false) {
throw new \PHPStan\ShouldNotHappenException();
}
$this->outputStream = new StreamOutput($resource);
}
return $this->outputStream;
}
protected function getOutput(): Output
{
if ($this->output === null) {
$errorConsoleStyle = new ErrorsConsoleStyle(new StringInput(''), $this->getOutputStream());
$this->output = new SymfonyOutput($this->getOutputStream(), new SymfonyStyle($errorConsoleStyle));
}
return $this->output;
}
protected function getOutputContent(): string
{
rewind($this->getOutputStream()->getStream());
$contents = stream_get_contents($this->getOutputStream()->getStream());
if ($contents === false) {
throw new \PHPStan\ShouldNotHappenException();
}
return $this->rtrimMultiline($contents);
}
protected function getAnalysisResult(int $numFileErrors, int $numGenericErrors): AnalysisResult
{
if ($numFileErrors > 5 || $numFileErrors < 0 || $numGenericErrors > 2 || $numGenericErrors < 0) {
throw new \PHPStan\ShouldNotHappenException();
}
$fileErrors = array_slice([
new Error('Foo', self::DIRECTORY_PATH . '/folder with unicode 😃/file name with "spaces" and unicode 😃.php', 4),
new Error('Foo', self::DIRECTORY_PATH . '/foo.php', 1),
new Error("Bar\nBar2", self::DIRECTORY_PATH . '/foo.php', 5),
new Error("Bar\nBar2", self::DIRECTORY_PATH . '/folder with unicode 😃/file name with "spaces" and unicode 😃.php', 2),
new Error("Bar\nBar2", self::DIRECTORY_PATH . '/foo.php', null),
], 0, $numFileErrors);
$genericErrors = array_slice([
'first generic error',
'second generic error',
], 0, $numGenericErrors);
return new AnalysisResult(
$fileErrors,
$genericErrors,
[],
[],
false,
null,
true
);
}
private function rtrimMultiline(string $output): string
{
$result = array_map(static function (string $line): string {
return rtrim($line, " \r\n");
}, explode("\n", $output));
return implode("\n", $result);
}
}