Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
Allow "friendsofsymfony/rest-bundle:^3.0"
Browse files Browse the repository at this point in the history
  • Loading branch information
wbloszyk committed Jul 24, 2020
1 parent 099f21a commit caf45e4
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 21 deletions.
15 changes: 9 additions & 6 deletions composer.json
Expand Up @@ -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.",
Expand Down
25 changes: 14 additions & 11 deletions src/Controller/Api/MessageController.php
Expand Up @@ -13,19 +13,20 @@

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;
use Sonata\NotificationBundle\Model\MessageInterface;
use Sonata\NotificationBundle\Model\MessageManagerInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
* @author Hugo Briand <briand@ekino.com>
*
* @Route(defaults={"_format": "json"}, requirements={"_format": "json|xml|html"})
*/
class MessageController
{
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/config/routing/api.xml
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<routes xmlns="http://friendsofsymfony.github.com/schema/rest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://friendsofsymfony.github.com/schema/rest https://raw.github.com/FriendsOfSymfony/FOSRestBundle/master/Resources/config/schema/routing/rest_routing-1.0.xsd">
<import type="rest" resource="Sonata\NotificationBundle\Controller\Api\MessageController" name-prefix="sonata_api_notification_message_"/>
<routes xmlns="http://symfony.com/schema/routing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing https://symfony.com/schema/routing/routing-1.0.xsd">
<import type="annotation" resource="Sonata\NotificationBundle\Controller\Api\MessageController" name-prefix="sonata_api_notification_message_"/>
</routes>
134 changes: 134 additions & 0 deletions tests/App/AppKernel.php
@@ -0,0 +1,134 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* 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/';
}
}
7 changes: 7 additions & 0 deletions 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"
4 changes: 2 additions & 2 deletions tests/Controller/Api/MessageControllerTest.php
Expand Up @@ -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;
Expand All @@ -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([]);

Expand Down
52 changes: 52 additions & 0 deletions tests/Functional/RoutingTest.php
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* 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 <phansys@gmail.com>
*/
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;
}
}

0 comments on commit caf45e4

Please sign in to comment.