From 63cead25240256dec103302b8e68f13efe98d3ac Mon Sep 17 00:00:00 2001 From: Anto Date: Sat, 28 Nov 2020 09:22:29 +0100 Subject: [PATCH] Add tests for the new Maker commands --- composer.json | 3 +- src/Bridge/Symfony/Maker/MakeDataProvider.php | 2 +- .../Resources/skeleton/DataPersister.tpl.php | 10 +- .../Resources/skeleton/DataProvider.tpl.php | 9 +- .../Symfony/Maker/MakeDataPersisterTest.php | 167 ++++++++++ .../Symfony/Maker/MakeDataProviderTest.php | 299 ++++++++++++++++++ tests/Fixtures/app/AppKernel.php | 2 + 7 files changed, 486 insertions(+), 6 deletions(-) create mode 100644 tests/Bridge/Symfony/Maker/MakeDataPersisterTest.php create mode 100644 tests/Bridge/Symfony/Maker/MakeDataProviderTest.php diff --git a/composer.json b/composer.json index b32d8ea9a2f..39085fc5e3b 100644 --- a/composer.json +++ b/composer.json @@ -114,7 +114,8 @@ }, "autoload-dev": { "psr-4": { - "ApiPlatform\\Core\\Tests\\": "tests/" + "ApiPlatform\\Core\\Tests\\": "tests/", + "App\\": "tests/Fixtures/app/var/tmp/src/" } }, "config": { diff --git a/src/Bridge/Symfony/Maker/MakeDataProvider.php b/src/Bridge/Symfony/Maker/MakeDataProvider.php index 47bd055d7ef..030801e5eb9 100644 --- a/src/Bridge/Symfony/Maker/MakeDataProvider.php +++ b/src/Bridge/Symfony/Maker/MakeDataProvider.php @@ -50,7 +50,7 @@ public static function getCommandName(): string public function configureCommand(Command $command, InputConfiguration $inputConfig) { $command - ->setDescription('Creates an API Platform data povider') + ->setDescription('Creates an API Platform data provider') ->addArgument('name', InputArgument::OPTIONAL, 'Choose a class name for your data provider (e.g. AwesomeDataProvider)') ->addArgument('resource-class', InputArgument::OPTIONAL, 'Choose a Resource class') ->addOption('item-only', null, InputOption::VALUE_NONE, 'Generate only an item data provider') diff --git a/src/Bridge/Symfony/Maker/Resources/skeleton/DataPersister.tpl.php b/src/Bridge/Symfony/Maker/Resources/skeleton/DataPersister.tpl.php index 58e19c35d10..207a39f43b7 100644 --- a/src/Bridge/Symfony/Maker/Resources/skeleton/DataPersister.tpl.php +++ b/src/Bridge/Symfony/Maker/Resources/skeleton/DataPersister.tpl.php @@ -14,7 +14,7 @@ final class implements ContextAwareDataPersisterInterface */ public function supports($data, array $context = []): bool { - + return $data instanceof ::class; // Add your custom conditions here return false; // Add your custom conditions here @@ -24,7 +24,13 @@ public function supports($data, array $context = []): bool /** * {@inheritdoc} */ - public function persist($data, array $context = []) + public function persist($data, array $context = [])= 70200) { + echo ': object'; + }?> + { // call your persistence layer to save $data diff --git a/src/Bridge/Symfony/Maker/Resources/skeleton/DataProvider.tpl.php b/src/Bridge/Symfony/Maker/Resources/skeleton/DataProvider.tpl.php index c1aa0711d23..2ddaab80594 100644 --- a/src/Bridge/Symfony/Maker/Resources/skeleton/DataProvider.tpl.php +++ b/src/Bridge/Symfony/Maker/Resources/skeleton/DataProvider.tpl.php @@ -17,7 +17,7 @@ final class implements + return ::class === $resourceClass; // Add your custom conditions here return false; // Add your custom conditions here @@ -38,7 +38,12 @@ public function getCollection(string $resourceClass, string $operationName = nul /** * {@inheritdoc} */ - public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])= 70200 ? ': ?object' : '') ?> + public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])= 70200) { + echo ': ?object'; + }?> { // Retrieve the item from somewhere then return it or null if not found diff --git a/tests/Bridge/Symfony/Maker/MakeDataPersisterTest.php b/tests/Bridge/Symfony/Maker/MakeDataPersisterTest.php new file mode 100644 index 00000000000..62e6e572c50 --- /dev/null +++ b/tests/Bridge/Symfony/Maker/MakeDataPersisterTest.php @@ -0,0 +1,167 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy; +use Symfony\Bundle\FrameworkBundle\Console\Application; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\Console\Tester\CommandTester; +use Symfony\Component\Filesystem\Filesystem; + +class MakeDataPersisterTest extends KernelTestCase +{ + protected function tearDown(): void + { + (new Filesystem())->remove(self::tempDir()); + } + + /** @dataProvider dataPersisterProvider */ + public function testMakeDataPersister(array $commandInputs, array $userInputs, string $expected) + { + $this->assertFileNotExists(self::tempFile('src/DataPersister/CustomDataPersister.php')); + + $tester = new CommandTester((new Application(self::bootKernel()))->find('make:data-persister')); + $tester->setInputs($userInputs); + $tester->execute($commandInputs); + + $this->assertFileExists(self::tempFile('src/DataPersister/CustomDataPersister.php')); + + $display = $tester->getDisplay(); + $this->assertStringContainsString('Success!', $display); + + if (!isset($commandInputs['name'])) { + $this->assertStringContainsString('Choose a class name for your data persister (e.g. AwesomeDataPersister):', $display); + } else { + $this->assertStringNotContainsString('Choose a class name for your data persister (e.g. AwesomeDataPersister):', $display); + } + if (!isset($commandInputs['resource-class'])) { + $this->assertStringContainsString('Choose a Resource class:', $display); + } else { + $this->assertStringNotContainsString('Choose a Resource class:', $display); + } + + $this->assertSame($expected, file_get_contents(self::tempFile('src/DataPersister/CustomDataPersister.php'))); + + $this->assertStringContainsString('Success!', $display = $tester->getDisplay()); + $this->assertStringContainsString(<< [ + [], + ['CustomDataPersister', ''], + \PHP_VERSION_ID >= 70200 ? $expected : str_replace(': object', '', $expected), + ]; + + $expected = <<<'EOF' + [ + [], + ['CustomDataPersister', Dummy::class], + $expected, + ]; + + yield 'Generate data persister with resource class not interactively' => [ + ['name' => 'CustomDataPersister', 'resource-class' => Dummy::class], + [], + $expected, + ]; + } + + private static function tempDir(): string + { + return __DIR__.'/../../../Fixtures/app/var/tmp'; + } + + private static function tempFile(string $path): string + { + return sprintf('%s/%s', self::tempDir(), $path); + } +} diff --git a/tests/Bridge/Symfony/Maker/MakeDataProviderTest.php b/tests/Bridge/Symfony/Maker/MakeDataProviderTest.php new file mode 100644 index 00000000000..b06a9edaae6 --- /dev/null +++ b/tests/Bridge/Symfony/Maker/MakeDataProviderTest.php @@ -0,0 +1,299 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy; +use Symfony\Bundle\FrameworkBundle\Console\Application; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\Console\Tester\CommandTester; +use Symfony\Component\Filesystem\Filesystem; + +class MakeDataProviderTest extends KernelTestCase +{ + protected function tearDown(): void + { + (new Filesystem())->remove(self::tempDir()); + } + + /** @dataProvider dataProviderProvider */ + public function testMakeDataProvider(array $commandInputs, array $userInputs, string $expected) + { + $this->assertFileNotExists(self::tempFile('src/DataProvider/CustomDataProvider.php')); + + $tester = new CommandTester((new Application(self::bootKernel()))->find('make:data-provider')); + $tester->setInputs($userInputs); + $tester->execute($commandInputs); + + $this->assertFileExists(self::tempFile('src/DataProvider/CustomDataProvider.php')); + + $this->assertSame($expected, file_get_contents(self::tempFile('src/DataProvider/CustomDataProvider.php'))); + + $display = $tester->getDisplay(); + $this->assertStringContainsString('Success!', $display); + + if (!isset($commandInputs['name'])) { + $this->assertStringContainsString('Choose a class name for your data provider (e.g. AwesomeDataProvider):', $display); + } else { + $this->assertStringNotContainsString('Choose a class name for your data provider (e.g. AwesomeDataProvider):', $display); + } + if (!isset($commandInputs['resource-class'])) { + $this->assertStringContainsString(' Choose a Resource class:', $display); + } else { + $this->assertStringNotContainsString('Choose a Resource class:', $display); + } + + $this->assertStringContainsString(<< [ + [], + ['CustomDataProvider', ''], + $expected, + ]; + + $expected = <<<'EOF' + [ + [], + ['CustomDataProvider', Dummy::class], + $expected, + ]; + + yield 'Generate all with resource class not interactively' => [ + ['name' => 'CustomDataProvider', 'resource-class' => Dummy::class], + [], + $expected, + ]; + + $expected = <<<'EOF' + [ + ['--item-only' => true], + ['CustomDataProvider', ''], + $expected, + ]; + + $expected = <<<'EOF' + [ + ['--item-only' => true], + ['CustomDataProvider', Dummy::class], + $expected, + ]; + + yield 'Generate an item data provider with a resource class not interactively' => [ + ['name' => 'CustomDataProvider', 'resource-class' => Dummy::class, '--item-only' => true], + [], + $expected, + ]; + + $expected = <<<'EOF' + [ + ['--collection-only' => true], + ['CustomDataProvider', ''], + $expected, + ]; + + $expected = <<<'EOF' + [ + ['--collection-only' => true], + ['CustomDataProvider', Dummy::class], + $expected, + ]; + + yield 'Generate a collection data provider with a resource class not interactively' => [ + ['name' => 'CustomDataProvider', 'resource-class' => Dummy::class, '--collection-only' => true], + [], + $expected, + ]; + } + + private static function tempDir(): string + { + return __DIR__.'/../../../Fixtures/app/var/tmp'; + } + + private static function tempFile(string $path): string + { + return sprintf('%s/%s', self::tempDir(), $path); + } +} diff --git a/tests/Fixtures/app/AppKernel.php b/tests/Fixtures/app/AppKernel.php index dc387c557cb..4893a5eb81d 100644 --- a/tests/Fixtures/app/AppKernel.php +++ b/tests/Fixtures/app/AppKernel.php @@ -23,6 +23,7 @@ use Nelmio\ApiDocBundle\NelmioApiDocBundle; use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; +use Symfony\Bundle\MakerBundle\MakerBundle; use Symfony\Bundle\MercureBundle\MercureBundle; use Symfony\Bundle\SecurityBundle\SecurityBundle; use Symfony\Bundle\TwigBundle\TwigBundle; @@ -70,6 +71,7 @@ public function registerBundles(): array new SecurityBundle(), new WebProfilerBundle(), new FriendsOfBehatSymfonyExtensionBundle(), + new MakerBundle(), ]; if (class_exists(DoctrineMongoDBBundle::class)) {