|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 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\Tests\Symfony\Maker; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\Exception\InvalidArgumentException; |
| 17 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 18 | +use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 19 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 20 | +use Symfony\Component\Console\Exception\MissingInputException; |
| 21 | +use Symfony\Component\Console\Tester\CommandTester; |
| 22 | +use Symfony\Component\Filesystem\Filesystem; |
| 23 | + |
| 24 | +class MakeFilterTest extends KernelTestCase |
| 25 | +{ |
| 26 | + protected function setup(): void |
| 27 | + { |
| 28 | + (new Filesystem())->remove(self::tempDir()); |
| 29 | + } |
| 30 | + |
| 31 | + #[DataProvider('filterProvider')] |
| 32 | + public function testMakeFilter(string $type, string $name, bool $isInteractive): void |
| 33 | + { |
| 34 | + $inputs = ['type' => $type, 'name' => $name]; |
| 35 | + $newFilterFile = self::tempFile("src/Filter/{$name}.php"); |
| 36 | + |
| 37 | + $command = (new Application(self::bootKernel()))->find('make:filter'); |
| 38 | + $commandTester = new CommandTester($command); |
| 39 | + $commandTester->setInputs($isInteractive ? $inputs : []); |
| 40 | + $commandTester->execute($isInteractive ? [] : $inputs); |
| 41 | + |
| 42 | + $this->assertFileExists($newFilterFile); |
| 43 | + |
| 44 | + $expected = preg_replace('~\R~u', "\r\n", file_get_contents(__DIR__."/../../Fixtures/Symfony/Maker/{$name}.fixture")); |
| 45 | + $result = preg_replace('~\R~u', "\r\n", file_get_contents($newFilterFile)); |
| 46 | + $this->assertStringContainsString($expected, $result); |
| 47 | + |
| 48 | + $display = $commandTester->getDisplay(); |
| 49 | + $commandTester->assertCommandIsSuccessful(); |
| 50 | + $interactiveOutputType = 'Choose a type for your filter'; |
| 51 | + $interactiveOutputName = 'Choose a class name for your filter'; |
| 52 | + |
| 53 | + if ($isInteractive) { |
| 54 | + $this->assertStringContainsString($interactiveOutputType, $display); |
| 55 | + $this->assertStringContainsString($interactiveOutputName, $display); |
| 56 | + } else { |
| 57 | + $this->assertStringNotContainsString($interactiveOutputType, $display); |
| 58 | + $this->assertStringNotContainsString($interactiveOutputName, $display); |
| 59 | + } |
| 60 | + |
| 61 | + $this->assertStringContainsString(' Next: Open your filter class and start customizing it.', $display); |
| 62 | + } |
| 63 | + |
| 64 | + public static function filterProvider(): \Generator |
| 65 | + { |
| 66 | + yield 'Generate ORM filter' => ['orm', 'CustomOrmFilter', true]; |
| 67 | + yield 'Generate ORM filter not interactively' => ['orm', 'CustomOrmFilter', false]; |
| 68 | + yield 'Generate ODM filter' => ['odm', 'CustomOdmFilter', true]; |
| 69 | + yield 'Generate ODM filter not interactively' => ['odm', 'CustomOdmFilter', false]; |
| 70 | + } |
| 71 | + |
| 72 | + #[DataProvider('filterErrorProvider')] |
| 73 | + public function testCommandFailsWithInvalidInput(array $inputs, string $exceptionClass = InvalidArgumentException::class): void |
| 74 | + { |
| 75 | + $this->expectException($exceptionClass); |
| 76 | + $command = (new Application(self::bootKernel()))->find('make:filter'); |
| 77 | + (new CommandTester($command))->execute($inputs); |
| 78 | + } |
| 79 | + |
| 80 | + public static function filterErrorProvider(): \Generator |
| 81 | + { |
| 82 | + yield 'Missing type and name arguments' => [ |
| 83 | + [], |
| 84 | + MissingInputException::class, |
| 85 | + ]; |
| 86 | + |
| 87 | + yield 'Invalid type argument' => [ |
| 88 | + ['type' => 'john', 'name' => 'MyCustomFilter'], |
| 89 | + ]; |
| 90 | + |
| 91 | + yield 'No valid type argument given' => [ |
| 92 | + ['type' => 'John', 'name' => 'MyCustomFilter'], |
| 93 | + ]; |
| 94 | + |
| 95 | + yield 'Missing name argument' => [ |
| 96 | + ['type' => 'orm'], |
| 97 | + MissingInputException::class, |
| 98 | + ]; |
| 99 | + } |
| 100 | + |
| 101 | + private static function tempDir(): string |
| 102 | + { |
| 103 | + return __DIR__.'/../../Fixtures/app/var/tmp'; |
| 104 | + } |
| 105 | + |
| 106 | + private static function tempFile(string $path): string |
| 107 | + { |
| 108 | + return \sprintf('%s/%s', self::tempDir(), $path); |
| 109 | + } |
| 110 | +} |
0 commit comments