|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Stolt\LeanPackage\Tests\Commands; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\RunInSeparateProcess; |
| 8 | +use PHPUnit\Framework\Attributes\Test; |
| 9 | +use Stolt\LeanPackage\Analyser; |
| 10 | +use Stolt\LeanPackage\Archive; |
| 11 | +use Stolt\LeanPackage\Archive\Validator; |
| 12 | +use Stolt\LeanPackage\Commands\ValidateCommand; |
| 13 | +use Stolt\LeanPackage\Presets\Finder; |
| 14 | +use Stolt\LeanPackage\Presets\PhpPreset; |
| 15 | +use Stolt\LeanPackage\Tests\Helpers\FakeInputReader; |
| 16 | +use Stolt\LeanPackage\Tests\TestCase; |
| 17 | +use Symfony\Component\Console\Application; |
| 18 | +use Zenstruck\Console\Test\TestCommand; |
| 19 | + |
| 20 | +final class ValidateCommandStdinOptionsTest extends TestCase |
| 21 | +{ |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + $this->setUpTemporaryDirectory(); |
| 25 | + |
| 26 | + if (!\defined('WORKING_DIRECTORY')) { |
| 27 | + \define('WORKING_DIRECTORY', $this->temporaryDirectory); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + protected function tearDown(): void |
| 32 | + { |
| 33 | + $this->removeDirectory($this->temporaryDirectory); |
| 34 | + } |
| 35 | + |
| 36 | + private function makeAppWithCommand(string $stdinContent): array |
| 37 | + { |
| 38 | + $application = new Application(); |
| 39 | + $fakeInputReader = new FakeInputReader(); |
| 40 | + $fakeInputReader->set($stdinContent); |
| 41 | + |
| 42 | + $command = new ValidateCommand( |
| 43 | + (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory), |
| 44 | + new Validator(new Archive($this->temporaryDirectory)), |
| 45 | + $fakeInputReader |
| 46 | + ); |
| 47 | + $application->add($command); |
| 48 | + |
| 49 | + return [$application, $command]; |
| 50 | + } |
| 51 | + |
| 52 | + #[Test] |
| 53 | + #[RunInSeparateProcess] |
| 54 | + public function stdinHonorsStrictOrderAndReportsInvalidOnShuffledOrder(): void |
| 55 | + { |
| 56 | + // Arrange repository artifacts so expected export-ignores can be computed |
| 57 | + $this->createTemporaryFiles( |
| 58 | + ['README.md', '.gitignore'], |
| 59 | + ['tests'] |
| 60 | + ); |
| 61 | + |
| 62 | + // Shuffled order: README before dotfiles, etc. |
| 63 | + $stdinContent = <<<GITATTR |
| 64 | +* text=auto eol=lf |
| 65 | +
|
| 66 | +README.md export-ignore |
| 67 | +tests/ export-ignore |
| 68 | +.gitignore export-ignore |
| 69 | +.gitattributes export-ignore |
| 70 | +
|
| 71 | +GITATTR; |
| 72 | + |
| 73 | + [$application, $cmd] = $this->makeAppWithCommand($stdinContent); |
| 74 | + $command = $application->find('validate'); |
| 75 | + |
| 76 | + // Act/Assert: with strict order, shuffled input is invalid |
| 77 | + TestCommand::for($command) |
| 78 | + ->addOption('stdin-input') |
| 79 | + ->addOption('enforce-strict-order') |
| 80 | + ->execute() |
| 81 | + ->assertOutputContains('The provided .gitattributes file is considered invalid.') |
| 82 | + ->assertFaulty(); |
| 83 | + } |
| 84 | + |
| 85 | + #[Test] |
| 86 | + #[RunInSeparateProcess] |
| 87 | + public function stdinHonorsStrictOrderAndReportsValidOnExpectedOrder(): void |
| 88 | + { |
| 89 | + // Arrange repository artifacts |
| 90 | + $this->createTemporaryFiles( |
| 91 | + ['README.md', '.gitignore'], |
| 92 | + ['tests'] |
| 93 | + ); |
| 94 | + |
| 95 | + // Likely expected order: .gitattributes, .gitignore, README.md, tests/ |
| 96 | + $stdinContent = <<<GITATTR |
| 97 | +* text=auto eol=lf |
| 98 | +
|
| 99 | +.gitattributes export-ignore |
| 100 | +.gitignore export-ignore |
| 101 | +README.md export-ignore |
| 102 | +tests/ export-ignore |
| 103 | +
|
| 104 | +GITATTR; |
| 105 | + |
| 106 | + [$application, $cmd] = $this->makeAppWithCommand($stdinContent); |
| 107 | + $command = $application->find('validate'); |
| 108 | + |
| 109 | + // Act/Assert: with strict order, correctly ordered input is valid |
| 110 | + TestCommand::for($command) |
| 111 | + ->addOption('stdin-input') |
| 112 | + ->addOption('enforce-strict-order') |
| 113 | + ->execute() |
| 114 | + ->assertOutputContains('The provided .gitattributes content is considered valid.') |
| 115 | + ->assertSuccessful(); |
| 116 | + } |
| 117 | + |
| 118 | + #[Test] |
| 119 | + #[RunInSeparateProcess] |
| 120 | + public function stdinHonorsEnforceAlignmentAndReportsValidOnExpectedOrder(): void |
| 121 | + { |
| 122 | + // Arrange repository artifacts |
| 123 | + $this->createTemporaryFiles( |
| 124 | + ['README.md', '.gitignore'], |
| 125 | + ['tests'] |
| 126 | + ); |
| 127 | + |
| 128 | + // Likely expected order: .gitattributes, .gitignore, README.md, tests/ |
| 129 | + $stdinContent = <<<GITATTR |
| 130 | +* text=auto eol=lf |
| 131 | +
|
| 132 | +.gitattributes export-ignore |
| 133 | +.gitignore export-ignore |
| 134 | +README.md export-ignore |
| 135 | +tests/ export-ignore |
| 136 | +
|
| 137 | +GITATTR; |
| 138 | + |
| 139 | + [$application, $cmd] = $this->makeAppWithCommand($stdinContent); |
| 140 | + $command = $application->find('validate'); |
| 141 | + |
| 142 | + // Act/Assert: with strict order, correctly ordered input is valid |
| 143 | + TestCommand::for($command) |
| 144 | + ->addOption('stdin-input') |
| 145 | + ->addOption('enforce-alignment') |
| 146 | + ->execute() |
| 147 | + ->assertOutputContains('The provided .gitattributes content is considered valid.') |
| 148 | + ->assertSuccessful(); |
| 149 | + } |
| 150 | +} |
0 commit comments