From b7dbb8fad61b237ae2e27b14c8d46dc3070b686d Mon Sep 17 00:00:00 2001 From: Alan Poulain Date: Sun, 10 Feb 2019 20:33:14 +0100 Subject: [PATCH] Request events --- composer.json | 2 +- .../EventListener/SwaggerUiListener.php | 23 ++++- .../EventSubscriber/EventDispatcher.php | 88 +++++++++++++++++ .../Symfony/Bundle/Resources/config/api.xml | 15 ++- .../Bundle/Resources/config/jsonapi.xml | 8 +- .../Bundle/Resources/config/security.xml | 2 +- .../Bundle/Resources/config/swagger-ui.xml | 2 +- .../Bundle/Resources/config/validator.xml | 2 +- src/Event/DeserializeEvent.php | 21 ++++ src/Event/Event.php | 51 ++++++++++ src/Event/EventInterface.php | 28 ++++++ src/Event/FormatAddEvent.php | 21 ++++ src/Event/PostDeserializeEvent.php | 21 ++++ src/Event/PostReadEvent.php | 21 ++++ src/Event/PreDeserializeEvent.php | 21 ++++ src/Event/PreReadEvent.php | 21 ++++ src/Event/QueryParameterValidateEvent.php | 21 ++++ src/Event/ReadEvent.php | 21 ++++ src/EventListener/AddFormatListener.php | 26 ++++- src/EventListener/DeserializeListener.php | 23 ++++- src/EventListener/ReadListener.php | 27 ++++- src/Events.php | 43 ++++++++ src/Filter/QueryParameterValidateListener.php | 21 +++- .../TransformFieldsetsParametersListener.php | 21 +++- .../TransformFilteringParametersListener.php | 22 ++++- .../TransformPaginationParametersListener.php | 21 +++- .../TransformSortingParametersListener.php | 21 +++- .../EventListener/DenyAccessListener.php | 25 ++++- .../ApiPlatformExtensionTest.php | 1 + .../EventListener/SwaggerUiListenerTest.php | 23 ++++- tests/EventListener/AddFormatListenerTest.php | 98 ++++++++++-------- .../EventListener/DeserializeListenerTest.php | 91 ++++++++++------- tests/EventListener/ReadListenerTest.php | 99 +++++++++++-------- .../QueryParameterValidateListenerTest.php | 52 +++++++--- ...ansformFieldsetsParametersListenerTest.php | 59 ++++++----- ...ansformFilteringParametersListenerTest.php | 43 +++++--- ...nsformPaginationParametersListenerTest.php | 43 +++++--- ...TransformSortingParametersListenerTest.php | 43 +++++--- .../EventListener/DenyAccessListenerTest.php | 78 +++++++++------ 39 files changed, 1014 insertions(+), 255 deletions(-) create mode 100644 src/Bridge/Symfony/Bundle/EventSubscriber/EventDispatcher.php create mode 100644 src/Event/DeserializeEvent.php create mode 100644 src/Event/Event.php create mode 100644 src/Event/EventInterface.php create mode 100644 src/Event/FormatAddEvent.php create mode 100644 src/Event/PostDeserializeEvent.php create mode 100644 src/Event/PostReadEvent.php create mode 100644 src/Event/PreDeserializeEvent.php create mode 100644 src/Event/PreReadEvent.php create mode 100644 src/Event/QueryParameterValidateEvent.php create mode 100644 src/Event/ReadEvent.php create mode 100644 src/Events.php diff --git a/composer.json b/composer.json index bb606055d4b..dc4ba5dbff2 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "doctrine/inflector": "^1.0", "psr/cache": "^1.0", "psr/container": "^1.0", + "symfony/event-dispatcher": "^3.4 || ^4.0", "symfony/http-foundation": "^3.4 || ^4.0", "symfony/http-kernel": "^3.4 || ^4.0", "symfony/property-access": "^3.4 || ^4.0", @@ -61,7 +62,6 @@ "symfony/dependency-injection": "^3.4 || ^4.0", "symfony/doctrine-bridge": "^3.4 || ^4.0", "symfony/dom-crawler": "^3.4 || ^4.0", - "symfony/event-dispatcher": "^3.4 || ^4.0", "symfony/expression-language": "^3.4 || ^4.0", "symfony/finder": "^3.4 || ^4.0", "symfony/form": "^3.4 || ^4.0", diff --git a/src/Bridge/Symfony/Bundle/EventListener/SwaggerUiListener.php b/src/Bridge/Symfony/Bundle/EventListener/SwaggerUiListener.php index 32c9320813b..d465f066327 100644 --- a/src/Bridge/Symfony/Bundle/EventListener/SwaggerUiListener.php +++ b/src/Bridge/Symfony/Bundle/EventListener/SwaggerUiListener.php @@ -13,16 +13,37 @@ namespace ApiPlatform\Core\Bridge\Symfony\Bundle\EventListener; +use ApiPlatform\Core\Event\EventInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; final class SwaggerUiListener { /** * Sets SwaggerUiAction as controller if the requested format is HTML. + * + * @deprecated since version 2.5, to be removed in 3.0. */ public function onKernelRequest(GetResponseEvent $event) { - $request = $event->getRequest(); + @trigger_error(sprintf('The method %s() is deprecated since 2.5 and will be removed in 3.0.', __METHOD__), E_USER_DEPRECATED); + + $this->handleEvent($event); + } + + /** + * Sets SwaggerUiAction as controller if the requested format is HTML. + */ + public function handleEvent(/*EventInterface */$event) + { + if ($event instanceof EventInterface) { + $request = $event->getContext()['request']; + } elseif ($event instanceof GetResponseEvent) { + @trigger_error(sprintf('Passing an instance of "%s" as argument of "%s" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "%s" instead.', GetResponseEvent::class, __METHOD__, EventInterface::class), E_USER_DEPRECATED); + + $request = $event->getRequest(); + } else { + return; + } if ( 'html' !== $request->getRequestFormat('') || (!$request->attributes->has('_api_resource_class') && !$request->attributes->has('_api_respond')) diff --git a/src/Bridge/Symfony/Bundle/EventSubscriber/EventDispatcher.php b/src/Bridge/Symfony/Bundle/EventSubscriber/EventDispatcher.php new file mode 100644 index 00000000000..8562128785b --- /dev/null +++ b/src/Bridge/Symfony/Bundle/EventSubscriber/EventDispatcher.php @@ -0,0 +1,88 @@ + + * + * 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\Core\Bridge\Symfony\Bundle\EventSubscriber; + +use ApiPlatform\Core\Event\DeserializeEvent; +use ApiPlatform\Core\Event\FormatAddEvent; +use ApiPlatform\Core\Event\PostDeserializeEvent; +use ApiPlatform\Core\Event\PostReadEvent; +use ApiPlatform\Core\Event\PreDeserializeEvent; +use ApiPlatform\Core\Event\PreReadEvent; +use ApiPlatform\Core\Event\QueryParameterValidateEvent; +use ApiPlatform\Core\Event\ReadEvent; +use ApiPlatform\Core\Events; +use Symfony\Component\EventDispatcher\Event; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\KernelEvents; + +/** + * @internal + * + * @author Alan Poulain + */ +final class EventDispatcher implements EventSubscriberInterface +{ + private const INTERNAL_EVENTS_CONFIGURATION = [ + KernelEvents::REQUEST => [ + QueryParameterValidateEvent::class => Events::QUERY_PARAMETER_VALIDATE, + FormatAddEvent::class => Events::FORMAT_ADD, + + PreReadEvent::class => Events::PRE_READ, + ReadEvent::class => Events::READ, + PostReadEvent::class => Events::POST_READ, + + PreDeserializeEvent::class => Events::PRE_DESERIALIZE, + DeserializeEvent::class => Events::DESERIALIZE, + PostDeserializeEvent::class => Events::POST_DESERIALIZE, + ], + ]; + + private $dispatcher; + + public function __construct(EventDispatcherInterface $dispatcher) + { + $this->dispatcher = $dispatcher; + } + + public static function getSubscribedEvents(): array + { + return [ + KernelEvents::REQUEST => 'dispatch', + ]; + } + + public function dispatch(Event $event, string $eventName): void + { + $internalEventData = null; + $internalEventContext = []; + + switch (true) { + case $event instanceof GetResponseEvent: + $internalEventContext = ['request' => $event->getRequest()]; + } + + foreach ($this->getInternalEventData($eventName) as $internalEventClass => $internalEventName) { + $internalEvent = new $internalEventClass($internalEventData, $internalEventContext); + + $this->dispatcher->dispatch($internalEventName, $internalEvent); + } + } + + private function getInternalEventData(string $eventName): \Generator + { + yield from self::INTERNAL_EVENTS_CONFIGURATION[$eventName]; + } +} diff --git a/src/Bridge/Symfony/Bundle/Resources/config/api.xml b/src/Bridge/Symfony/Bundle/Resources/config/api.xml index b9a7824fe15..75adeb606fd 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/api.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/api.xml @@ -138,14 +138,21 @@ + + + + + + + + - - + @@ -155,7 +162,7 @@ - + @@ -171,7 +178,7 @@ - + diff --git a/src/Bridge/Symfony/Bundle/Resources/config/jsonapi.xml b/src/Bridge/Symfony/Bundle/Resources/config/jsonapi.xml index 5d71ec29369..0ea2b8b9f95 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/jsonapi.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/jsonapi.xml @@ -63,22 +63,22 @@ - + %api_platform.collection.order_parameter_name% - + - + - + diff --git a/src/Bridge/Symfony/Bundle/Resources/config/security.xml b/src/Bridge/Symfony/Bundle/Resources/config/security.xml index e04314ff5fc..91bde4e262a 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/security.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/security.xml @@ -21,7 +21,7 @@ - + diff --git a/src/Bridge/Symfony/Bundle/Resources/config/swagger-ui.xml b/src/Bridge/Symfony/Bundle/Resources/config/swagger-ui.xml index 753184f1fa2..be8abf92878 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/swagger-ui.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/swagger-ui.xml @@ -7,7 +7,7 @@ - + diff --git a/src/Bridge/Symfony/Bundle/Resources/config/validator.xml b/src/Bridge/Symfony/Bundle/Resources/config/validator.xml index d34dcd8cfe6..60c4f9a62ab 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/validator.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/validator.xml @@ -27,7 +27,7 @@ - + diff --git a/src/Event/DeserializeEvent.php b/src/Event/DeserializeEvent.php new file mode 100644 index 00000000000..f255c337c80 --- /dev/null +++ b/src/Event/DeserializeEvent.php @@ -0,0 +1,21 @@ + + * + * 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\Core\Event; + +/** + * @author Alan Poulain + */ +final class DeserializeEvent extends Event +{ +} diff --git a/src/Event/Event.php b/src/Event/Event.php new file mode 100644 index 00000000000..2f562983523 --- /dev/null +++ b/src/Event/Event.php @@ -0,0 +1,51 @@ + + * + * 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\Core\Event; + +use Symfony\Component\EventDispatcher\Event as BaseEvent; + +/** + * @author Alan Poulain + */ +abstract class Event extends BaseEvent implements EventInterface +{ + private $data; + private $context; + + public function __construct($data, array $context = []) + { + $this->data = $data; + $this->context = $context; + } + + public function getData() + { + return $this->data; + } + + public function setData($data): void + { + $this->data = $data; + } + + public function getContext(): array + { + return $this->context; + } + + public function setContext(array $context): void + { + $this->context = $context; + } +} diff --git a/src/Event/EventInterface.php b/src/Event/EventInterface.php new file mode 100644 index 00000000000..e61e2979e65 --- /dev/null +++ b/src/Event/EventInterface.php @@ -0,0 +1,28 @@ + + * + * 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\Core\Event; + +/** + * @author Alan Poulain + */ +interface EventInterface +{ + public function getData(); + + public function setData($data): void; + + public function getContext(): array; + + public function setContext(array $context): void; +} diff --git a/src/Event/FormatAddEvent.php b/src/Event/FormatAddEvent.php new file mode 100644 index 00000000000..9cc669f19cd --- /dev/null +++ b/src/Event/FormatAddEvent.php @@ -0,0 +1,21 @@ + + * + * 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\Core\Event; + +/** + * @author Alan Poulain + */ +final class FormatAddEvent extends Event +{ +} diff --git a/src/Event/PostDeserializeEvent.php b/src/Event/PostDeserializeEvent.php new file mode 100644 index 00000000000..8ed5eab7c16 --- /dev/null +++ b/src/Event/PostDeserializeEvent.php @@ -0,0 +1,21 @@ + + * + * 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\Core\Event; + +/** + * @author Alan Poulain + */ +final class PostDeserializeEvent extends Event +{ +} diff --git a/src/Event/PostReadEvent.php b/src/Event/PostReadEvent.php new file mode 100644 index 00000000000..0f57273f6ed --- /dev/null +++ b/src/Event/PostReadEvent.php @@ -0,0 +1,21 @@ + + * + * 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\Core\Event; + +/** + * @author Alan Poulain + */ +final class PostReadEvent extends Event +{ +} diff --git a/src/Event/PreDeserializeEvent.php b/src/Event/PreDeserializeEvent.php new file mode 100644 index 00000000000..564aba328c0 --- /dev/null +++ b/src/Event/PreDeserializeEvent.php @@ -0,0 +1,21 @@ + + * + * 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\Core\Event; + +/** + * @author Alan Poulain + */ +final class PreDeserializeEvent extends Event +{ +} diff --git a/src/Event/PreReadEvent.php b/src/Event/PreReadEvent.php new file mode 100644 index 00000000000..36ff15c0cbc --- /dev/null +++ b/src/Event/PreReadEvent.php @@ -0,0 +1,21 @@ + + * + * 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\Core\Event; + +/** + * @author Alan Poulain + */ +final class PreReadEvent extends Event +{ +} diff --git a/src/Event/QueryParameterValidateEvent.php b/src/Event/QueryParameterValidateEvent.php new file mode 100644 index 00000000000..e96441df823 --- /dev/null +++ b/src/Event/QueryParameterValidateEvent.php @@ -0,0 +1,21 @@ + + * + * 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\Core\Event; + +/** + * @author Alan Poulain + */ +final class QueryParameterValidateEvent extends Event +{ +} diff --git a/src/Event/ReadEvent.php b/src/Event/ReadEvent.php new file mode 100644 index 00000000000..ab59fb0b801 --- /dev/null +++ b/src/Event/ReadEvent.php @@ -0,0 +1,21 @@ + + * + * 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\Core\Event; + +/** + * @author Alan Poulain + */ +final class ReadEvent extends Event +{ +} diff --git a/src/EventListener/AddFormatListener.php b/src/EventListener/AddFormatListener.php index 72e53713dda..ff1640f6256 100644 --- a/src/EventListener/AddFormatListener.php +++ b/src/EventListener/AddFormatListener.php @@ -15,6 +15,7 @@ use ApiPlatform\Core\Api\FormatMatcher; use ApiPlatform\Core\Api\FormatsProviderInterface; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\Exception\InvalidArgumentException; use ApiPlatform\Core\Util\RequestAttributesExtractor; use Negotiation\Negotiator; @@ -59,10 +60,33 @@ public function __construct(Negotiator $negotiator, /* FormatsProviderInterface * * @throws NotFoundHttpException * @throws NotAcceptableHttpException + * + * @deprecated since version 2.5, to be removed in 3.0. */ public function onKernelRequest(GetResponseEvent $event) { - $request = $event->getRequest(); + @trigger_error(sprintf('The method %s() is deprecated since 2.5 and will be removed in 3.0.', __METHOD__), E_USER_DEPRECATED); + + $this->handleEvent($event); + } + + /** + * Sets the applicable format to the HttpFoundation Request. + * + * @throws NotFoundHttpException + * @throws NotAcceptableHttpException + */ + public function handleEvent(/*EventInterface */$event) + { + if ($event instanceof EventInterface) { + $request = $event->getContext()['request']; + } elseif ($event instanceof GetResponseEvent) { + @trigger_error(sprintf('Passing an instance of "%s" as argument of "%s" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "%s" instead.', GetResponseEvent::class, __METHOD__, EventInterface::class), E_USER_DEPRECATED); + + $request = $event->getRequest(); + } else { + return; + } if (!$request->attributes->has('_api_resource_class') && !$request->attributes->has('_api_respond') && !$request->attributes->has('_graphql')) { return; } diff --git a/src/EventListener/DeserializeListener.php b/src/EventListener/DeserializeListener.php index 206174fe08d..c547af0afcf 100644 --- a/src/EventListener/DeserializeListener.php +++ b/src/EventListener/DeserializeListener.php @@ -15,6 +15,7 @@ use ApiPlatform\Core\Api\FormatMatcher; use ApiPlatform\Core\Api\FormatsProviderInterface; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\Exception\InvalidArgumentException; use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; use ApiPlatform\Core\Util\RequestAttributesExtractor; @@ -58,10 +59,30 @@ public function __construct(SerializerInterface $serializer, SerializerContextBu /** * Deserializes the data sent in the requested format. + * + * @deprecated since version 2.5, to be removed in 3.0. */ public function onKernelRequest(GetResponseEvent $event) { - $request = $event->getRequest(); + @trigger_error(sprintf('The method %s() is deprecated since 2.5 and will be removed in 3.0.', __METHOD__), E_USER_DEPRECATED); + + $this->handleEvent($event); + } + + /** + * Deserializes the data sent in the requested format. + */ + public function handleEvent(/*EventInterface */$event) + { + if ($event instanceof EventInterface) { + $request = $event->getContext()['request']; + } elseif ($event instanceof GetResponseEvent) { + @trigger_error(sprintf('Passing an instance of "%s" as argument of "%s" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "%s" instead.', GetResponseEvent::class, __METHOD__, EventInterface::class), E_USER_DEPRECATED); + + $request = $event->getRequest(); + } else { + return; + } $method = $request->getMethod(); if ( diff --git a/src/EventListener/ReadListener.php b/src/EventListener/ReadListener.php index 80fb62d439f..275478ceddb 100644 --- a/src/EventListener/ReadListener.php +++ b/src/EventListener/ReadListener.php @@ -17,13 +17,13 @@ use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; use ApiPlatform\Core\DataProvider\OperationDataProviderTrait; use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\Exception\InvalidIdentifierException; use ApiPlatform\Core\Exception\RuntimeException; use ApiPlatform\Core\Identifier\IdentifierConverterInterface; use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; use ApiPlatform\Core\Util\RequestAttributesExtractor; use ApiPlatform\Core\Util\RequestParser; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -51,10 +51,33 @@ public function __construct(CollectionDataProviderInterface $collectionDataProvi * Calls the data provider and sets the data attribute. * * @throws NotFoundHttpException + * + * @deprecated since version 2.5, to be removed in 3.0. */ public function onKernelRequest(GetResponseEvent $event) { - $request = $event->getRequest(); + @trigger_error(sprintf('The method %s() is deprecated since 2.5 and will be removed in 3.0.', __METHOD__), E_USER_DEPRECATED); + + $this->handleEvent($event); + } + + /** + * Calls the data provider and sets the data attribute. + * + * @throws NotFoundHttpException + */ + public function handleEvent(/*EventInterface */$event) + { + if ($event instanceof EventInterface) { + $request = $event->getContext()['request']; + } elseif ($event instanceof GetResponseEvent) { + @trigger_error(sprintf('Passing an instance of "%s" as argument of "%s" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "%s" instead.', GetResponseEvent::class, __METHOD__, EventInterface::class), E_USER_DEPRECATED); + + $request = $event->getRequest(); + } else { + return; + } + if ( !($attributes = RequestAttributesExtractor::extractAttributes($request)) || !$attributes['receive'] diff --git a/src/Events.php b/src/Events.php new file mode 100644 index 00000000000..826133337be --- /dev/null +++ b/src/Events.php @@ -0,0 +1,43 @@ + + * + * 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\Core; + +/** + * @author Alan Poulain + */ +final class Events +{ + public const QUERY_PARAMETER_VALIDATE = 'api_platform.query_parameter_validate'; + public const FORMAT_ADD = 'api_platform.format_add'; + + public const PRE_READ = 'api_platform.pre_read'; + public const READ = 'api_platform.read'; + public const POST_READ = 'api_platform.post_read'; + + public const PRE_DESERIALIZE = 'api_platform.pre_deserialize'; + public const DESERIALIZE = 'api_platform.deserialize'; + public const POST_DESERIALIZE = 'api_platform.post_deserialize'; + + public const PRE_VALIDATE = 'api_platform.pre_validate'; + public const VALIDATE = 'api_platform.validate'; + public const POST_VALIDATE = 'api_platform.post_validate'; + + public const PRE_WRITE = 'api_platform.pre_write'; + public const WRITE = 'api_platform.write'; + public const POST_WRITE = 'api_platform.post_write'; + + public const PRE_SERIALIZE = 'api_platform.pre_serialize'; + public const SERIALIZE = 'api_platform.serialize'; + public const POST_SERIALIZE = 'api_platform.post_serialize'; +} diff --git a/src/Filter/QueryParameterValidateListener.php b/src/Filter/QueryParameterValidateListener.php index ffb919a69fd..7445eee8a2f 100644 --- a/src/Filter/QueryParameterValidateListener.php +++ b/src/Filter/QueryParameterValidateListener.php @@ -14,6 +14,7 @@ namespace ApiPlatform\Core\Filter; use ApiPlatform\Core\Api\FilterLocatorTrait; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\Exception\FilterValidationException; use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; use ApiPlatform\Core\Util\RequestAttributesExtractor; @@ -38,9 +39,27 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa $this->setFilterLocator($filterLocator); } + /** + * @deprecated since version 2.5, to be removed in 3.0. + */ public function onKernelRequest(GetResponseEvent $event) { - $request = $event->getRequest(); + @trigger_error(sprintf('The method %s() is deprecated since 2.5 and will be removed in 3.0.', __METHOD__), E_USER_DEPRECATED); + + $this->handleEvent($event); + } + + public function handleEvent(/*EventInterface */$event) + { + if ($event instanceof EventInterface) { + $request = $event->getContext()['request']; + } elseif ($event instanceof GetResponseEvent) { + @trigger_error(sprintf('Passing an instance of "%s" as argument of "%s" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "%s" instead.', GetResponseEvent::class, __METHOD__, EventInterface::class), E_USER_DEPRECATED); + + $request = $event->getRequest(); + } else { + return; + } if ( !$request->isMethodSafe(false) || !($attributes = RequestAttributesExtractor::extractAttributes($request)) diff --git a/src/JsonApi/EventListener/TransformFieldsetsParametersListener.php b/src/JsonApi/EventListener/TransformFieldsetsParametersListener.php index 941c7d9bf73..79216beee7e 100644 --- a/src/JsonApi/EventListener/TransformFieldsetsParametersListener.php +++ b/src/JsonApi/EventListener/TransformFieldsetsParametersListener.php @@ -13,6 +13,7 @@ namespace ApiPlatform\Core\JsonApi\EventListener; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; @@ -31,9 +32,27 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa $this->resourceMetadataFactory = $resourceMetadataFactory; } + /** + * @deprecated since version 2.5, to be removed in 3.0. + */ public function onKernelRequest(GetResponseEvent $event) { - $request = $event->getRequest(); + @trigger_error(sprintf('The method %s() is deprecated since 2.5 and will be removed in 3.0.', __METHOD__), E_USER_DEPRECATED); + + $this->handleEvent($event); + } + + public function handleEvent(/*EventInterface */$event) + { + if ($event instanceof EventInterface) { + $request = $event->getContext()['request']; + } elseif ($event instanceof GetResponseEvent) { + @trigger_error(sprintf('Passing an instance of "%s" as argument of "%s" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "%s" instead.', GetResponseEvent::class, __METHOD__, EventInterface::class), E_USER_DEPRECATED); + + $request = $event->getRequest(); + } else { + return; + } $includeParameter = $request->query->get('include'); if ( diff --git a/src/JsonApi/EventListener/TransformFilteringParametersListener.php b/src/JsonApi/EventListener/TransformFilteringParametersListener.php index 91ca94248b0..d484209fd74 100644 --- a/src/JsonApi/EventListener/TransformFilteringParametersListener.php +++ b/src/JsonApi/EventListener/TransformFilteringParametersListener.php @@ -13,6 +13,7 @@ namespace ApiPlatform\Core\JsonApi\EventListener; +use ApiPlatform\Core\Event\EventInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; /** @@ -24,9 +25,28 @@ */ final class TransformFilteringParametersListener { + /** + * @deprecated since version 2.5, to be removed in 3.0. + */ public function onKernelRequest(GetResponseEvent $event) { - $request = $event->getRequest(); + @trigger_error(sprintf('The method %s() is deprecated since 2.5 and will be removed in 3.0.', __METHOD__), E_USER_DEPRECATED); + + $this->handleEvent($event); + } + + public function handleEvent(/*EventInterface */$event) + { + if ($event instanceof EventInterface) { + $request = $event->getContext()['request']; + } elseif ($event instanceof GetResponseEvent) { + @trigger_error(sprintf('Passing an instance of "%s" as argument of "%s" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "%s" instead.', GetResponseEvent::class, __METHOD__, EventInterface::class), E_USER_DEPRECATED); + + $request = $event->getRequest(); + } else { + return; + } + if ( 'jsonapi' !== $request->getRequestFormat() || null === ($filters = $request->query->get('filter')) || diff --git a/src/JsonApi/EventListener/TransformPaginationParametersListener.php b/src/JsonApi/EventListener/TransformPaginationParametersListener.php index 7bce05b9640..5a11cd7d76a 100644 --- a/src/JsonApi/EventListener/TransformPaginationParametersListener.php +++ b/src/JsonApi/EventListener/TransformPaginationParametersListener.php @@ -13,6 +13,7 @@ namespace ApiPlatform\Core\JsonApi\EventListener; +use ApiPlatform\Core\Event\EventInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; /** @@ -24,9 +25,27 @@ */ final class TransformPaginationParametersListener { + /** + * @deprecated since version 2.5, to be removed in 3.0. + */ public function onKernelRequest(GetResponseEvent $event) { - $request = $event->getRequest(); + @trigger_error(sprintf('The method %s() is deprecated since 2.5 and will be removed in 3.0.', __METHOD__), E_USER_DEPRECATED); + + $this->handleEvent($event); + } + + public function handleEvent(/*EventInterface */$event) + { + if ($event instanceof EventInterface) { + $request = $event->getContext()['request']; + } elseif ($event instanceof GetResponseEvent) { + @trigger_error(sprintf('Passing an instance of "%s" as argument of "%s" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "%s" instead.', GetResponseEvent::class, __METHOD__, EventInterface::class), E_USER_DEPRECATED); + + $request = $event->getRequest(); + } else { + return; + } if ( 'jsonapi' !== $request->getRequestFormat() || diff --git a/src/JsonApi/EventListener/TransformSortingParametersListener.php b/src/JsonApi/EventListener/TransformSortingParametersListener.php index 7bb5f154a7b..849062c6fc6 100644 --- a/src/JsonApi/EventListener/TransformSortingParametersListener.php +++ b/src/JsonApi/EventListener/TransformSortingParametersListener.php @@ -13,6 +13,7 @@ namespace ApiPlatform\Core\JsonApi\EventListener; +use ApiPlatform\Core\Event\EventInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; /** @@ -31,9 +32,27 @@ public function __construct(string $orderParameterName = 'order') $this->orderParameterName = $orderParameterName; } + /** + * @deprecated since version 2.5, to be removed in 3.0. + */ public function onKernelRequest(GetResponseEvent $event) { - $request = $event->getRequest(); + @trigger_error(sprintf('The method %s() is deprecated since 2.5 and will be removed in 3.0.', __METHOD__), E_USER_DEPRECATED); + + $this->handleEvent($event); + } + + public function handleEvent(/*EventInterface */$event) + { + if ($event instanceof EventInterface) { + $request = $event->getContext()['request']; + } elseif ($event instanceof GetResponseEvent) { + @trigger_error(sprintf('Passing an instance of "%s" as argument of "%s" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "%s" instead.', GetResponseEvent::class, __METHOD__, EventInterface::class), E_USER_DEPRECATED); + + $request = $event->getRequest(); + } else { + return; + } if ( 'jsonapi' !== $request->getRequestFormat() || diff --git a/src/Security/EventListener/DenyAccessListener.php b/src/Security/EventListener/DenyAccessListener.php index f9ee41b8b3d..247a8fbdd10 100644 --- a/src/Security/EventListener/DenyAccessListener.php +++ b/src/Security/EventListener/DenyAccessListener.php @@ -13,6 +13,7 @@ namespace ApiPlatform\Core\Security\EventListener; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; use ApiPlatform\Core\Security\ExpressionLanguage; use ApiPlatform\Core\Security\ResourceAccessChecker; @@ -52,11 +53,31 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa /** * Sets the applicable format to the HttpFoundation Request. * - * @throws AccessDeniedException + * @deprecated since version 2.5, to be removed in 3.0. */ public function onKernelRequest(GetResponseEvent $event) { - $request = $event->getRequest(); + @trigger_error(sprintf('The method %s() is deprecated since 2.5 and will be removed in 3.0.', __METHOD__), E_USER_DEPRECATED); + + $this->handleEvent($event); + } + + /** + * Sets the applicable format to the HttpFoundation Request. + * + * @throws AccessDeniedException + */ + public function handleEvent(/*EventInterface */$event) + { + if ($event instanceof EventInterface) { + $request = $event->getContext()['request']; + } elseif ($event instanceof GetResponseEvent) { + @trigger_error(sprintf('Passing an instance of "%s" as argument of "%s" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "%s" instead.', GetResponseEvent::class, __METHOD__, EventInterface::class), E_USER_DEPRECATED); + + $request = $event->getRequest(); + } else { + return; + } if (!$attributes = RequestAttributesExtractor::extractAttributes($request)) { return; } diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php index d53ea870d12..109dfab8034 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php @@ -739,6 +739,7 @@ private function getPartialContainerBuilderProphecy() 'api_platform.identifiers_extractor', 'api_platform.identifiers_extractor.cached', 'api_platform.item_data_provider', + 'api_platform.dispatcher.internal_events', 'api_platform.listener.exception', 'api_platform.listener.exception.validation', 'api_platform.listener.request.add_format', diff --git a/tests/Bridge/Symfony/Bundle/EventListener/SwaggerUiListenerTest.php b/tests/Bridge/Symfony/Bundle/EventListener/SwaggerUiListenerTest.php index 0b68f7b45c9..2972e43ad77 100644 --- a/tests/Bridge/Symfony/Bundle/EventListener/SwaggerUiListenerTest.php +++ b/tests/Bridge/Symfony/Bundle/EventListener/SwaggerUiListenerTest.php @@ -14,6 +14,7 @@ namespace ApiPlatform\Core\Tests\Bridge\Symfony\Bundle\EventListener; use ApiPlatform\Core\Bridge\Symfony\Bundle\EventListener\SwaggerUiListener; +use ApiPlatform\Core\Event\EventInterface; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\GetResponseEvent; @@ -26,13 +27,13 @@ class SwaggerUiListenerTest extends TestCase /** * @dataProvider getParameters */ - public function testOnKernelRequest(Request $request, string $controller = null) + public function testSetController(Request $request, string $controller = null) { - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $listener = new SwaggerUiListener(); - $listener->onKernelRequest($eventProphecy->reveal()); + $listener->handleEvent($eventProphecy->reveal()); $this->assertEquals($controller, $request->attributes->get('_controller')); } @@ -55,4 +56,18 @@ public function getParameters() [$jsonRequest, null], ]; } + + /** + * @group legacy + * @expectedDeprecation The method ApiPlatform\Core\Bridge\Symfony\Bundle\EventListener\SwaggerUiListener::onKernelRequest() is deprecated since 2.5 and will be removed in 3.0. + * @expectedDeprecation Passing an instance of "Symfony\Component\HttpKernel\Event\GetResponseEvent" as argument of "ApiPlatform\Core\Bridge\Symfony\Bundle\EventListener\SwaggerUiListener::handleEvent" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "ApiPlatform\Core\Event\EventInterface" instead. + */ + public function testLegacyOnKernelRequest() + { + $eventProphecy = $this->prophesize(GetResponseEvent::class); + $eventProphecy->getRequest()->willReturn(new Request()); + + $listener = new SwaggerUiListener(); + $listener->onKernelRequest($eventProphecy->reveal()); + } } diff --git a/tests/EventListener/AddFormatListenerTest.php b/tests/EventListener/AddFormatListenerTest.php index 3c60f0556cf..a3782bb637c 100644 --- a/tests/EventListener/AddFormatListenerTest.php +++ b/tests/EventListener/AddFormatListenerTest.php @@ -14,6 +14,7 @@ namespace ApiPlatform\Core\Tests\EventListener; use ApiPlatform\Core\Api\FormatsProviderInterface; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\EventListener\AddFormatListener; use Negotiation\Negotiator; use PHPUnit\Framework\TestCase; @@ -32,14 +33,14 @@ public function testNoResourceClass() { $request = new Request(); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); $listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); $this->assertNull($request->getRequestFormat(null)); } @@ -49,15 +50,15 @@ public function testSupportedRequestFormat() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); $request->setRequestFormat('xml'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); $formatsProviderProphecy->getFormatsFromAttributes(Argument::any())->willReturn(['xml' => ['text/xml']]); $listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); $this->assertSame('xml', $request->getRequestFormat()); $this->assertSame('text/xml', $request->getMimeType($request->getRequestFormat())); @@ -68,15 +69,15 @@ public function testRespondFlag() $request = new Request([], [], ['_api_respond' => true]); $request->setRequestFormat('xml'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); $formatsProviderProphecy->getFormatsFromAttributes(Argument::any())->willReturn(['xml' => ['text/xml']]); $listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); $this->assertSame('xml', $request->getRequestFormat()); $this->assertSame('text/xml', $request->getMimeType($request->getRequestFormat())); @@ -90,15 +91,15 @@ public function testUnsupportedRequestFormat() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); $request->setRequestFormat('xml'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); $formatsProviderProphecy->getFormatsFromAttributes(Argument::any())->willReturn(['json' => ['application/json']]); $listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); $this->assertSame('json', $request->getRequestFormat()); } @@ -108,15 +109,15 @@ public function testSupportedAcceptHeader() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); $request->headers->set('Accept', 'text/html, application/xhtml+xml, application/xml, application/json;q=0.9, */*;q=0.8'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); $formatsProviderProphecy->getFormatsFromAttributes(Argument::any())->willReturn(['binary' => ['application/octet-stream'], 'json' => ['application/json']]); $listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); $this->assertSame('json', $request->getRequestFormat()); } @@ -126,15 +127,15 @@ public function testSupportedAcceptHeaderSymfonyBuiltInFormat() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); $request->headers->set('Accept', 'application/json'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); $formatsProviderProphecy->getFormatsFromAttributes(Argument::any())->willReturn(['jsonld' => ['application/ld+json', 'application/json']]); $listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); $this->assertSame('jsonld', $request->getRequestFormat()); } @@ -144,15 +145,15 @@ public function testAcceptAllHeader() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); $request->headers->set('Accept', 'text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); $formatsProviderProphecy->getFormatsFromAttributes(Argument::any())->willReturn(['binary' => ['application/octet-stream'], 'json' => ['application/json']]); $listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); $this->assertSame('binary', $request->getRequestFormat()); $this->assertSame('application/octet-stream', $request->getMimeType($request->getRequestFormat())); @@ -166,15 +167,15 @@ public function testUnsupportedAcceptHeader() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); $request->headers->set('Accept', 'text/html, application/xhtml+xml, application/xml;q=0.9'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); $formatsProviderProphecy->getFormatsFromAttributes(Argument::any())->willReturn(['binary' => ['application/octet-stream'], 'json' => ['application/json']]); $listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); } public function testUnsupportedAcceptHeaderSymfonyBuiltInFormat() @@ -185,15 +186,15 @@ public function testUnsupportedAcceptHeaderSymfonyBuiltInFormat() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); $request->headers->set('Accept', 'text/xml'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); $formatsProviderProphecy->getFormatsFromAttributes(Argument::any())->willReturn(['json' => ['application/json']]); $listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); } public function testInvalidAcceptHeader() @@ -204,15 +205,15 @@ public function testInvalidAcceptHeader() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); $request->headers->set('Accept', 'invalid'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); $formatsProviderProphecy->getFormatsFromAttributes(Argument::any())->willReturn(['json' => ['application/json']]); $listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); } public function testAcceptHeaderTakePrecedenceOverRequestFormat() @@ -221,15 +222,15 @@ public function testAcceptHeaderTakePrecedenceOverRequestFormat() $request->headers->set('Accept', 'application/json'); $request->setRequestFormat('xml'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); $formatsProviderProphecy->getFormatsFromAttributes(Argument::any())->willReturn(['xml' => ['application/xml'], 'json' => ['application/json']]); $listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); $this->assertSame('json', $request->getRequestFormat()); } @@ -241,15 +242,15 @@ public function testInvalidRouteFormat() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get', '_format' => 'invalid']); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); $formatsProviderProphecy->getFormatsFromAttributes(Argument::any())->willReturn(['json' => ['application/json']]); $listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); } public function testResourceClassSupportedRequestFormat() @@ -257,15 +258,15 @@ public function testResourceClassSupportedRequestFormat() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); $request->setRequestFormat('csv'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); $formatsProviderProphecy->getFormatsFromAttributes(['resource_class' => 'Foo', 'collection_operation_name' => 'get', 'receive' => true, 'persist' => true])->willReturn(['csv' => ['text/csv']])->shouldBeCalled(); $listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); $this->assertSame('csv', $request->getRequestFormat()); $this->assertSame('text/csv', $request->getMimeType($request->getRequestFormat())); @@ -287,4 +288,23 @@ public function testLegacyFormatsParameter() { new AddFormatListener(new Negotiator(), ['xml' => ['text/xml']]); } + + /** + * @group legacy + * @expectedDeprecation The method ApiPlatform\Core\EventListener\AddFormatListener::onKernelRequest() is deprecated since 2.5 and will be removed in 3.0. + * @expectedDeprecation Passing an instance of "Symfony\Component\HttpKernel\Event\GetResponseEvent" as argument of "ApiPlatform\Core\EventListener\AddFormatListener::handleEvent" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "ApiPlatform\Core\Event\EventInterface" instead. + */ + public function testLegacyOnKernelRequest() + { + $request = new Request(); + + $eventProphecy = $this->prophesize(GetResponseEvent::class); + $eventProphecy->getRequest()->willReturn($request); + $event = $eventProphecy->reveal(); + + $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); + + $listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal()); + $listener->onKernelRequest($event); + } } diff --git a/tests/EventListener/DeserializeListenerTest.php b/tests/EventListener/DeserializeListenerTest.php index 20345460d3f..f33d1c3ef2b 100644 --- a/tests/EventListener/DeserializeListenerTest.php +++ b/tests/EventListener/DeserializeListenerTest.php @@ -14,6 +14,7 @@ namespace ApiPlatform\Core\Tests\EventListener; use ApiPlatform\Core\Api\FormatsProviderInterface; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\EventListener\DeserializeListener; use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; use PHPUnit\Framework\TestCase; @@ -33,11 +34,10 @@ class DeserializeListenerTest extends TestCase public function testDoNotCallWhenRequestMethodIsSafe() { - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $request = new Request([], [], ['data' => new \stdClass()]); $request->setMethod('GET'); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $serializerProphecy = $this->prophesize(SerializerInterface::class); $serializerProphecy->deserialize()->shouldNotBeCalled(); @@ -49,7 +49,7 @@ public function testDoNotCallWhenRequestMethodIsSafe() $formatsProviderProphecy->getFormatsFromAttributes(Argument::type('array'))->shouldNotBeCalled(); $listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($eventProphecy->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } /** @@ -57,12 +57,11 @@ public function testDoNotCallWhenRequestMethodIsSafe() */ public function testDoNotCallWhenSendingAndEmptyRequestContent($method) { - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $request = new Request([], [], ['data' => new \stdClass(), '_api_resource_class' => 'Foo', '_api_item_operation_name' => 'put'], [], [], [], ''); $request->setMethod($method); $request->headers->set('Content-Type', 'application/json'); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $serializerProphecy = $this->prophesize(SerializerInterface::class); $serializerProphecy->deserialize()->shouldNotBeCalled(); @@ -74,7 +73,7 @@ public function testDoNotCallWhenSendingAndEmptyRequestContent($method) $formatsProviderProphecy->getFormatsFromAttributes(Argument::type('array'))->shouldNotBeCalled(); $listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($eventProphecy->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } public function allowedEmptyRequestMethodsProvider() @@ -84,11 +83,10 @@ public function allowedEmptyRequestMethodsProvider() public function testDoNotCallWhenRequestNotManaged() { - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $request = new Request([], [], ['data' => new \stdClass()], [], [], [], '{}'); $request->setMethod('POST'); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $serializerProphecy = $this->prophesize(SerializerInterface::class); $serializerProphecy->deserialize()->shouldNotBeCalled(); @@ -100,16 +98,15 @@ public function testDoNotCallWhenRequestNotManaged() $formatsProviderProphecy->getFormatsFromAttributes(Argument::type('array'))->shouldNotBeCalled(); $listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($eventProphecy->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } public function testDoNotCallWhenReceiveFlagIsFalse() { - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $request = new Request([], [], ['data' => new \stdClass(), '_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post', '_api_receive' => false]); $request->setMethod('POST'); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $serializerProphecy = $this->prophesize(SerializerInterface::class); $serializerProphecy->deserialize()->shouldNotBeCalled(); @@ -121,16 +118,15 @@ public function testDoNotCallWhenReceiveFlagIsFalse() $formatsProviderProphecy->getFormatsFromAttributes(Argument::type('array'))->shouldNotBeCalled(); $listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($eventProphecy->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } public function testDoNotCallWhenInputClassDisabled() { - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $request = new Request([], [], ['data' => new \stdClass(), '_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post'], [], [], [], 'content'); $request->setMethod('POST'); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $serializerProphecy = $this->prophesize(SerializerInterface::class); $serializerProphecy->deserialize()->shouldNotBeCalled(); @@ -142,7 +138,7 @@ public function testDoNotCallWhenInputClassDisabled() $formatsProviderProphecy->getFormatsFromAttributes(Argument::type('array'))->shouldNotBeCalled(); $listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($eventProphecy->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } /** @@ -151,12 +147,12 @@ public function testDoNotCallWhenInputClassDisabled() public function testDeserialize(string $method, bool $populateObject) { $result = $populateObject ? new \stdClass() : null; - $eventProphecy = $this->prophesize(GetResponseEvent::class); $request = new Request([], [], ['data' => $result, '_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post'], [], [], [], '{}'); $request->setMethod($method); $request->headers->set('Content-Type', 'application/json'); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $serializerProphecy = $this->prophesize(SerializerInterface::class); $context = $populateObject ? [AbstractNormalizer::OBJECT_TO_POPULATE => $populateObject] : []; @@ -172,7 +168,7 @@ public function testDeserialize(string $method, bool $populateObject) $serializerContextBuilderProphecy->createFromRequest(Argument::type(Request::class), false, Argument::type('array'))->willReturn(['input' => ['class' => 'Foo'], 'output' => ['class' => 'Foo'], 'resource_class' => 'Foo'])->shouldBeCalled(); $listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($eventProphecy->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } /** @@ -181,12 +177,12 @@ public function testDeserialize(string $method, bool $populateObject) public function testDeserializeResourceClassSupportedFormat(string $method, bool $populateObject) { $result = $populateObject ? new \stdClass() : null; - $eventProphecy = $this->prophesize(GetResponseEvent::class); $request = new Request([], [], ['data' => $result, '_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post'], [], [], [], '{}'); $request->setMethod($method); $request->headers->set('Content-Type', 'application/json'); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $serializerProphecy = $this->prophesize(SerializerInterface::class); $context = $populateObject ? [AbstractNormalizer::OBJECT_TO_POPULATE => $populateObject] : []; @@ -203,7 +199,7 @@ public function testDeserializeResourceClassSupportedFormat(string $method, bool $listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), $formatsProviderProphecy->reveal()); - $listener->onKernelRequest($eventProphecy->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } public function methodProvider() @@ -213,13 +209,12 @@ public function methodProvider() public function testContentNegotiation() { - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post'], [], [], [], '{}'); $request->setMethod('POST'); $request->headers->set('Content-Type', 'text/xml'); $request->setFormat('xml', 'text/xml'); // Workaround to avoid weird behaviors - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $context = ['input' => ['class' => 'Foo'], 'output' => ['class' => 'Foo'], 'resource_class' => 'Foo']; @@ -237,7 +232,7 @@ public function testContentNegotiation() $serializerContextBuilderProphecy->reveal(), $formatsProviderProphecy->reveal() ); - $listener->onKernelRequest($eventProphecy->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } public function testNotSupportedContentType() @@ -245,13 +240,12 @@ public function testNotSupportedContentType() $this->expectException(NotAcceptableHttpException::class); $this->expectExceptionMessage('The content-type "application/rdf+xml" is not supported. Supported MIME types are "application/ld+json", "text/xml".'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post'], [], [], [], '{}'); $request->setMethod('POST'); $request->headers->set('Content-Type', 'application/rdf+xml'); $request->setRequestFormat('xml'); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $serializerProphecy = $this->prophesize(SerializerInterface::class); $serializerProphecy->deserialize()->shouldNotBeCalled(); @@ -267,7 +261,7 @@ public function testNotSupportedContentType() $serializerContextBuilderProphecy->reveal(), $formatsProviderProphecy->reveal() ); - $listener->onKernelRequest($eventProphecy->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } public function testNoContentType() @@ -275,12 +269,11 @@ public function testNoContentType() $this->expectException(NotAcceptableHttpException::class); $this->expectExceptionMessage('The "Content-Type" header must exist.'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post'], [], [], [], '{}'); $request->setMethod('POST'); $request->setRequestFormat('unknown'); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $serializerProphecy = $this->prophesize(SerializerInterface::class); $serializerProphecy->deserialize()->shouldNotBeCalled(); @@ -296,7 +289,7 @@ public function testNoContentType() $serializerContextBuilderProphecy->reveal(), $formatsProviderProphecy->reveal() ); - $listener->onKernelRequest($eventProphecy->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } public function testBadFormatsProviderParameterThrowsException() @@ -335,4 +328,26 @@ public function testLegacyFormatsParameter() self::FORMATS ); } + + /** + * @group legacy + * @expectedDeprecation The method ApiPlatform\Core\EventListener\DeserializeListener::onKernelRequest() is deprecated since 2.5 and will be removed in 3.0. + * @expectedDeprecation Passing an instance of "Symfony\Component\HttpKernel\Event\GetResponseEvent" as argument of "ApiPlatform\Core\EventListener\DeserializeListener::handleEvent" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "ApiPlatform\Core\Event\EventInterface" instead. + */ + public function testLegacyOnKernelRequest() + { + $serializerProphecy = $this->prophesize(SerializerInterface::class); + $serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class); + $formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class); + + $eventProphecy = $this->prophesize(GetResponseEvent::class); + $eventProphecy->getRequest()->willReturn(new Request()); + + $listener = new DeserializeListener( + $serializerProphecy->reveal(), + $serializerContextBuilderProphecy->reveal(), + $formatsProviderProphecy->reveal() + ); + $listener->onKernelRequest($eventProphecy->reveal()); + } } diff --git a/tests/EventListener/ReadListenerTest.php b/tests/EventListener/ReadListenerTest.php index e259bf70010..832f9a5834c 100644 --- a/tests/EventListener/ReadListenerTest.php +++ b/tests/EventListener/ReadListenerTest.php @@ -16,6 +16,7 @@ use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface; use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\EventListener\ReadListener; use ApiPlatform\Core\Exception\InvalidIdentifierException; use ApiPlatform\Core\Exception\RuntimeException; @@ -44,11 +45,11 @@ public function testNotAnApiPlatformRequest() $subresourceDataProvider = $this->prophesize(SubresourceDataProviderInterface::class); $subresourceDataProvider->getSubresource()->shouldNotBeCalled(); - $event = $this->prophesize(GetResponseEvent::class); - $event->getRequest()->willReturn(new Request())->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => new Request()]); $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $subresourceDataProvider->reveal(), null, $identifierConverter->reveal()); - $listener->onKernelRequest($event->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } /** @@ -65,11 +66,11 @@ public function testLegacyConstructor() $subresourceDataProvider = $this->prophesize(SubresourceDataProviderInterface::class); $subresourceDataProvider->getSubresource()->shouldNotBeCalled(); - $event = $this->prophesize(GetResponseEvent::class); - $event->getRequest()->willReturn(new Request())->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => new Request()]); $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $subresourceDataProvider->reveal()); - $listener->onKernelRequest($event->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } public function testDoNotCallWhenReceiveFlagIsFalse() @@ -88,11 +89,11 @@ public function testDoNotCallWhenReceiveFlagIsFalse() $request = new Request([], [], ['data' => new \stdClass(), '_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post', '_api_receive' => false]); $request->setMethod('PUT'); - $event = $this->prophesize(GetResponseEvent::class); - $event->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $subresourceDataProvider->reveal(), null, $identifierConverter->reveal()); - $listener->onKernelRequest($event->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } public function testRetrieveCollectionPost() @@ -111,11 +112,11 @@ public function testRetrieveCollectionPost() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post', '_api_format' => 'json', '_api_mime_type' => 'application/json'], [], [], [], '{}'); $request->setMethod('POST'); - $event = $this->prophesize(GetResponseEvent::class); - $event->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $subresourceDataProvider->reveal(), null, $identifierConverter->reveal()); - $listener->onKernelRequest($event->reveal()); + $listener->handleEvent($eventProphecy->reveal()); $this->assertTrue($request->attributes->has('data')); $this->assertNull($request->attributes->get('data')); @@ -137,11 +138,11 @@ public function testRetrieveCollectionGet() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get', '_api_format' => 'json', '_api_mime_type' => 'application/json'], [], [], ['QUERY_STRING' => 'foo=bar']); $request->setMethod('GET'); - $event = $this->prophesize(GetResponseEvent::class); - $event->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $subresourceDataProvider->reveal(), null, $identifierConverter->reveal()); - $listener->onKernelRequest($event->reveal()); + $listener->handleEvent($eventProphecy->reveal()); $this->assertSame([], $request->attributes->get('data')); } @@ -164,11 +165,11 @@ public function testRetrieveItem() $request = new Request([], [], ['id' => 1, '_api_resource_class' => 'Foo', '_api_item_operation_name' => 'get', '_api_format' => 'json', '_api_mime_type' => 'application/json']); $request->setMethod('GET'); - $event = $this->prophesize(GetResponseEvent::class); - $event->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $subresourceDataProvider->reveal(), null, $identifierConverter->reveal()); - $listener->onKernelRequest($event->reveal()); + $listener->handleEvent($eventProphecy->reveal()); $this->assertSame($data, $request->attributes->get('data')); } @@ -189,11 +190,11 @@ public function testRetrieveItemNoIdentifier() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_item_operation_name' => 'get', '_api_format' => 'json', '_api_mime_type' => 'application/json']); $request->setMethod('GET'); - $event = $this->prophesize(GetResponseEvent::class); - $event->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $subresourceDataProvider->reveal()); - $listener->onKernelRequest($event->reveal()); + $listener->handleEvent($eventProphecy->reveal()); $request->attributes->get('data'); } @@ -216,11 +217,11 @@ public function testRetrieveSubresource() $request = new Request([], [], ['id' => 1, '_api_resource_class' => 'Foo', '_api_subresource_operation_name' => 'get', '_api_format' => 'json', '_api_mime_type' => 'application/json', '_api_subresource_context' => ['identifiers' => [['id', 'Bar', true]], 'property' => 'bar']]); $request->setMethod('GET'); - $event = $this->prophesize(GetResponseEvent::class); - $event->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $subresourceDataProvider->reveal(), null, $identifierConverter->reveal()); - $listener->onKernelRequest($event->reveal()); + $listener->handleEvent($eventProphecy->reveal()); $this->assertSame($data, $request->attributes->get('data')); } @@ -238,11 +239,11 @@ public function testRetrieveSubresourceNoDataProvider() $request = new Request([], [], ['id' => 1, '_api_resource_class' => 'Foo', '_api_subresource_operation_name' => 'get', '_api_format' => 'json', '_api_mime_type' => 'application/json', '_api_subresource_context' => ['identifiers' => [['id', 'Bar', true]], 'property' => 'bar']]); $request->setMethod('GET'); - $event = $this->prophesize(GetResponseEvent::class); - $event->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal()); - $listener->onKernelRequest($event->reveal()); + $listener->handleEvent($eventProphecy->reveal()); $request->attributes->get('data'); } @@ -262,11 +263,11 @@ public function testRetrieveSubresourceNotFound() $request = new Request([], [], ['id' => 1, '_api_resource_class' => 'Foo', '_api_subresource_operation_name' => 'get', '_api_format' => 'json', '_api_mime_type' => 'application/json', '_api_subresource_context' => ['identifiers' => [['id', 'Bar', true]], 'property' => 'bar']]); $request->setMethod('GET'); - $event = $this->prophesize(GetResponseEvent::class); - $event->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $this->prophesize(SubresourceDataProviderInterface::class)->reveal(), null, $identifierConverter->reveal()); - $listener->onKernelRequest($event->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } public function testRetrieveItemNotFound() @@ -285,11 +286,11 @@ public function testRetrieveItemNotFound() $request = new Request([], [], ['id' => 22, '_api_resource_class' => 'Foo', '_api_item_operation_name' => 'get', '_api_format' => 'json', '_api_mime_type' => 'application/json']); $request->setMethod('GET'); - $event = $this->prophesize(GetResponseEvent::class); - $event->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $subresourceDataProvider->reveal(), null, $identifierConverter->reveal()); - $listener->onKernelRequest($event->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } public function testRetrieveBadItemNormalizedIdentifiers() @@ -306,11 +307,11 @@ public function testRetrieveBadItemNormalizedIdentifiers() $request = new Request([], [], ['id' => 1, '_api_resource_class' => 'Foo', '_api_item_operation_name' => 'get', '_api_format' => 'json', '_api_mime_type' => 'application/json']); $request->setMethod(Request::METHOD_GET); - $event = $this->prophesize(GetResponseEvent::class); - $event->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $subresourceDataProvider->reveal(), null, $identifierConverter->reveal()); - $listener->onKernelRequest($event->reveal()); + $listener->handleEvent($eventProphecy->reveal()); } public function testRetrieveBadSubresourceNormalizedIdentifiers() @@ -326,17 +327,35 @@ public function testRetrieveBadSubresourceNormalizedIdentifiers() $itemDataProvider = $this->prophesize(ItemDataProviderInterface::class); $itemDataProvider->getItem()->shouldNotBeCalled(); - $data = [new \stdClass()]; $subresourceDataProvider = $this->prophesize(SubresourceDataProviderInterface::class); $subresourceDataProvider->getSubresource()->shouldNotBeCalled(); $request = new Request([], [], ['id' => 1, '_api_resource_class' => 'Foo', '_api_subresource_operation_name' => 'get', '_api_format' => 'json', '_api_mime_type' => 'application/json', '_api_subresource_context' => ['identifiers' => [['id', 'Bar', true]], 'property' => 'bar']]); $request->setMethod(Request::METHOD_GET); - $event = $this->prophesize(GetResponseEvent::class); - $event->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); + + $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $subresourceDataProvider->reveal(), null, $identifierConverter->reveal()); + $listener->handleEvent($eventProphecy->reveal()); + } + + /** + * @group legacy + * @expectedDeprecation The method ApiPlatform\Core\EventListener\ReadListener::onKernelRequest() is deprecated since 2.5 and will be removed in 3.0. + * @expectedDeprecation Passing an instance of "Symfony\Component\HttpKernel\Event\GetResponseEvent" as argument of "ApiPlatform\Core\EventListener\ReadListener::handleEvent" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "ApiPlatform\Core\Event\EventInterface" instead. + */ + public function testLegacyOnKernelRequest() + { + $collectionDataProvider = $this->prophesize(CollectionDataProviderInterface::class); + $itemDataProvider = $this->prophesize(ItemDataProviderInterface::class); + $subresourceDataProvider = $this->prophesize(SubresourceDataProviderInterface::class); + $identifierConverter = $this->prophesize(IdentifierConverterInterface::class); + + $eventProphecy = $this->prophesize(GetResponseEvent::class); + $eventProphecy->getRequest()->willReturn(new Request()); $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $subresourceDataProvider->reveal(), null, $identifierConverter->reveal()); - $listener->onKernelRequest($event->reveal()); + $listener->onKernelRequest($eventProphecy->reveal()); } } diff --git a/tests/Filter/QueryParameterValidateListenerTest.php b/tests/Filter/QueryParameterValidateListenerTest.php index a05393d24dc..cce018e1824 100644 --- a/tests/Filter/QueryParameterValidateListenerTest.php +++ b/tests/Filter/QueryParameterValidateListenerTest.php @@ -14,6 +14,7 @@ namespace ApiPlatform\Core\Tests\Filter; use ApiPlatform\Core\Api\FilterInterface; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\Exception\FilterValidationException; use ApiPlatform\Core\Filter\QueryParameterValidateListener; use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; @@ -26,60 +27,61 @@ class QueryParameterValidateListenerTest extends TestCase { + /** @var QueryParameterValidateListener */ private $testedInstance; private $filterLocatorProphecy; /** * unsafe method should not use filter validations. */ - public function testOnKernelRequestWithUnsafeMethod() + public function testWithUnsafeMethod() { $this->setUpWithFilters(); $request = new Request(); $request->setMethod('POST'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $this->assertNull( - $this->testedInstance->onKernelRequest($eventProphecy->reveal()) + $this->testedInstance->handleEvent($eventProphecy->reveal()) ); } /** * If the tested filter is non-existent, then nothing should append. */ - public function testOnKernelRequestWithWrongFilter() + public function testWithWrongFilter() { $this->setUpWithFilters(['some_inexistent_filter']); $request = new Request([], [], ['_api_resource_class' => Dummy::class, '_api_collection_operation_name' => 'get']); $request->setMethod('GET'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $this->filterLocatorProphecy->has('some_inexistent_filter')->shouldBeCalled(); $this->filterLocatorProphecy->get('some_inexistent_filter')->shouldNotBeCalled(); $this->assertNull( - $this->testedInstance->onKernelRequest($eventProphecy->reveal()) + $this->testedInstance->handleEvent($eventProphecy->reveal()) ); } /** * if the required parameter is not set, throw an FilterValidationException. */ - public function testOnKernelRequestWithRequiredFilterNotSet() + public function testWithRequiredFilterNotSet() { $this->setUpWithFilters(['some_filter']); $request = new Request([], [], ['_api_resource_class' => Dummy::class, '_api_collection_operation_name' => 'get']); $request->setMethod('GET'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $this->filterLocatorProphecy ->has('some_filter') @@ -104,13 +106,13 @@ public function testOnKernelRequestWithRequiredFilterNotSet() $this->expectException(FilterValidationException::class); $this->expectExceptionMessage('Query parameter "required" is required'); - $this->testedInstance->onKernelRequest($eventProphecy->reveal()); + $this->testedInstance->handleEvent($eventProphecy->reveal()); } /** * if the required parameter is set, no exception should be thrown. */ - public function testOnKernelRequestWithRequiredFilter() + public function testWithRequiredFilter() { $this->setUpWithFilters(['some_filter']); @@ -121,8 +123,8 @@ public function testOnKernelRequestWithRequiredFilter() ); $request->setMethod('GET'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $this->filterLocatorProphecy ->has('some_filter') @@ -146,10 +148,28 @@ public function testOnKernelRequestWithRequiredFilter() ; $this->assertNull( - $this->testedInstance->onKernelRequest($eventProphecy->reveal()) + $this->testedInstance->handleEvent($eventProphecy->reveal()) ); } + /** + * @group legacy + * @expectedDeprecation The method ApiPlatform\Core\Filter\QueryParameterValidateListener::onKernelRequest() is deprecated since 2.5 and will be removed in 3.0. + * @expectedDeprecation Passing an instance of "Symfony\Component\HttpKernel\Event\GetResponseEvent" as argument of "ApiPlatform\Core\Filter\QueryParameterValidateListener::handleEvent" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "ApiPlatform\Core\Event\EventInterface" instead. + */ + public function testLegacyOnKernelRequest() + { + $this->setUpWithFilters(); + + $request = new Request(); + $request->setMethod('POST'); + + $eventProphecy = $this->prophesize(GetResponseEvent::class); + $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + + $this->testedInstance->onKernelRequest($eventProphecy->reveal()); + } + private function setUpWithFilters(array $filters = []) { $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); diff --git a/tests/JsonApi/EventListener/TransformFieldsetsParametersListenerTest.php b/tests/JsonApi/EventListener/TransformFieldsetsParametersListenerTest.php index 1da7b75ba3e..7aeb94fbcfb 100644 --- a/tests/JsonApi/EventListener/TransformFieldsetsParametersListenerTest.php +++ b/tests/JsonApi/EventListener/TransformFieldsetsParametersListenerTest.php @@ -13,6 +13,7 @@ namespace ApiPlatform\Core\Tests\JsonApi\EventListener; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\JsonApi\EventListener\TransformFieldsetsParametersListener; use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; @@ -23,6 +24,7 @@ class TransformFieldsetsParametersListenerTest extends TestCase { + /** @var TransformFieldsetsParametersListener */ private $listener; protected function setUp() @@ -33,44 +35,44 @@ protected function setUp() $this->listener = new TransformFieldsetsParametersListener($resourceMetadataFactoryProphecy->reveal()); } - public function testOnKernelRequestWithInvalidFormat() + public function testWithInvalidFormat() { $expectedRequest = new Request(); $expectedRequest->setRequestFormat('badformat'); $request = $expectedRequest->duplicate(); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $this->listener->handleEvent($eventProphecy->reveal()); $this->assertEquals($expectedRequest, $request); } - public function testOnKernelRequestWithInvalidFilter() + public function testWithInvalidFilter() { - $eventProphecy = $this->prophesize(GetResponseEvent::class); + $eventProphecy = $this->prophesize(EventInterface::class); $expectedRequest = new Request(); $expectedRequest->setRequestFormat('jsonapi'); $request = $expectedRequest->duplicate(); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $eventProphecy->getContext()->willReturn(['request' => $request]); + $this->listener->handleEvent($eventProphecy->reveal()); $this->assertEquals($expectedRequest, $request); $expectedRequest = $expectedRequest->duplicate(['fields' => 'foo']); $request = $expectedRequest->duplicate(); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $eventProphecy->getContext()->willReturn(['request' => $request]); + $this->listener->handleEvent($eventProphecy->reveal()); $this->assertEquals($expectedRequest, $request); } - public function testOnKernelRequest() + public function testWithValidFilter() { $request = new Request( ['fields' => ['dummy' => 'id,name,dummyFloat', 'relatedDummy' => 'id,name'], 'include' => 'relatedDummy,foo'], @@ -79,10 +81,10 @@ public function testOnKernelRequest() ); $request->setRequestFormat('jsonapi'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $this->listener->handleEvent($eventProphecy->reveal()); $expectedRequest = new Request( ['fields' => ['dummy' => 'id,name,dummyFloat', 'relatedDummy' => 'id,name'], 'include' => 'relatedDummy,foo'], @@ -98,7 +100,7 @@ public function testOnKernelRequest() $this->assertEquals($expectedRequest, $request); } - public function testOnKernelRequestWithIncludeWithoutFields() + public function testWithIncludeWithoutFields() { $request = new Request( ['include' => 'relatedDummy,foo'], @@ -107,10 +109,10 @@ public function testOnKernelRequestWithIncludeWithoutFields() ); $request->setRequestFormat('jsonapi'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $this->listener->handleEvent($eventProphecy->reveal()); $expectedRequest = new Request( ['include' => 'relatedDummy,foo'], @@ -125,7 +127,7 @@ public function testOnKernelRequestWithIncludeWithoutFields() $this->assertEquals($expectedRequest, $request); } - public function testOnKernelRequestWithWrongParametersTypesDoesnTAffectRequestAttributes() + public function testWithWrongParametersTypesDoesnTAffectRequestAttributes() { $request = new Request( ['fields' => 'foo', 'include' => ['relatedDummy,foo']], @@ -134,10 +136,10 @@ public function testOnKernelRequestWithWrongParametersTypesDoesnTAffectRequestAt ); $request->setRequestFormat('jsonapi'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $this->listener->handleEvent($eventProphecy->reveal()); $expectedRequest = new Request( ['fields' => 'foo', 'include' => ['relatedDummy,foo']], @@ -148,4 +150,17 @@ public function testOnKernelRequestWithWrongParametersTypesDoesnTAffectRequestAt $this->assertEquals($expectedRequest, $request); } + + /** + * @group legacy + * @expectedDeprecation The method ApiPlatform\Core\JsonApi\EventListener\TransformFieldsetsParametersListener::onKernelRequest() is deprecated since 2.5 and will be removed in 3.0. + * @expectedDeprecation Passing an instance of "Symfony\Component\HttpKernel\Event\GetResponseEvent" as argument of "ApiPlatform\Core\JsonApi\EventListener\TransformFieldsetsParametersListener::handleEvent" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "ApiPlatform\Core\Event\EventInterface" instead. + */ + public function testLegacyOnKernelRequest() + { + $eventProphecy = $this->prophesize(GetResponseEvent::class); + $eventProphecy->getRequest()->willReturn(new Request())->shouldBeCalled(); + + $this->listener->onKernelRequest($eventProphecy->reveal()); + } } diff --git a/tests/JsonApi/EventListener/TransformFilteringParametersListenerTest.php b/tests/JsonApi/EventListener/TransformFilteringParametersListenerTest.php index 72a76c69a59..6febb2dc225 100644 --- a/tests/JsonApi/EventListener/TransformFilteringParametersListenerTest.php +++ b/tests/JsonApi/EventListener/TransformFilteringParametersListenerTest.php @@ -13,6 +13,7 @@ namespace ApiPlatform\Core\Tests\JsonApi\EventListener; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\JsonApi\EventListener\TransformFilteringParametersListener; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; @@ -23,6 +24,7 @@ */ class TransformFilteringParametersListenerTest extends TestCase { + /** @var TransformFilteringParametersListener */ private $listener; protected function setUp() @@ -30,56 +32,69 @@ protected function setUp() $this->listener = new TransformFilteringParametersListener(); } - public function testOnKernelRequestWithInvalidFormat() + public function testWithInvalidFormat() { $expectedRequest = new Request(); $expectedRequest->setRequestFormat('badformat'); $request = $expectedRequest->duplicate(); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $this->listener->handleEvent($eventProphecy->reveal()); $this->assertEquals($expectedRequest, $request); } - public function testOnKernelRequestWithInvalidFilter() + public function testWithInvalidFilter() { - $eventProphecy = $this->prophesize(GetResponseEvent::class); + $eventProphecy = $this->prophesize(EventInterface::class); $expectedRequest = new Request(); $expectedRequest->setRequestFormat('jsonapi'); $request = $expectedRequest->duplicate(); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $eventProphecy->getContext()->willReturn(['request' => $request]); + $this->listener->handleEvent($eventProphecy->reveal()); $this->assertEquals($expectedRequest, $request); $expectedRequest = $expectedRequest->duplicate(['filter' => 'foo']); $request = $expectedRequest->duplicate(); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $eventProphecy->getContext()->willReturn(['request' => $request]); + $this->listener->handleEvent($eventProphecy->reveal()); $this->assertEquals($expectedRequest, $request); } - public function testOnKernelRequest() + public function testWithValidFilter() { $request = new Request(['filter' => ['foo' => 'bar', 'baz' => 'qux']]); $request->setRequestFormat('jsonapi'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $this->listener->handleEvent($eventProphecy->reveal()); $expectedRequest = new Request(['filter' => ['foo' => 'bar', 'baz' => 'qux']], [], ['_api_filters' => ['foo' => 'bar', 'baz' => 'qux']]); $expectedRequest->setRequestFormat('jsonapi'); $this->assertEquals($expectedRequest, $request); } + + /** + * @group legacy + * @expectedDeprecation The method ApiPlatform\Core\JsonApi\EventListener\TransformFilteringParametersListener::onKernelRequest() is deprecated since 2.5 and will be removed in 3.0. + * @expectedDeprecation Passing an instance of "Symfony\Component\HttpKernel\Event\GetResponseEvent" as argument of "ApiPlatform\Core\JsonApi\EventListener\TransformFilteringParametersListener::handleEvent" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "ApiPlatform\Core\Event\EventInterface" instead. + */ + public function testLegacyOnKernelRequest() + { + $eventProphecy = $this->prophesize(GetResponseEvent::class); + $eventProphecy->getRequest()->willReturn(new Request())->shouldBeCalled(); + + $this->listener->onKernelRequest($eventProphecy->reveal()); + } } diff --git a/tests/JsonApi/EventListener/TransformPaginationParametersListenerTest.php b/tests/JsonApi/EventListener/TransformPaginationParametersListenerTest.php index 54ae4b9fa35..de054712672 100644 --- a/tests/JsonApi/EventListener/TransformPaginationParametersListenerTest.php +++ b/tests/JsonApi/EventListener/TransformPaginationParametersListenerTest.php @@ -13,6 +13,7 @@ namespace ApiPlatform\Core\Tests\JsonApi\EventListener; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\JsonApi\EventListener\TransformPaginationParametersListener; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; @@ -23,6 +24,7 @@ */ class TransformPaginationParametersListenerTest extends TestCase { + /** @var TransformPaginationParametersListener */ private $listener; protected function setUp() @@ -30,52 +32,52 @@ protected function setUp() $this->listener = new TransformPaginationParametersListener(); } - public function testOnKernelRequestWithInvalidFormat() + public function testWithInvalidFormat() { $expectedRequest = new Request(); $expectedRequest->setRequestFormat('badformat'); $request = $expectedRequest->duplicate(); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $this->listener->handleEvent($eventProphecy->reveal()); $this->assertEquals($expectedRequest, $request); } - public function testOnKernelRequestWithInvalidPage() + public function testWithInvalidPage() { - $eventProphecy = $this->prophesize(GetResponseEvent::class); + $eventProphecy = $this->prophesize(EventInterface::class); $expectedRequest = new Request(); $expectedRequest->setRequestFormat('jsonapi'); $request = $expectedRequest->duplicate(); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $eventProphecy->getContext()->willReturn(['request' => $request]); + $this->listener->handleEvent($eventProphecy->reveal()); $this->assertEquals($expectedRequest, $request); $expectedRequest = $expectedRequest->duplicate(['page' => 'foo']); $request = $expectedRequest->duplicate(); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $eventProphecy->getContext()->willReturn(['request' => $request]); + $this->listener->handleEvent($eventProphecy->reveal()); $this->assertEquals($expectedRequest, $request); } - public function testOnKernelRequest() + public function testWithValidFilter() { $request = new Request(['page' => ['size' => 5, 'number' => 3, 'error' => -1]]); $request->setRequestFormat('jsonapi'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $this->listener->handleEvent($eventProphecy->reveal()); $filters = ['size' => 5, 'number' => 3, 'error' => -1]; @@ -84,4 +86,17 @@ public function testOnKernelRequest() $this->assertEquals($expectedRequest, $request); } + + /** + * @group legacy + * @expectedDeprecation The method ApiPlatform\Core\JsonApi\EventListener\TransformPaginationParametersListener::onKernelRequest() is deprecated since 2.5 and will be removed in 3.0. + * @expectedDeprecation Passing an instance of "Symfony\Component\HttpKernel\Event\GetResponseEvent" as argument of "ApiPlatform\Core\JsonApi\EventListener\TransformPaginationParametersListener::handleEvent" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "ApiPlatform\Core\Event\EventInterface" instead. + */ + public function testLegacyOnKernelRequest() + { + $eventProphecy = $this->prophesize(GetResponseEvent::class); + $eventProphecy->getRequest()->willReturn(new Request())->shouldBeCalled(); + + $this->listener->onKernelRequest($eventProphecy->reveal()); + } } diff --git a/tests/JsonApi/EventListener/TransformSortingParametersListenerTest.php b/tests/JsonApi/EventListener/TransformSortingParametersListenerTest.php index d1d9f51bf34..37510df9b62 100644 --- a/tests/JsonApi/EventListener/TransformSortingParametersListenerTest.php +++ b/tests/JsonApi/EventListener/TransformSortingParametersListenerTest.php @@ -13,6 +13,7 @@ namespace ApiPlatform\Core\Tests\JsonApi\EventListener; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\JsonApi\EventListener\TransformSortingParametersListener; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; @@ -23,6 +24,7 @@ */ class TransformSortingParametersListenerTest extends TestCase { + /** @var TransformSortingParametersListener */ private $listener; protected function setUp() @@ -30,56 +32,69 @@ protected function setUp() $this->listener = new TransformSortingParametersListener(); } - public function testOnKernelRequestWithInvalidFormat() + public function testWithInvalidFormat() { $expectedRequest = new Request(); $expectedRequest->setRequestFormat('badformat'); $request = $expectedRequest->duplicate(); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $this->listener->handleEvent($eventProphecy->reveal()); $this->assertEquals($expectedRequest, $request); } - public function testOnKernelRequestWithInvalidFilter() + public function testWithInvalidFilter() { - $eventProphecy = $this->prophesize(GetResponseEvent::class); + $eventProphecy = $this->prophesize(EventInterface::class); $expectedRequest = new Request(); $expectedRequest->setRequestFormat('jsonapi'); $request = $expectedRequest->duplicate(); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $eventProphecy->getContext()->willReturn(['request' => $request]); + $this->listener->handleEvent($eventProphecy->reveal()); $this->assertEquals($expectedRequest, $request); $expectedRequest = $expectedRequest->duplicate(['sort' => ['foo', '-bar']]); $request = $expectedRequest->duplicate(); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $eventProphecy->getContext()->willReturn(['request' => $request]); + $this->listener->handleEvent($eventProphecy->reveal()); $this->assertEquals($expectedRequest, $request); } - public function testOnKernelRequest() + public function testWithValidFilter() { $request = new Request(['sort' => 'foo,-bar,-baz,qux']); $request->setRequestFormat('jsonapi'); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); - $this->listener->onKernelRequest($eventProphecy->reveal()); + $this->listener->handleEvent($eventProphecy->reveal()); $expectedRequest = new Request(['sort' => 'foo,-bar,-baz,qux'], [], ['_api_filters' => ['order' => ['foo' => 'asc', 'bar' => 'desc', 'baz' => 'desc', 'qux' => 'asc']]]); $expectedRequest->setRequestFormat('jsonapi'); $this->assertEquals($expectedRequest, $request); } + + /** + * @group legacy + * @expectedDeprecation The method ApiPlatform\Core\JsonApi\EventListener\TransformSortingParametersListener::onKernelRequest() is deprecated since 2.5 and will be removed in 3.0. + * @expectedDeprecation Passing an instance of "Symfony\Component\HttpKernel\Event\GetResponseEvent" as argument of "ApiPlatform\Core\JsonApi\EventListener\TransformSortingParametersListener::handleEvent" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "ApiPlatform\Core\Event\EventInterface" instead. + */ + public function testLegacyOnKernelRequest() + { + $eventProphecy = $this->prophesize(GetResponseEvent::class); + $eventProphecy->getRequest()->willReturn(new Request())->shouldBeCalled(); + + $this->listener->onKernelRequest($eventProphecy->reveal()); + } } diff --git a/tests/Security/EventListener/DenyAccessListenerTest.php b/tests/Security/EventListener/DenyAccessListenerTest.php index 63de9ddb29d..d329ce19319 100644 --- a/tests/Security/EventListener/DenyAccessListenerTest.php +++ b/tests/Security/EventListener/DenyAccessListenerTest.php @@ -13,6 +13,7 @@ namespace ApiPlatform\Core\Tests\Security\EventListener; +use ApiPlatform\Core\Event\EventInterface; use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; use ApiPlatform\Core\Security\EventListener\DenyAccessListener; @@ -38,8 +39,8 @@ public function testNoResourceClass() { $request = new Request(); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); @@ -47,15 +48,15 @@ public function testNoResourceClass() $resourceMetadataFactory = $resourceMetadataFactoryProphecy->reveal(); $listener = $this->getListener($resourceMetadataFactory); - $listener->onKernelRequest($event); + $listener->handleEvent($event); } public function testNoIsGrantedAttribute() { $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $resourceMetadata = new ResourceMetadata(); @@ -64,7 +65,7 @@ public function testNoIsGrantedAttribute() $resourceMetadataFactoryProphecy->create('Foo')->willReturn($resourceMetadata)->shouldBeCalled(); $listener = $this->getListener($resourceMetadataFactoryProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); } public function testIsGranted() @@ -72,8 +73,8 @@ public function testIsGranted() $data = new \stdClass(); $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get', 'data' => $data]); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['access_control' => 'has_role("ROLE_ADMIN")']); @@ -85,7 +86,7 @@ public function testIsGranted() $resourceAccessCheckerProphecy->isGranted('Foo', 'has_role("ROLE_ADMIN")', Argument::type('array'))->willReturn(true)->shouldBeCalled(); $listener = $this->getListener($resourceMetadataFactoryProphecy->reveal(), $resourceAccessCheckerProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); } public function testIsNotGranted() @@ -94,8 +95,8 @@ public function testIsNotGranted() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['access_control' => 'has_role("ROLE_ADMIN")']); @@ -107,7 +108,7 @@ public function testIsNotGranted() $resourceAccessCheckerProphecy->isGranted('Foo', 'has_role("ROLE_ADMIN")', Argument::type('array'))->willReturn(false)->shouldBeCalled(); $listener = $this->getListener($resourceMetadataFactoryProphecy->reveal(), $resourceAccessCheckerProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); } public function testAccessControlMessage() @@ -117,8 +118,8 @@ public function testAccessControlMessage() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['access_control' => 'has_role("ROLE_ADMIN")', 'access_control_message' => 'You are not admin.']); @@ -130,7 +131,7 @@ public function testAccessControlMessage() $resourceAccessCheckerProphecy->isGranted('Foo', 'has_role("ROLE_ADMIN")', Argument::type('array'))->willReturn(false)->shouldBeCalled(); $listener = $this->getListener($resourceMetadataFactoryProphecy->reveal(), $resourceAccessCheckerProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); } /** @@ -141,8 +142,8 @@ public function testIsGrantedLegacy() $data = new \stdClass(); $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get', 'data' => $data]); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['access_control' => 'has_role("ROLE_ADMIN")']); @@ -154,7 +155,7 @@ public function testIsGrantedLegacy() $expressionLanguageProphecy->evaluate('has_role("ROLE_ADMIN")', Argument::type('array'))->willReturn(true)->shouldBeCalled(); $listener = $this->getLegacyListener($resourceMetadataFactoryProphecy->reveal(), $expressionLanguageProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); } /** @@ -166,8 +167,8 @@ public function testIsNotGrantedLegacy() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['access_control' => 'has_role("ROLE_ADMIN")']); @@ -179,7 +180,7 @@ public function testIsNotGrantedLegacy() $expressionLanguageProphecy->evaluate('has_role("ROLE_ADMIN")', Argument::type('array'))->willReturn(false)->shouldBeCalled(); $listener = $this->getLegacyListener($resourceMetadataFactoryProphecy->reveal(), $expressionLanguageProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); } /** @@ -191,8 +192,8 @@ public function testSecurityComponentNotAvailable() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['access_control' => 'has_role("ROLE_ADMIN")']); @@ -201,7 +202,7 @@ public function testSecurityComponentNotAvailable() $resourceMetadataFactoryProphecy->create('Foo')->willReturn($resourceMetadata)->shouldBeCalled(); $listener = new DenyAccessListener($resourceMetadataFactoryProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); } /** @@ -213,8 +214,8 @@ public function testExpressionLanguageNotInstalled() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['access_control' => 'has_role("ROLE_ADMIN")']); @@ -227,7 +228,7 @@ public function testExpressionLanguageNotInstalled() $tokenStorageProphecy->getToken()->willReturn($this->prophesize(TokenInterface::class)->reveal()); $listener = new DenyAccessListener($resourceMetadataFactoryProphecy->reveal(), null, $authenticationTrustResolverProphecy->reveal(), null, $tokenStorageProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); } /** @@ -239,8 +240,8 @@ public function testNotBehindAFirewall() $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); - $eventProphecy = $this->prophesize(GetResponseEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy = $this->prophesize(EventInterface::class); + $eventProphecy->getContext()->willReturn(['request' => $request]); $event = $eventProphecy->reveal(); $resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['access_control' => 'has_role("ROLE_ADMIN")']); @@ -252,7 +253,7 @@ public function testNotBehindAFirewall() $tokenStorageProphecy = $this->prophesize(TokenStorageInterface::class); $listener = new DenyAccessListener($resourceMetadataFactoryProphecy->reveal(), null, $authenticationTrustResolverProphecy->reveal(), null, $tokenStorageProphecy->reveal()); - $listener->onKernelRequest($event); + $listener->handleEvent($event); } private function getListener(ResourceMetadataFactoryInterface $resourceMetadataFactory, ResourceAccessCheckerInterface $resourceAccessChecker = null) @@ -289,4 +290,21 @@ private function getLegacyListener(ResourceMetadataFactoryInterface $resourceMet $authorizationCheckerInterface->reveal() ); } + + /** + * @group legacy + * @expectedDeprecation The method ApiPlatform\Core\Security\EventListener\DenyAccessListener::onKernelRequest() is deprecated since 2.5 and will be removed in 3.0. + * @expectedDeprecation Passing an instance of "Symfony\Component\HttpKernel\Event\GetResponseEvent" as argument of "ApiPlatform\Core\Security\EventListener\DenyAccessListener::handleEvent" is deprecated since 2.5 and will not be possible anymore in 3.0. Pass an instance of "ApiPlatform\Core\Event\EventInterface" instead. + */ + public function testLegacyOnKernelRequest() + { + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); + $resourceAccessCheckerProphecy = $this->prophesize(ResourceAccessCheckerInterface::class); + + $eventProphecy = $this->prophesize(GetResponseEvent::class); + $eventProphecy->getRequest()->willReturn(new Request()); + + $listener = new DenyAccessListener($resourceMetadataFactoryProphecy->reveal(), $resourceAccessCheckerProphecy->reveal()); + $listener->onKernelRequest($eventProphecy->reveal()); + } }