Skip to content

Commit

Permalink
Add tests for the new Maker commands
Browse files Browse the repository at this point in the history
  • Loading branch information
antograssiot committed Nov 28, 2020
1 parent b01f809 commit 63cead2
Show file tree
Hide file tree
Showing 7 changed files with 486 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@
},
"autoload-dev": {
"psr-4": {
"ApiPlatform\\Core\\Tests\\": "tests/"
"ApiPlatform\\Core\\Tests\\": "tests/",
"App\\": "tests/Fixtures/app/var/tmp/src/"
}
},
"config": {
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Symfony/Maker/MakeDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. <fg=yellow>AwesomeDataProvider</>)')
->addArgument('resource-class', InputArgument::OPTIONAL, 'Choose a Resource class')
->addOption('item-only', null, InputOption::VALUE_NONE, 'Generate only an item data provider')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class <?= $class_name ?> implements ContextAwareDataPersisterInterface
*/
public function supports($data, array $context = []): bool
{
<?php if ($resource_class !== null): ?>
<?php if (null !== $resource_class): ?>
return $data instanceof <?= $resource_class ?>::class; // Add your custom conditions here
<?php else : ?>
return false; // Add your custom conditions here
Expand All @@ -24,7 +24,13 @@ public function supports($data, array $context = []): bool
/**
* {@inheritdoc}
*/
public function persist($data, array $context = [])
public function persist($data, array $context = [])<?php
if (null !==$resource_class) {
echo ": $resource_class";
} elseif (\PHP_VERSION_ID >= 70200) {
echo ': object';
}?>

{
// call your persistence layer to save $data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class <?= $class_name ?> implements <?= ($generate_collection ? 'ContextAw
{
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
<?php if ($resource_class !== null): ?>
<?php if (null !== $resource_class): ?>
return <?= $resource_class ?>::class === $resourceClass; // Add your custom conditions here
<?php else : ?>
return false; // Add your custom conditions here
Expand All @@ -38,7 +38,12 @@ public function getCollection(string $resourceClass, string $operationName = nul
/**
* {@inheritdoc}
*/
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])<?= $resource_class ? ": ?$resource_class": (\PHP_VERSION_ID >= 70200 ? ': ?object' : '') ?>
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])<?php
if (null !== $resource_class) {
echo ": ?$resource_class";
} elseif (\PHP_VERSION_ID >= 70200) {
echo ': ?object';
}?>

{
// Retrieve the item from somewhere then return it or null if not found
Expand Down
167 changes: 167 additions & 0 deletions tests/Bridge/Symfony/Maker/MakeDataPersisterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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(<<<EOF
Next: Open your new data persister class and start customizing it.
Find the documentation at https://api-platform.com/docs/core/data-persisters/
EOF
, $display);
}

public function dataPersisterProvider(): Generator
{
$expected = <<<'EOF'
<?php
namespace App\DataPersister;
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
final class CustomDataPersister implements ContextAwareDataPersisterInterface
{
/**
* {@inheritdoc}
*/
public function supports($data, array $context = []): bool
{
return false; // Add your custom conditions here
}
/**
* {@inheritdoc}
*/
public function persist($data, array $context = []): object
{
// call your persistence layer to save $data
return $data;
}
/**
* {@inheritdoc}
*/
public function remove($data, array $context = [])
{
// call your persistence layer to delete $data
}
}

EOF;
yield 'Generate data persister without resource class' => [
[],
['CustomDataPersister', ''],
\PHP_VERSION_ID >= 70200 ? $expected : str_replace(': object', '', $expected),
];

$expected = <<<'EOF'
<?php
namespace App\DataPersister;
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
final class CustomDataPersister implements ContextAwareDataPersisterInterface
{
/**
* {@inheritdoc}
*/
public function supports($data, array $context = []): bool
{
return $data instanceof Dummy::class; // Add your custom conditions here
}
/**
* {@inheritdoc}
*/
public function persist($data, array $context = []): Dummy
{
// call your persistence layer to save $data
return $data;
}
/**
* {@inheritdoc}
*/
public function remove($data, array $context = [])
{
// call your persistence layer to delete $data
}
}

EOF;
yield 'Generate data persister with resource class' => [
[],
['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);
}
}
Loading

0 comments on commit 63cead2

Please sign in to comment.