Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions src/Bridge/Symfony/Bundle/Command/DebugResourceCommand.php
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;
}
}
11 changes: 11 additions & 0 deletions src/Core/Bridge/Symfony/Bundle/Resources/config/debug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,16 @@
<service id="debug.api_platform.data_persister" class="ApiPlatform\Core\Bridge\Symfony\Bundle\DataPersister\TraceableChainDataPersister" decorates="api_platform.data_persister">
<argument type="service" id="debug.api_platform.data_persister.inner" />
</service>

<service id="debug.var_dumper.cloner" class="Symfony\Component\VarDumper\Cloner\VarCloner" />

<service id="debug.var_dumper.cli_dumper" class="Symfony\Component\VarDumper\Dumper\CliDumper" />

<service id="debug.api_platform.debug_resource.command" class="ApiPlatform\Bridge\Symfony\Bundle\Command\DebugResourceCommand">
<tag name="console.command" />
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory" />
<argument type="service" id="debug.var_dumper.cloner" />
<argument type="service" id="debug.var_dumper.cli_dumper" />
</service>
</services>
</container>
108 changes: 108 additions & 0 deletions tests/Bridge/Symfony/Bundle/Command/DebugResourceCommandTest.php
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',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ public function testEnableProfilerWithDebug()
$containerBuilderProphecy->setDefinition('debug.api_platform.item_data_provider', Argument::type(Definition::class))->shouldBeCalled();
$containerBuilderProphecy->setDefinition('debug.api_platform.subresource_data_provider', Argument::type(Definition::class))->shouldBeCalled();
$containerBuilderProphecy->setDefinition('debug.api_platform.data_persister', Argument::type(Definition::class))->shouldBeCalled();
$containerBuilderProphecy->setDefinition('debug.api_platform.debug_resource.command', Argument::type(Definition::class))->shouldBeCalled();
$containerBuilderProphecy->setDefinition('debug.var_dumper.cloner', Argument::type(Definition::class))->shouldBeCalled();
$containerBuilderProphecy->setDefinition('debug.var_dumper.cli_dumper', Argument::type(Definition::class))->shouldBeCalled();
$containerBuilder = $containerBuilderProphecy->reveal();

$config = self::DEFAULT_CONFIG;
Expand Down Expand Up @@ -794,6 +797,9 @@ public function testKeepCachePoolClearerCacheWarmerWithDebug()
$containerBuilderProphecy->setDefinition('debug.api_platform.item_data_provider', Argument::type(Definition::class))->will(function () {});
$containerBuilderProphecy->setDefinition('debug.api_platform.subresource_data_provider', Argument::type(Definition::class))->will(function () {});
$containerBuilderProphecy->setDefinition('debug.api_platform.data_persister', Argument::type(Definition::class))->will(function () {});
$containerBuilderProphecy->setDefinition('debug.api_platform.debug_resource.command', Argument::type(Definition::class))->will(function () {});
$containerBuilderProphecy->setDefinition('debug.var_dumper.cloner', Argument::type(Definition::class))->shouldBeCalled();
$containerBuilderProphecy->setDefinition('debug.var_dumper.cli_dumper', Argument::type(Definition::class))->shouldBeCalled();

$containerBuilder = $containerBuilderProphecy->reveal();

Expand Down