diff --git a/.gitattributes b/.gitattributes index fc86054..46cbe14 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,5 @@ # Files that composer should ignore when downloading package +/.* export-ignore /Dockerfile export-ignore /Makefile export-ignore /README.md export-ignore diff --git a/.gitignore b/.gitignore index 405484d..4c6bc4c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /var /vendor /composer.lock +.*.cache diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..0d154ff --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,14 @@ +setRules([ + '@Symfony' => true, + '@Symfony:risky' => true, + ]) + ->setRiskyAllowed(true) + ->setFinder( + PhpCsFixer\Finder::create() + ->in(__DIR__.'/src') + ->in(__DIR__.'/tests') + ) +; diff --git a/Dockerfile b/Dockerfile index be3eece..e6eb5d6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,4 +2,7 @@ FROM php:8.1-fpm-alpine RUN curl -s https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer +RUN curl -s https://cs.symfony.com/download/php-cs-fixer-v3.phar -o /usr/local/bin/php-cs-fixer \ + && chmod +x /usr/local/bin/php-cs-fixer + WORKDIR /app diff --git a/Makefile b/Makefile index c469bcc..538b38e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ clear-cache: rm -rf var +php-cs-fixer: clear-cache + docker-compose run --rm -T php /usr/local/bin/php-cs-fixer fix --no-interaction --verbose --dry-run + phpunit: clear-cache docker-compose run --rm -T php /usr/local/bin/php /app/vendor/bin/phpunit diff --git a/README.md b/README.md index ea296f1..11510ef 100644 --- a/README.md +++ b/README.md @@ -45,10 +45,10 @@ public function indexAction(Request $request) Per default request content will be transformed only for requests with content type `json` or `jsonld`. -but you can stil configure it with +But you can stil configure it with ``` yaml -# serices.yaml +# config/serices.yaml json_request: content_types: diff --git a/composer.json b/composer.json index 8d04206..fc7a90c 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,13 @@ "phpunit/php-code-coverage": "^9.2", "phpunit/phpunit": "^9.5", "symfony/browser-kit": "^5.2 || ^6.0", + "symfony/phpunit-bridge": "^5.2 || ^6.2", "symfony/yaml": "^5.2 || ^6.0" }, + "scripts": { + "csfixer": "php-cs-fixer fix --verbose --dry-run --diff", + "csfixer-fix": "php-cs-fixer fix --verbose" + }, "config": { "preferred-install": "dist", "sort-packages": true diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 8a80d2e..16d882e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -22,6 +22,10 @@ + + + + src diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 9d86ebb..ece96ef 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -1,4 +1,6 @@ -getContent(), true, 512, \JSON_THROW_ON_ERROR); + $data = json_decode((string) $request->getContent(), true, 512, \JSON_THROW_ON_ERROR); if (\is_array($data)) { $request->request->replace($data); @@ -37,6 +39,8 @@ public function onKernelRequest(RequestEvent $event): void private function supports(Request $request): bool { - return in_array($request->getContentType(), $this->contentTypes, true) && $request->getContent(); + $contentType = method_exists($request, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType(); + + return \in_array($contentType, $this->contentTypes, true) && $request->getContent(); } } diff --git a/src/JsonRequestBundle.php b/src/JsonRequestBundle.php index a04e224..a55d9c7 100644 --- a/src/JsonRequestBundle.php +++ b/src/JsonRequestBundle.php @@ -1,4 +1,6 @@ - 'baz']); + $body = json_encode(['foo' => 'baz']); $response = $this->sendRequest($body); @@ -19,7 +21,7 @@ public function testTransformRequest(): void public function testTransformJsonldRequest(): void { - $body = \json_encode(['foo' => 'baz']); + $body = json_encode(['foo' => 'baz']); $response = $this->sendRequest($body, 'application/ld+json'); @@ -28,7 +30,7 @@ public function testTransformJsonldRequest(): void public function testTransformSoneOtherTypeRequest(): void { - $body = \json_encode(['foo' => 'baz']); + $body = json_encode(['foo' => 'baz']); // add content type. they are stored in static Request::formats variable (new Request())->setFormat('someother', 'application/some+other+type'); @@ -48,7 +50,7 @@ public function testInvalidBody(): void public function testInvalidContentType(): void { - $body = \json_encode(['foo' => 'baz']); + $body = json_encode(['foo' => 'baz']); $response = $this->sendRequest($body, 'application/javascript'); @@ -56,10 +58,7 @@ public function testInvalidContentType(): void } /** - * @param mixed $body - * @param string $contentType - * - * @return Response + * @param mixed $body */ private function sendRequest($body, string $contentType = 'application/json'): Response { diff --git a/tests/DependencyInjectionTest.php b/tests/DependencyInjectionTest.php index bf24a51..009a4c1 100644 --- a/tests/DependencyInjectionTest.php +++ b/tests/DependencyInjectionTest.php @@ -1,4 +1,6 @@ -load([$config], $this->containerBuilder); } - private function assertContentTypes(array $expected) + private function assertContentTypes(array $expected): void { $listenerDefinition = $this->containerBuilder->findDefinition(RequestTransformerListener::class); $this->assertEquals($expected, $listenerDefinition->getArgument(0)); diff --git a/tests/EventListener/RequestTransformerListenerTest.php b/tests/EventListener/RequestTransformerListenerTest.php index 3c0a623..0244b37 100644 --- a/tests/EventListener/RequestTransformerListenerTest.php +++ b/tests/EventListener/RequestTransformerListenerTest.php @@ -12,8 +12,17 @@ class RequestTransformerListenerTest extends TestCase { + private static string $getContentTypeFormat = 'getContentTypeFormat'; + private RequestTransformerListener $listener; + public static function setUpBeforeClass(): void + { + if (!method_exists(Request::class, 'getContentTypeFormat')) { + self::$getContentTypeFormat = 'getContentType'; + } + } + protected function setUp(): void { $this->listener = new RequestTransformerListener(['json']); @@ -23,7 +32,7 @@ public function testOnKernelRequestWithInvalidJson(): void { $request = $this->createMock(Request::class); $request->method('getContent')->willReturn('{"test": "val}'); - $request->method('getContentType')->willReturn("json"); + $request->method(self::$getContentTypeFormat)->willReturn('json'); $requestEvent = $this->createMock(RequestEvent::class); $requestEvent->method('getRequest')->willReturn($request); @@ -31,6 +40,7 @@ public function testOnKernelRequestWithInvalidJson(): void $requestEvent->expects($this->once())->method('setResponse')->willReturnCallback(function ($resp) { $this->assertInstanceOf(JsonResponse::class, $resp); $this->assertEquals(Response::HTTP_BAD_REQUEST, $resp->getStatusCode()); + $this->assertIsString($resp->getContent()); $this->assertJson($resp->getContent()); }); @@ -43,7 +53,7 @@ public function testOnKernelRequestWithValidJson(): void $request = $this->createMock(Request::class); $request->method('getContent')->willReturn('{"test": "val"}'); - $request->method('getContentType')->willReturn('json'); + $request->method(self::$getContentTypeFormat)->willReturn('json'); $request->request = $inputBag; $requestEvent = $this->createMock(RequestEvent::class); diff --git a/tests/Fixture/App/Controller/ExampleController.php b/tests/Fixture/App/Controller/ExampleController.php index 098a4ee..fa420e4 100644 --- a/tests/Fixture/App/Controller/ExampleController.php +++ b/tests/Fixture/App/Controller/ExampleController.php @@ -1,13 +1,14 @@ -request->all()); diff --git a/tests/Fixture/App/Kernel.php b/tests/Fixture/App/Kernel.php index 74d2d2b..8fdd404 100644 --- a/tests/Fixture/App/Kernel.php +++ b/tests/Fixture/App/Kernel.php @@ -1,11 +1,13 @@ -