diff --git a/DependencyInjection/Compiler/OverrideRequestHandlerCompilerPass.php b/DependencyInjection/Compiler/OverrideRequestHandlerCompilerPass.php index c09cf8a..c3bae8a 100644 --- a/DependencyInjection/Compiler/OverrideRequestHandlerCompilerPass.php +++ b/DependencyInjection/Compiler/OverrideRequestHandlerCompilerPass.php @@ -2,15 +2,16 @@ namespace Elao\Bundle\JsonHttpFormBundle\DependencyInjection\Compiler; +use Elao\Bundle\JsonHttpFormBundle\Form\RequestHandler\JsonHttpFoundationRequestHandler; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; class OverrideRequestHandlerCompilerPass implements CompilerPassInterface { - public function process(ContainerBuilder $container) + public function process(ContainerBuilder $container): void { $container ->getDefinition('form.type_extension.form.request_handler') - ->setClass('Elao\Bundle\JsonHttpFormBundle\Form\RequestHandler\JsonHttpFoundationRequestHandler'); + ->setClass(JsonHttpFoundationRequestHandler::class); } } diff --git a/ElaoJsonHttpFormBundle.php b/ElaoJsonHttpFormBundle.php index 56c91b4..afebde0 100644 --- a/ElaoJsonHttpFormBundle.php +++ b/ElaoJsonHttpFormBundle.php @@ -2,13 +2,13 @@ namespace Elao\Bundle\JsonHttpFormBundle; +use Elao\Bundle\JsonHttpFormBundle\DependencyInjection\Compiler\OverrideRequestHandlerCompilerPass; use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Elao\Bundle\JsonHttpFormBundle\DependencyInjection\Compiler\OverrideRequestHandlerCompilerPass; class ElaoJsonHttpFormBundle extends Bundle { - public function build(ContainerBuilder $container) + public function build(ContainerBuilder $container): void { parent::build($container); diff --git a/Form/RequestHandler/JsonHttpFoundationRequestHandler.php b/Form/RequestHandler/JsonHttpFoundationRequestHandler.php index 231c88b..c49a383 100644 --- a/Form/RequestHandler/JsonHttpFoundationRequestHandler.php +++ b/Form/RequestHandler/JsonHttpFoundationRequestHandler.php @@ -17,9 +17,7 @@ */ class JsonHttpFoundationRequestHandler extends HttpFoundationRequestHandler { - /** - * @var ServerParams - */ + /** @var ServerParams */ private $serverParams; /** @@ -29,9 +27,6 @@ class JsonHttpFoundationRequestHandler extends HttpFoundationRequestHandler */ private static $bodyMethods = ['POST', 'PUT', 'PATCH', 'DELETE']; - /** - * {@inheritdoc} - */ public function __construct(ServerParams $serverParams = null) { parent::__construct($serverParams); @@ -39,20 +34,22 @@ public function __construct(ServerParams $serverParams = null) $this->serverParams = $serverParams ?: new ServerParams(); } - /** - * {@inheritdoc} - */ - public function handleRequest(FormInterface $form, $request = null) + public function handleRequest(FormInterface $form, $request = null): void { if (!$request instanceof Request) { - throw new UnexpectedTypeException($request, 'Symfony\Component\HttpFoundation\Request'); + throw new UnexpectedTypeException($request, Request::class); } - if ($request->getContentType() === 'json' && in_array($request->getMethod(), static::$bodyMethods)) { - return $this->handleJsonRequest($form, $request); - } else { - return parent::handleRequest($form, $request); + if ( + 'json' === $request->getContentType() + && in_array($request->getMethod(), static::$bodyMethods, false) + ) { + $this->handleJsonRequest($form, $request); + + return; } + + parent::handleRequest($form, $request); } /** @@ -61,23 +58,27 @@ public function handleRequest(FormInterface $form, $request = null) * @param FormInterface $form * @param Request $request */ - protected function handleJsonRequest(FormInterface $form, Request $request) + protected function handleJsonRequest(FormInterface $form, Request $request): void { if ($this->isContentSizeValid($form)) { - $name = $form->getName(); + $name = $form->getName(); $content = json_decode($request->getContent(), true); if (json_last_error() !== JSON_ERROR_NONE) { $form->submit(null, false); - $form->addError(new FormError(sprintf( - 'The given JSON content could not be parsed: %s', - json_last_error_msg() - ))); + $form->addError( + new FormError( + sprintf( + 'The given JSON content could not be parsed: %s', + json_last_error_msg() + ) + ) + ); return; } - if ('' === $name || $request->getMethod() === 'DELETE') { + if ('' === $name || 'DELETE' === $request->getMethod()) { $data = $content; } else { // Don't submit if the form's name does not exist in the request @@ -96,13 +97,14 @@ protected function handleJsonRequest(FormInterface $form, Request $request) * Check content size * * Code from {@link HttpFoundationRequestHandler} max size verification. - * @author Bernhard Schussek * * @param FormInterface $form * * @return boolean + * + * @author Bernhard Schussek */ - protected function isContentSizeValid(FormInterface $form) + protected function isContentSizeValid(FormInterface $form): bool { // Mark the form with an error if the uploaded size was too large // This is done here and not in FormValidator because $_POST is @@ -110,15 +112,16 @@ protected function isContentSizeValid(FormInterface $form) $contentLength = $this->serverParams->getContentLength(); $maxContentLength = $this->serverParams->getPostMaxSize(); - if (!empty($maxContentLength) && $contentLength > $maxContentLength) { + if (null !== $maxContentLength && $contentLength > $maxContentLength) { // Submit the form, but don't clear the default values $form->submit(null, false); - - $form->addError(new FormError( - $form->getConfig()->getOption('post_max_size_message'), - null, - array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize()) - )); + $form->addError( + new FormError( + $form->getConfig()->getOption('post_max_size_message'), + null, + ['{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize()] + ) + ); return false; } diff --git a/Tests/Form/RequestHandler/RequestHandlerTest.php b/Tests/Form/RequestHandler/RequestHandlerTest.php index dc68f57..922a42f 100644 --- a/Tests/Form/RequestHandler/RequestHandlerTest.php +++ b/Tests/Form/RequestHandler/RequestHandlerTest.php @@ -2,91 +2,78 @@ namespace Elao\Bundle\JsonHttpFormBundle\Tests\Form\RequestHandler; -use Symfony\Component\Form\FormFactory; +use Elao\Bundle\JsonHttpFormBundle\Form\RequestHandler\JsonHttpFoundationRequestHandler; +use Symfony\Component\Form\Extension\Core\Type\{FormType, TextType, ChoiceType}; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\Forms; +use Symfony\Component\Form\Util\ServerParams; use Symfony\Component\HttpFoundation\Request; -use Elao\Bundle\JsonHttpFormBundle\Form\RequestHandler\JsonHttpFoundationRequestHandler; -use Symfony\Component\Form\Extension\Core\Type\FormType; -use Symfony\Component\Form\Extension\Core\Type\TextType; -use Symfony\Component\Form\Extension\Core\Type\ChoiceType; -class RequestHandlerTest extends \PHPUnit_Framework_TestCase +class RequestHandlerTest extends TestCase { - /** - * {@inheritdoc} - */ - protected function setUp() + private $requestHandler; + private $factory; + + protected function setUp(): void { - $this->serverParams = $this->getMock( - 'Symfony\Component\Form\Util\ServerParams', - ['getNormalizedIniPostMaxSize', 'getContentLength'] - ); - $this->requestHandler = new JsonHttpFoundationRequestHandler($this->serverParams); - $this->factory = Forms::createFormFactoryBuilder()->getFormFactory(); + $serverParams = $this->createMock(ServerParams::class); + $this->requestHandler = new JsonHttpFoundationRequestHandler($serverParams); + $this->factory = Forms::createFormFactoryBuilder()->getFormFactory(); } - /** - * Test JSON POST request - */ - public function testJsonPostRequest() + public function testJsonPostRequest(): void { - $form = $this->getSampleForm(); - $data = $this->getSampleData(); - $request = new Request([], [], [], [], [], [ - 'REQUEST_METHOD' => 'POST', + $form = $this->getSampleForm(); + $data = $this->getSampleData(); + $request = new Request( + [], [], [], [], [], [ + 'REQUEST_METHOD' => 'POST', 'HTTP_CONTENT_TYPE' => 'application/json', - ], json_encode(['rocket' => $data])); + ], json_encode(['rocket' => $data]) + ); $this->requestHandler->handleRequest($form, $request); $this->assertEquals($data, $form->getData()); } - /** - * Test Classic POST request - */ - public function testClassicPostRequest() + public function testClassicPostRequest(): void { - $form = $this->getSampleForm(); - $data = $this->getSampleData(); + $form = $this->getSampleForm(); + $data = $this->getSampleData(); $request = new Request([], ['rocket' => $data], [], [], [], ['REQUEST_METHOD' => 'POST']); $this->requestHandler->handleRequest($form, $request); $this->assertEquals($data, $form->getData()); } - /** - * Get sample data - * - * @return array - */ - private function getSampleData() + private function getSampleData(): array { return [ - 'name' => "Méliès", - 'colors' => ['brown', 'pink'] + 'name' => 'Méliès', + 'colors' => ['brown', 'pink'], ]; } - /** - * Get sample form - * - * @return Form - */ - private function getSampleForm() + private function getSampleForm(): FormInterface { return $this->factory ->createNamed('rocket', FormType::class, [], []) ->add('name', TextType::class) - ->add('colors', ChoiceType::class, [ - 'multiple' => true, - 'choices' => [ - 'White' => 'white', - 'Orange' => 'orange', - 'Blonde' => 'blonde', - 'Pink' => 'pink', - 'Blue' => 'blue', - 'Brown' => 'brown', + ->add( + 'colors', + ChoiceType::class, + [ + 'multiple' => true, + 'choices' => [ + 'White' => 'white', + 'Orange' => 'orange', + 'Blonde' => 'blonde', + 'Pink' => 'pink', + 'Blue' => 'blue', + 'Brown' => 'brown', + ], ] - ]); + ); } } diff --git a/composer.json b/composer.json index 1624c08..926afd1 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "require": { "php": "^7.1", "symfony/framework-bundle": "~3.0|~4.0|~5.0", - "symfony/form": "~3.0|~4.0|~5.0" + "symfony/form": "~3.0|~4.0|~5.0", + "ext-json": "*" }, "autoload": { "psr-4": { @@ -22,7 +23,7 @@ } }, "require-dev": { - "phpunit/phpunit": "~4.5" + "phpunit/phpunit": "^7.5" }, "extra": { "branch-alias": {