|
| 1 | +<?php |
| 2 | + |
| 3 | +use AgeekDev\VaporIgnore\Commands\InitCommand; |
| 4 | +use AgeekDev\VaporIgnore\Manifest; |
| 5 | +use AgeekDev\VaporIgnore\Path; |
| 6 | +use Symfony\Component\Console\Tester\CommandTester; |
| 7 | +use Symfony\Component\Yaml\Yaml; |
| 8 | +use Tests\TestCase; |
| 9 | + |
| 10 | +use function PHPUnit\Framework\assertFileExists; |
| 11 | +use function PHPUnit\Framework\assertStringMatchesFormatFile; |
| 12 | + |
| 13 | +it('can run init command', function (): void { |
| 14 | + /** |
| 15 | + * @var TestCase $this |
| 16 | + */ |
| 17 | + $command = $this->resolveCommand(InitCommand::class); |
| 18 | + |
| 19 | + $tester = new CommandTester($command); |
| 20 | + |
| 21 | + $tester->setInputs([ |
| 22 | + 'no', |
| 23 | + ]); |
| 24 | + |
| 25 | + $tester->execute([ |
| 26 | + 'command' => $command->getName(), |
| 27 | + ]); |
| 28 | + |
| 29 | + assertInitCommandRunPorperly($tester, Path::defaultManifest(), Yaml::dump(Manifest::freshConfiguration())); |
| 30 | +}); |
| 31 | + |
| 32 | +it('can run init command with force option', function (): void { |
| 33 | + touch(Path::defaultManifest()); |
| 34 | + |
| 35 | + /** |
| 36 | + * @var TestCase $this |
| 37 | + */ |
| 38 | + $command = $this->resolveCommand(InitCommand::class); |
| 39 | + |
| 40 | + $tester = new CommandTester($command); |
| 41 | + |
| 42 | + $tester->setInputs([ |
| 43 | + 'no', |
| 44 | + ]); |
| 45 | + |
| 46 | + $tester->execute([ |
| 47 | + 'command' => $command->getName(), |
| 48 | + '--force' => true, |
| 49 | + ]); |
| 50 | + |
| 51 | + assertInitCommandRunPorperly($tester, Path::defaultManifest(), Yaml::dump(Manifest::freshConfiguration())); |
| 52 | +}); |
| 53 | + |
| 54 | +it('can run init command and override', function (): void { |
| 55 | + touch(Path::defaultManifest()); |
| 56 | + |
| 57 | + /** |
| 58 | + * @var TestCase $this |
| 59 | + */ |
| 60 | + $command = $this->resolveCommand(InitCommand::class); |
| 61 | + |
| 62 | + $tester = new CommandTester($command); |
| 63 | + |
| 64 | + $tester->setInputs([ |
| 65 | + 'yes', |
| 66 | + 'no', |
| 67 | + ]); |
| 68 | + |
| 69 | + $tester->execute([ |
| 70 | + 'command' => $command->getName(), |
| 71 | + ]); |
| 72 | + |
| 73 | + assertInitCommandRunPorperly($tester, Path::defaultManifest(), Yaml::dump(Manifest::freshConfiguration())); |
| 74 | +}); |
| 75 | + |
| 76 | +it('can run init command with custom manifest', function (): void { |
| 77 | + $customManifest = getcwd().'/vapor-ignore-test.yml'; |
| 78 | + |
| 79 | + /** |
| 80 | + * @var TestCase $this |
| 81 | + */ |
| 82 | + $command = $this->resolveCommand(InitCommand::class); |
| 83 | + |
| 84 | + $tester = new CommandTester($command); |
| 85 | + |
| 86 | + $tester->setInputs([ |
| 87 | + 'no', |
| 88 | + ]); |
| 89 | + |
| 90 | + $tester->execute([ |
| 91 | + 'command' => $command->getName(), |
| 92 | + '--manifest' => $customManifest, |
| 93 | + ]); |
| 94 | + |
| 95 | + assertInitCommandRunPorperly($tester, $customManifest, Yaml::dump(Manifest::freshConfiguration())); |
| 96 | + |
| 97 | + removeFile($customManifest); |
| 98 | +}); |
| 99 | + |
| 100 | +it('can run init command and add clean command', function (): void { |
| 101 | + createVaporManifest(); |
| 102 | + |
| 103 | + /** |
| 104 | + * @var TestCase $this |
| 105 | + */ |
| 106 | + $command = $this->resolveCommand(InitCommand::class); |
| 107 | + |
| 108 | + $tester = new CommandTester($command); |
| 109 | + |
| 110 | + $tester->setInputs([ |
| 111 | + 'yes', |
| 112 | + ]); |
| 113 | + |
| 114 | + $tester->execute([ |
| 115 | + 'command' => $command->getName(), |
| 116 | + ]); |
| 117 | + |
| 118 | + assertInitCommandRunPorperly($tester, Path::defaultManifest(), Yaml::dump(Manifest::freshConfiguration())); |
| 119 | + |
| 120 | + assertFileExists($vaporPath = Path::vaporManifest()); |
| 121 | + |
| 122 | + assertStringMatchesFormatFile($vaporPath, vaporManifestContentWithCleanCommand()); |
| 123 | +}); |
| 124 | + |
| 125 | +function assertInitCommandRunPorperly(CommandTester $tester, string $path, string $format): void |
| 126 | +{ |
| 127 | + $tester->assertCommandIsSuccessful(); |
| 128 | + |
| 129 | + assertFileExists($path); |
| 130 | + |
| 131 | + assertStringMatchesFormatFile($path, $format); |
| 132 | +} |
0 commit comments