|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Stolt\LeanPackage\Tests\Commands; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | +use Stolt\LeanPackage\Analyser; |
| 9 | +use Stolt\LeanPackage\Commands\UpdateCommand; |
| 10 | +use Stolt\LeanPackage\GitattributesFileRepository; |
| 11 | +use Stolt\LeanPackage\Presets\Finder; |
| 12 | +use Stolt\LeanPackage\Presets\PhpPreset; |
| 13 | +use Stolt\LeanPackage\Tests\TestCase; |
| 14 | +use Zenstruck\Console\Test\TestCommand; |
| 15 | + |
| 16 | +final class UpdateCommandTest extends TestCase |
| 17 | +{ |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + $this->setUpTemporaryDirectory(); |
| 21 | + } |
| 22 | + |
| 23 | + protected function tearDown(): void |
| 24 | + { |
| 25 | + $this->removeDirectory($this->temporaryDirectory); |
| 26 | + } |
| 27 | + |
| 28 | + #[Test] |
| 29 | + public function updatesExistingGitattributesAndReplacesHeader(): void |
| 30 | + { |
| 31 | + $analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory); |
| 32 | + $repository = new GitattributesFileRepository($analyser); |
| 33 | + $command = new UpdateCommand($analyser, $repository); |
| 34 | + |
| 35 | + $artifactFilenames = ['.gitignore']; |
| 36 | + |
| 37 | + $this->createTemporaryFiles( |
| 38 | + $artifactFilenames, |
| 39 | + ['tests', '.github'] |
| 40 | + ); |
| 41 | + |
| 42 | + $gitattributesContent = <<<CONTENT |
| 43 | +# This file was generated by the lean package validator (http://git.io/lean-package-validator). |
| 44 | +
|
| 45 | +* text=auto eol=lf |
| 46 | +
|
| 47 | +.gitattributes export-ignore |
| 48 | +.github/ export-ignore |
| 49 | +tests/ export-ignore |
| 50 | +CONTENT; |
| 51 | + |
| 52 | + $this->createTemporaryGitattributesFile($gitattributesContent); |
| 53 | + |
| 54 | + TestCommand::for($command) |
| 55 | + ->addArgument($this->temporaryDirectory) |
| 56 | + ->execute() |
| 57 | + ->assertSuccessful() |
| 58 | + ->assertOutputContains("The .gitattributes file at {$this->temporaryDirectory} has been updated."); |
| 59 | + |
| 60 | + $gitattributesPath = $this->temporaryDirectory . DIRECTORY_SEPARATOR . '.gitattributes'; |
| 61 | + $this->assertFileExists($gitattributesPath); |
| 62 | + |
| 63 | + $content = (string) \file_get_contents($gitattributesPath); |
| 64 | + |
| 65 | + // Header should be the "modified" one after update |
| 66 | + $this->assertStringContainsString( |
| 67 | + '# This file was partly modified by the lean package validator (http://git.io/lean-package-validator).', |
| 68 | + $content |
| 69 | + ); |
| 70 | + |
| 71 | + // Sanity: should still contain export-ignore entries and likely 'tests/' gets added |
| 72 | + $this->assertStringContainsString('.gitattributes', $content); |
| 73 | + $this->assertStringContainsString('.github/', $content); |
| 74 | + $this->assertStringContainsString('tests/', $content); |
| 75 | + $this->assertStringContainsString('export-ignore', $content); |
| 76 | + } |
| 77 | + |
| 78 | + #[Test] |
| 79 | + public function failsWhenNoGitattributesFileIsPresent(): void |
| 80 | + { |
| 81 | + // Remove the file |
| 82 | + @\unlink($this->temporaryDirectory . DIRECTORY_SEPARATOR . '.gitattributes'); |
| 83 | + |
| 84 | + $analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory); |
| 85 | + $repository = new GitattributesFileRepository($analyser); |
| 86 | + $command = new UpdateCommand($analyser, $repository); |
| 87 | + |
| 88 | + TestCommand::for($command) |
| 89 | + ->addArgument($this->temporaryDirectory) |
| 90 | + ->execute() |
| 91 | + ->assertFaulty() |
| 92 | + ->assertOutputContains('No .gitattributes file found. Use the create command to create one first.'); |
| 93 | + } |
| 94 | +} |
0 commit comments