-
-
Notifications
You must be signed in to change notification settings - Fork 933
feat(debug): add a debug resource command #4401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
soyuka
merged 1 commit into
api-platform:main
from
samuelbigard:feat/debug-resource-command
Sep 7, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
src/Bridge/Symfony/Bundle/Command/DebugResourceCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Bridge\Symfony\Bundle\Command; | ||
|
||
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ChoiceQuestion; | ||
use Symfony\Component\VarDumper\Cloner\ClonerInterface; | ||
|
||
final class DebugResourceCommand extends Command | ||
{ | ||
protected static $defaultName = 'debug:api-resource'; | ||
|
||
private $resourceMetadataCollectionFactory; | ||
private $cloner; | ||
private $dumper; | ||
|
||
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, ClonerInterface $cloner, $dumper) | ||
{ | ||
parent::__construct(); | ||
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory; | ||
$this->cloner = $cloner; | ||
$this->dumper = $dumper; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this | ||
->setDescription('Debug API Platform resources') | ||
->addArgument('class', InputArgument::REQUIRED, 'The class you want to debug'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$resourceClass = $input->getArgument('class'); | ||
|
||
$resourceCollection = $this->resourceMetadataCollectionFactory->create($resourceClass); | ||
|
||
if (0 === \count($resourceCollection)) { | ||
$output->writeln(sprintf('<error>No resources found for class %s</error>', $resourceClass)); | ||
|
||
return Command::INVALID; | ||
} | ||
|
||
$shortName = (false !== $pos = strrpos($resourceClass, '\\')) ? substr($resourceClass, $pos + 1) : $resourceClass; | ||
|
||
$helper = $this->getHelper('question'); | ||
|
||
$resources = []; | ||
foreach ($resourceCollection as $resource) { | ||
if ($resource->getUriTemplate()) { | ||
$resources[] = $resource->getUriTemplate(); | ||
continue; | ||
} | ||
|
||
foreach ($resource->getOperations() as $operation) { | ||
if ($operation->getUriTemplate()) { | ||
$resources[] = $operation->getUriTemplate(); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (\count($resourceCollection) > 1) { | ||
$questionResource = new ChoiceQuestion( | ||
sprintf('There are %d resources declared on the class %s, which one do you want to debug ? ', \count($resourceCollection), $shortName).\PHP_EOL, | ||
$resources | ||
); | ||
|
||
$answerResource = $helper->ask($input, $output, $questionResource); | ||
$resourceIndex = array_search($answerResource, $resources, true); | ||
$selectedResource = $resourceCollection[$resourceIndex]; | ||
} else { | ||
$selectedResource = $resourceCollection[0]; | ||
$output->writeln(sprintf('Class %s declares 1 resource.', $shortName).\PHP_EOL); | ||
} | ||
|
||
$operations = ['Debug the resource itself']; | ||
foreach ($selectedResource->getOperations() as $operationName => $operation) { | ||
$operations[] = $operationName; | ||
} | ||
|
||
$questionOperation = new ChoiceQuestion( | ||
sprintf('There are %d operation%s declared on the resource, which one do you want to debug ? ', $selectedResource->getOperations()->count(), $selectedResource->getOperations()->count() > 1 ? 's' : '').\PHP_EOL, | ||
$operations | ||
); | ||
|
||
$answerOperation = $helper->ask($input, $output, $questionOperation); | ||
if ('Debug the resource itself' === $answerOperation) { | ||
$this->dumper->dump($this->cloner->cloneVar($selectedResource)); | ||
$output->writeln('Successfully dumped the selected resource'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
$this->dumper->dump($this->cloner->cloneVar($resourceCollection->getOperation($answerOperation))); | ||
$output->writeln('Successfully dumped the selected operation'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
tests/Bridge/Symfony/Bundle/Command/DebugResourceCommandTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Bridge\Symfony\Bundle\Command; | ||
|
||
use ApiPlatform\Bridge\Symfony\Bundle\Command\DebugResourceCommand; | ||
use ApiPlatform\Core\Tests\ProphecyTrait; | ||
use ApiPlatform\Exception\ResourceClassNotFoundException; | ||
use ApiPlatform\Metadata\Resource\Factory\AttributesResourceMetadataCollectionFactory; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Symfony\Component\VarDumper\Cloner\VarCloner; | ||
use Symfony\Component\VarDumper\Dumper\CliDumper; | ||
use Symfony\Component\VarDumper\Dumper\DataDumperInterface; | ||
|
||
class DebugResourceCommandTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private function getCommandTester(DataDumperInterface $dumper = null): CommandTester | ||
{ | ||
$application = new Application(); | ||
$application->setCatchExceptions(false); | ||
$application->setAutoExit(false); | ||
|
||
$application->add(new DebugResourceCommand(new AttributesResourceMetadataCollectionFactory(), new VarCloner(), $dumper ?? new CliDumper())); | ||
|
||
$command = $application->find('debug:api-resource'); | ||
|
||
return new CommandTester($command); | ||
} | ||
|
||
/** | ||
* @requires PHP 8.0 | ||
*/ | ||
public function testDebugResource() | ||
{ | ||
$varDumper = $this->prophesize(DataDumperInterface::class); | ||
$commandTester = $this->getCommandTester($varDumper->reveal()); | ||
$varDumper->dump(Argument::any())->shouldBeCalledTimes(1); | ||
$commandTester->setInputs(['0', '0']); | ||
$commandTester->execute([ | ||
'class' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeResource', | ||
]); | ||
|
||
$this->assertStringContainsString('Successfully dumped the selected resource', $commandTester->getDisplay()); | ||
} | ||
|
||
/** | ||
* @requires PHP 8.0 | ||
*/ | ||
public function testDebugOperation() | ||
{ | ||
$varDumper = $this->prophesize(DataDumperInterface::class); | ||
$commandTester = $this->getCommandTester($varDumper->reveal()); | ||
$varDumper->dump(Argument::any())->shouldBeCalledTimes(1); | ||
$commandTester->setInputs(['0', '1']); | ||
|
||
$commandTester->execute([ | ||
'class' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeResource', | ||
]); | ||
|
||
$this->assertStringContainsString('Successfully dumped the selected operation', $commandTester->getDisplay()); | ||
} | ||
|
||
/** | ||
* @requires PHP 8.0 | ||
*/ | ||
public function testWithOnlyOneResource() | ||
{ | ||
$varDumper = $this->prophesize(DataDumperInterface::class); | ||
$commandTester = $this->getCommandTester($varDumper->reveal()); | ||
$varDumper->dump(Argument::any())->shouldBeCalledTimes(1); | ||
$commandTester->setInputs(['1']); | ||
|
||
$commandTester->execute([ | ||
'class' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\AlternateResource', | ||
]); | ||
|
||
$this->assertStringContainsString('declares 1 resource', $commandTester->getDisplay()); | ||
$this->assertStringContainsString('Successfully dumped the selected operation', $commandTester->getDisplay()); | ||
} | ||
|
||
/** | ||
* @requires PHP 8.0 | ||
*/ | ||
public function testExecuteWithNotExistingClass() | ||
{ | ||
$this->expectException(ResourceClassNotFoundException::class); | ||
$commandTester = $this->getCommandTester(); | ||
|
||
$commandTester->execute([ | ||
'class' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\NotExisting', | ||
]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.