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