From caf45e4b838c83cefb7ddfeee2f0aebfc980b88f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20B=C5=82oszyk?= Date: Fri, 24 Jul 2020 10:39:09 +0200 Subject: [PATCH] Allow "friendsofsymfony/rest-bundle:^3.0" --- composer.json | 15 +- src/Controller/Api/MessageController.php | 25 ++-- src/Resources/config/routing/api.xml | 4 +- tests/App/AppKernel.php | 134 ++++++++++++++++++ tests/App/config/routes.yaml | 7 + .../Controller/Api/MessageControllerTest.php | 4 +- tests/Functional/RoutingTest.php | 52 +++++++ 7 files changed, 220 insertions(+), 21 deletions(-) create mode 100644 tests/App/AppKernel.php create mode 100644 tests/App/config/routes.yaml create mode 100644 tests/Functional/RoutingTest.php diff --git a/composer.json b/composer.json index 9a882bf4..0f62a8d1 100644 --- a/composer.json +++ b/composer.json @@ -40,21 +40,24 @@ "zendframework/zenddiagnostics": "^1.0" }, "conflict": { - "friendsofsymfony/rest-bundle": "<2.1 || >=3.0", + "friendsofsymfony/rest-bundle": "<2.1", "jms/serializer": "<0.13", "sonata-project/admin-bundle": "<3.1", "sonata-project/core-bundle": "<3.20" }, "require-dev": { "enqueue/amqp-lib": "^0.8", - "friendsofsymfony/rest-bundle": "^2.1", + "friendsofsymfony/rest-bundle": "^2.1 || ^3.0", "guzzlehttp/guzzle": "^3.8", "jms/serializer-bundle": "^2.0 || ^3.0", - "liip/monitor-bundle": "^2.0", - "nelmio/api-doc-bundle": "^2.4", - "sonata-project/doctrine-orm-admin-bundle": "^3.4", + "liip/monitor-bundle": "^2.6", + "nelmio/api-doc-bundle": "^2.13", + "sensio/framework-extra-bundle": "^5.5", + "sonata-project/doctrine-orm-admin-bundle": "^3.19", "swiftmailer/swiftmailer": "^5.0 || ^6.0", - "symfony/phpunit-bridge": "^5.0" + "symfony/browser-kit": "^4.4 || ^5.1", + "symfony/phpunit-bridge": "^5.1", + "symfony/yaml": "^4.4" }, "suggest": { "guzzlehttp/guzzle": "If you want to get a status report when using the rabbitMQ backend.", diff --git a/src/Controller/Api/MessageController.php b/src/Controller/Api/MessageController.php index 9b2a1583..744ab202 100644 --- a/src/Controller/Api/MessageController.php +++ b/src/Controller/Api/MessageController.php @@ -13,9 +13,7 @@ namespace Sonata\NotificationBundle\Controller\Api; -use FOS\RestBundle\Controller\Annotations\QueryParam; -use FOS\RestBundle\Controller\Annotations\Route; -use FOS\RestBundle\Controller\Annotations\View; +use FOS\RestBundle\Controller\Annotations as Rest; use FOS\RestBundle\Request\ParamFetcherInterface; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Sonata\DatagridBundle\Pager\PagerInterface; @@ -23,9 +21,12 @@ use Sonata\NotificationBundle\Model\MessageManagerInterface; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Routing\Annotation\Route; /** * @author Hugo Briand + * + * @Route(defaults={"_format": "json"}, requirements={"_format": "json|xml|html"}) */ class MessageController { @@ -53,13 +54,15 @@ public function __construct(MessageManagerInterface $messageManager, FormFactory * output={"class"="Sonata\DatagridBundle\Pager\PagerInterface", "groups"={"sonata_api_read"}} * ) * - * @QueryParam(name="page", requirements="\d+", default="1", description="Page for message list pagination") - * @QueryParam(name="count", requirements="\d+", default="10", description="Number of messages by page") - * @QueryParam(name="type", nullable=true, description="Message type filter") - * @QueryParam(name="state", requirements="\d+", strict=true, nullable=true, description="Message status filter") - * @QueryParam(name="orderBy", map=true, requirements="ASC|DESC", nullable=true, strict=true, description="Query groups order by clause (key is field, value is direction)") + * @Rest\Get("/messages.{_format}", name="get_messages") + * + * @Rest\QueryParam(name="page", requirements="\d+", default="1", description="Page for message list pagination") + * @Rest\QueryParam(name="count", requirements="\d+", default="10", description="Number of messages by page") + * @Rest\QueryParam(name="type", nullable=true, description="Message type filter") + * @Rest\QueryParam(name="state", requirements="\d+", strict=true, nullable=true, description="Message status filter") + * @Rest\QueryParam(name="orderBy", map=true, requirements="ASC|DESC", nullable=true, strict=true, description="Query groups order by clause (key is field, value is direction)") * - * @View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) + * @Rest\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) * * @return PagerInterface */ @@ -102,9 +105,9 @@ public function getMessagesAction(ParamFetcherInterface $paramFetcher) * } * ) * - * @View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) + * @Rest\Post("/messages.{_format}", name="post_message") * - * @Route(requirements={"_format"="json|xml"}) + * @Rest\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) * * @param Request $request A Symfony request * diff --git a/src/Resources/config/routing/api.xml b/src/Resources/config/routing/api.xml index 9f4f1da5..313483e9 100644 --- a/src/Resources/config/routing/api.xml +++ b/src/Resources/config/routing/api.xml @@ -1,4 +1,4 @@ - - + + diff --git a/tests/App/AppKernel.php b/tests/App/AppKernel.php new file mode 100644 index 00000000..af7289f7 --- /dev/null +++ b/tests/App/AppKernel.php @@ -0,0 +1,134 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\NotificationBundle\Tests\App; + +use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; +use FOS\RestBundle\FOSRestBundle; +use JMS\SerializerBundle\JMSSerializerBundle; +use Nelmio\ApiDocBundle\NelmioApiDocBundle; +use Sonata\NotificationBundle\SonataNotificationBundle; +use Symfony\Bundle\FrameworkBundle\FrameworkBundle; +use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; +use Symfony\Bundle\SecurityBundle\SecurityBundle; +use Symfony\Bundle\TwigBundle\TwigBundle; +use Symfony\Component\Config\Loader\LoaderInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\HttpKernel\Kernel; +use Symfony\Component\Routing\RouteCollectionBuilder; + +final class AppKernel extends Kernel +{ + use MicroKernelTrait; + + public function __construct() + { + parent::__construct('test', false); + } + + public function registerBundles() + { + return [ + new FrameworkBundle(), + new SecurityBundle(), + new TwigBundle(), + new FOSRestBundle(), + new SonataNotificationBundle(), + new JMSSerializerBundle(), + new DoctrineBundle(), + new NelmioApiDocBundle(), + ]; + } + + public function getCacheDir(): string + { + return $this->getBaseDir().'cache'; + } + + public function getLogDir(): string + { + return $this->getBaseDir().'log'; + } + + public function getProjectDir(): string + { + return __DIR__; + } + + protected function configureRoutes(RouteCollectionBuilder $routes) + { + $routes->import($this->getProjectDir().'/config/routes.yaml'); + } + + protected function configureContainer(ContainerBuilder $containerBuilder, LoaderInterface $loader): void + { + $containerBuilder->register('templating')->setSynthetic(true); + $containerBuilder->register('templating.locator')->setSynthetic(true); + $containerBuilder->register('templating.name_parser')->setSynthetic(true); + $containerBuilder->register('mailer')->setSynthetic(true); + + $containerBuilder->loadFromExtension('framework', [ + 'secret' => '50n474.U53r', + 'session' => [ + 'handler_id' => 'session.handler.native_file', + 'storage_id' => 'session.storage.mock_file', + 'name' => 'MOCKSESSID', + ], + 'translator' => null, + 'validation' => [ + 'enabled' => true, + ], + 'form' => [ + 'enabled' => true, + ], + 'assets' => null, + 'test' => true, + 'profiler' => [ + 'enabled' => true, + 'collect' => false, + ], + ]); + + $containerBuilder->loadFromExtension('security', [ + 'firewalls' => ['api' => ['anonymous' => true]], + 'providers' => ['in_memory' => ['memory' => null]], + ]); + + $containerBuilder->loadFromExtension('twig', [ + 'strict_variables' => '%kernel.debug%', + 'exception_controller' => null, + ]); + + $containerBuilder->loadFromExtension('doctrine', [ + 'dbal' => [ + 'connections' => [ + 'default' => [ + 'driver' => 'pdo_sqlite', + ], + ], + ], + 'orm' => [ + 'default_entity_manager' => 'default', + ], + ]); + + $containerBuilder->loadFromExtension('fos_rest', [ + 'param_fetcher_listener' => true, + ]); + } + + private function getBaseDir(): string + { + return sys_get_temp_dir().'/sonata-notification-bundle/var/'; + } +} diff --git a/tests/App/config/routes.yaml b/tests/App/config/routes.yaml new file mode 100644 index 00000000..fb318861 --- /dev/null +++ b/tests/App/config/routes.yaml @@ -0,0 +1,7 @@ +#NelmioApiDocBundle: +# prefix: /api/doc +# resource: "@NelmioApiDocBundle/Resources/config/routing.yml" + +sonata_api_notification: + prefix: /api/notification + resource: "@SonataNotificationBundle/Resources/config/routing/api.xml" diff --git a/tests/Controller/Api/MessageControllerTest.php b/tests/Controller/Api/MessageControllerTest.php index 36ab139f..1d57ae43 100644 --- a/tests/Controller/Api/MessageControllerTest.php +++ b/tests/Controller/Api/MessageControllerTest.php @@ -13,7 +13,7 @@ namespace Sonata\NotificationBundle\Tests\Controller\Api; -use FOS\RestBundle\Request\ParamFetcher; +use FOS\RestBundle\Request\ParamFetcherInterface; use PHPUnit\Framework\TestCase; use Sonata\NotificationBundle\Controller\Api\MessageController; use Sonata\NotificationBundle\Model\MessageInterface; @@ -34,7 +34,7 @@ public function testGetMessagesAction(): void $messageManager = $this->createMock(MessageManagerInterface::class); $messageManager->expects($this->once())->method('getPager')->willReturn([]); - $paramFetcher = $this->createMock(ParamFetcher::class); + $paramFetcher = $this->createMock(ParamFetcherInterface::class); $paramFetcher->expects($this->exactly(3))->method('get'); $paramFetcher->expects($this->once())->method('all')->willReturn([]); diff --git a/tests/Functional/RoutingTest.php b/tests/Functional/RoutingTest.php new file mode 100644 index 00000000..2898f4a2 --- /dev/null +++ b/tests/Functional/RoutingTest.php @@ -0,0 +1,52 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\NotificationBundle\Tests\Functional\Routing; + +use Sonata\NotificationBundle\Tests\App\AppKernel; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; + +/** + * @author Javier Spagnoletti + */ +final class RoutingTest extends WebTestCase +{ + /** + * @group legacy + * + * @dataProvider getRoutes + */ + public function testRoutes(string $name, string $path, array $methods): void + { + $client = static::createClient(); + $router = $client->getContainer()->get('router'); + + $route = $router->getRouteCollection()->get($name); + + $this->assertNotNull($route); + $this->assertSame($path, $route->getPath()); + $this->assertEmpty(array_diff($methods, $route->getMethods())); + } + + public function getRoutes(): iterable + { + //yield ['nelmio_api_doc_index', '/api/doc/{view}', ['GET']]; + yield ['sonata_api_notification_message_get_messages', '/api/notification/messages.{_format}', ['GET']]; + yield ['sonata_api_notification_message_post_message', '/api/notification/messages.{_format}', ['POST']]; + } + + protected static function getKernelClass(): string + { + return AppKernel::class; + } +}