1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Integration; |
6
|
|
|
|
7
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\BaseLineFileName; |
8
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\ProjectRoot; |
9
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\RelativeFileName; |
10
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\File\FileReader; |
11
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\File\FileWriter; |
12
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\Identifier; |
13
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\UpgradeBaseLineCommand; |
14
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Container\Container; |
15
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\GitDiffHistoryAnalyser\internal\GitCliWrapper; |
16
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\PhanJsonResultsParser\PhanJsonIdentifier; |
17
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\PhpCodeSnifferJsonResultsParser\PhpCodeSnifferJsonIdentifier; |
18
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\PhpmdJsonResultsParser\PhpmdJsonIdentifier; |
19
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\PhpstanJsonResultsParser\PhpstanJsonIdentifier; |
20
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\PsalmJsonResultsParser\PsalmJsonIdentifier; |
21
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\SarbJsonResultsParser\SarbJsonIdentifier; |
22
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\ResourceLoaderTrait; |
23
|
|
|
use PHPUnit\Framework\TestCase; |
24
|
|
|
use Symfony\Component\Console\Application; |
25
|
|
|
use Symfony\Component\Console\Command\Command; |
26
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
27
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
28
|
|
|
|
29
|
|
|
class UpgradeV0BaselineFilesTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
use ResourceLoaderTrait; |
32
|
|
|
use TestDirectoryTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Filesystem |
36
|
|
|
*/ |
37
|
|
|
private $fileSystem; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var GitCliWrapper |
41
|
|
|
*/ |
42
|
|
|
private $gitWrapper; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ProjectRoot |
46
|
|
|
*/ |
47
|
|
|
private $projectRoot; |
48
|
|
|
/** |
49
|
|
|
* @var BaseLineFileName |
50
|
|
|
*/ |
51
|
|
|
private $baseLineFileName; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var FileWriter |
55
|
|
|
*/ |
56
|
|
|
private $fileWriter; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Application |
60
|
|
|
*/ |
61
|
|
|
private $application; |
62
|
|
|
/** |
63
|
|
|
* @var Command |
64
|
|
|
*/ |
65
|
|
|
private $upgradeCommand; |
66
|
|
|
|
67
|
|
|
protected function setUp(): void |
68
|
|
|
{ |
69
|
|
|
$this->fileSystem = new Filesystem(); |
70
|
|
|
$this->gitWrapper = new GitCliWrapper(); |
71
|
|
|
$this->createTestDirectory(); |
72
|
|
|
$baseline = $this->projectRoot->getAbsoluteFileName(new RelativeFileName('baseline')); |
73
|
|
|
$this->baseLineFileName = new BaseLineFileName($baseline->getFileName()); |
74
|
|
|
$this->fileWriter = new FileWriter(); |
75
|
|
|
|
76
|
|
|
$container = new Container(); |
77
|
|
|
$this->application = $container->getApplication(); |
78
|
|
|
$this->upgradeCommand = $this->application->find(UpgradeBaseLineCommand::COMMAND_NAME); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @return array<int,array{string,Identifier}> */ |
|
|
|
|
82
|
|
|
public function dataProvider(): array |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
['phan', new PhanJsonIdentifier()], |
86
|
|
|
['phpcs-json', new PhpCodeSnifferJsonIdentifier()], |
87
|
|
|
['phpcs-txt', new PhpCodeSnifferJsonIdentifier()], |
88
|
|
|
['phpmd', new PhpmdJsonIdentifier()], |
89
|
|
|
['phpstan-json', new PhpstanJsonIdentifier()], |
90
|
|
|
['phpstan-text', new PhpstanJsonIdentifier()], |
91
|
|
|
['psalm-json', new PsalmJsonIdentifier()], |
92
|
|
|
['psalm-text', new PsalmJsonIdentifier()], |
93
|
|
|
['sarb', new SarbJsonIdentifier()], |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @dataProvider dataProvider |
99
|
|
|
*/ |
100
|
|
|
public function testUpgrade(string $file, Identifier $identifier): void |
101
|
|
|
{ |
102
|
|
|
$this->createOriginalBaselineFile($file); |
103
|
|
|
|
104
|
|
|
$commandTester = new CommandTester($this->upgradeCommand); |
105
|
|
|
$arguments = [ |
106
|
|
|
'command' => $this->upgradeCommand->getName(), |
107
|
|
|
'baseline-file' => $this->baseLineFileName->getFileName(), |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
$actualExitCode = $commandTester->execute($arguments); |
111
|
|
|
|
112
|
|
|
// Check CLI responds with correct messaging to the user |
113
|
|
|
$this->assertSame(0, $actualExitCode); |
114
|
|
|
$output = $commandTester->getDisplay(); |
115
|
|
|
$this->assertStringContainsString('Baseline updated', $output); |
116
|
|
|
$this->assertStringContainsString($identifier->getToolCommand(), $output); |
117
|
|
|
|
118
|
|
|
// Check updated baseline is correct |
119
|
|
|
$fileReader = new FileReader(); |
120
|
|
|
$updatedBaselineContents = $fileReader->readFile($this->baseLineFileName); |
121
|
|
|
$expectedBaselineContents = $this->getResource("v0/{$file}.expected"); |
122
|
|
|
$this->assertSame($expectedBaselineContents, $updatedBaselineContents); |
123
|
|
|
|
124
|
|
|
$this->removeTestDirectory(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testUpgradeFails(): void |
128
|
|
|
{ |
129
|
|
|
$this->createOriginalBaselineFile('invalid'); |
130
|
|
|
|
131
|
|
|
$commandTester = new CommandTester($this->upgradeCommand); |
132
|
|
|
$arguments = [ |
133
|
|
|
'command' => $this->upgradeCommand->getName(), |
134
|
|
|
'baseline-file' => $this->baseLineFileName->getFileName(), |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
$actualExitCode = $commandTester->execute($arguments); |
138
|
|
|
|
139
|
|
|
$this->assertSame(12, $actualExitCode); |
140
|
|
|
$this->removeTestDirectory(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function createOriginalBaselineFile(string $file): void |
144
|
|
|
{ |
145
|
|
|
$originalBaselineContents = $this->getResource("v0/{$file}.baseline"); |
146
|
|
|
$this->fileWriter->writeFile($this->baseLineFileName, $originalBaselineContents); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|