|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Laravel\Tests\Console\Maker; |
| 15 | + |
| 16 | +use ApiPlatform\Laravel\Tests\Console\Maker\Utils\AppServiceFileGenerator; |
| 17 | +use ApiPlatform\Laravel\Tests\Console\Maker\Utils\PathResolver; |
| 18 | +use Illuminate\Console\Command; |
| 19 | +use Illuminate\Contracts\Filesystem\FileNotFoundException; |
| 20 | +use Illuminate\Filesystem\Filesystem; |
| 21 | +use Orchestra\Testbench\Concerns\WithWorkbench; |
| 22 | +use Orchestra\Testbench\TestCase; |
| 23 | + |
| 24 | +class MakeStateProviderCommandTest extends TestCase |
| 25 | +{ |
| 26 | + use WithWorkbench; |
| 27 | + |
| 28 | + /** @var string */ |
| 29 | + private const MAKE_STATE_PROVIDER_COMMAND = 'make:state-provider'; |
| 30 | + /** @var string */ |
| 31 | + private const STATE_PROVIDER_CLASS_NAME = 'Choose a class name for your state provider (e.g. <fg=yellow>AwesomeStateProvider</>)'; |
| 32 | + |
| 33 | + private ?Filesystem $filesystem; |
| 34 | + private PathResolver $pathResolver; |
| 35 | + private AppServiceFileGenerator $appServiceFileGenerator; |
| 36 | + |
| 37 | + /** |
| 38 | + * @throws FileNotFoundException |
| 39 | + */ |
| 40 | + protected function setup(): void |
| 41 | + { |
| 42 | + parent::setUp(); |
| 43 | + |
| 44 | + $this->filesystem = new Filesystem(); |
| 45 | + $this->pathResolver = new PathResolver(); |
| 46 | + $this->appServiceFileGenerator = new AppServiceFileGenerator($this->filesystem); |
| 47 | + |
| 48 | + $this->appServiceFileGenerator->regenerateProviderFile(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @throws FileNotFoundException |
| 53 | + */ |
| 54 | + public function testMakeStateProviderCommand(): void |
| 55 | + { |
| 56 | + $providerName = 'MyStateProvider'; |
| 57 | + $filePath = $this->pathResolver->generateStateFilename($providerName); |
| 58 | + $appServiceProviderPath = $this->pathResolver->getServiceProviderFilePath(); |
| 59 | + |
| 60 | + $this->artisan(self::MAKE_STATE_PROVIDER_COMMAND) |
| 61 | + ->expectsQuestion(self::STATE_PROVIDER_CLASS_NAME, $providerName) |
| 62 | + ->expectsOutputToContain('Success!') |
| 63 | + ->expectsOutputToContain("created: $filePath") |
| 64 | + ->expectsOutputToContain('Next: Open your new state provider class and start customizing it.') |
| 65 | + ->assertExitCode(Command::SUCCESS); |
| 66 | + |
| 67 | + $this->assertFileExists($filePath); |
| 68 | + |
| 69 | + $appServiceProviderContent = $this->filesystem->get($appServiceProviderPath); |
| 70 | + $this->assertStringContainsString('use ApiPlatform\State\ProviderInterface;', $appServiceProviderContent); |
| 71 | + $this->assertStringContainsString("use App\State\\$providerName;", $appServiceProviderContent); |
| 72 | + $this->assertStringContainsString('$this->app->tag(MyStateProvider::class, ProviderInterface::class);', $appServiceProviderContent); |
| 73 | + |
| 74 | + $this->filesystem->delete($filePath); |
| 75 | + } |
| 76 | + |
| 77 | + public function testWhenStateProviderClassAlreadyExists(): void |
| 78 | + { |
| 79 | + $providerName = 'ExistingProvider'; |
| 80 | + $existingFile = $this->pathResolver->generateStateFilename($providerName); |
| 81 | + $this->filesystem->put($existingFile, '<?php // Existing provider'); |
| 82 | + |
| 83 | + $expectedError = \sprintf('[ERROR] The file "%s" can\'t be generated because it already exists.', $existingFile); |
| 84 | + |
| 85 | + $this->artisan(self::MAKE_STATE_PROVIDER_COMMAND) |
| 86 | + ->expectsQuestion(self::STATE_PROVIDER_CLASS_NAME, $providerName) |
| 87 | + ->expectsOutput($expectedError) |
| 88 | + ->assertExitCode(Command::FAILURE); |
| 89 | + |
| 90 | + $this->filesystem->delete($existingFile); |
| 91 | + } |
| 92 | + |
| 93 | + public function testMakeStateProviderCommandWithoutGivenClassName(): void |
| 94 | + { |
| 95 | + $providerName = 'NoEmptyClassName'; |
| 96 | + $filePath = $this->pathResolver->generateStateFilename($providerName); |
| 97 | + |
| 98 | + $this->artisan(self::MAKE_STATE_PROVIDER_COMMAND) |
| 99 | + ->expectsQuestion(self::STATE_PROVIDER_CLASS_NAME, '') |
| 100 | + ->expectsOutput('[ERROR] This value cannot be blank.') |
| 101 | + ->expectsQuestion(self::STATE_PROVIDER_CLASS_NAME, $providerName) |
| 102 | + ->assertExitCode(Command::SUCCESS); |
| 103 | + |
| 104 | + $this->assertFileExists($filePath); |
| 105 | + |
| 106 | + $this->filesystem->delete($filePath); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @throws FileNotFoundException |
| 111 | + */ |
| 112 | + protected function tearDown(): void |
| 113 | + { |
| 114 | + parent::tearDown(); |
| 115 | + |
| 116 | + $this->appServiceFileGenerator->regenerateProviderFile(); |
| 117 | + } |
| 118 | +} |
0 commit comments