Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Files that composer should ignore when downloading package
/.* export-ignore
/Dockerfile export-ignore
/Makefile export-ignore
/README.md export-ignore
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/var
/vendor
/composer.lock
.*.cache
14 changes: 14 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return (new PhpCsFixer\Config())
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
])
->setRiskyAllowed(true)
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
->in(__DIR__.'/tests')
)
;
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
</testsuite>
</testsuites>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>

<coverage>
<include>
<directory>src</directory>
Expand Down
4 changes: 3 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SymfonyBundles\JsonRequestBundle\DependencyInjection;

Expand Down
6 changes: 4 additions & 2 deletions src/DependencyInjection/JsonRequestExtension.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SymfonyBundles\JsonRequestBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\HttpKernel\KernelEvents;
use SymfonyBundles\JsonRequestBundle\EventListener\RequestTransformerListener;

final class JsonRequestExtension extends Extension
Expand Down
12 changes: 8 additions & 4 deletions src/EventListener/RequestTransformerListener.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SymfonyBundles\JsonRequestBundle\EventListener;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;

final class RequestTransformerListener
Expand All @@ -25,7 +27,7 @@ public function onKernelRequest(RequestEvent $event): void
}

try {
$data = \json_decode((string) $request->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);
Expand All @@ -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();
}
}
4 changes: 3 additions & 1 deletion src/JsonRequestBundle.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SymfonyBundles\JsonRequestBundle;

Expand Down
19 changes: 9 additions & 10 deletions tests/Controller/ExampleControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SymfonyBundles\JsonRequestBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

final class ExampleControllerTest extends WebTestCase
{
public function testTransformRequest(): void
{
$body = \json_encode(['foo' => 'baz']);
$body = json_encode(['foo' => 'baz']);

$response = $this->sendRequest($body);

Expand All @@ -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');

Expand All @@ -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');
Expand All @@ -48,18 +50,15 @@ public function testInvalidBody(): void

public function testInvalidContentType(): void
{
$body = \json_encode(['foo' => 'baz']);
$body = json_encode(['foo' => 'baz']);

$response = $this->sendRequest($body, 'application/javascript');

$this->assertSame('[]', $response->getContent());
}

/**
* @param mixed $body
* @param string $contentType
*
* @return Response
* @param mixed $body
*/
private function sendRequest($body, string $contentType = 'application/json'): Response
{
Expand Down
6 changes: 4 additions & 2 deletions tests/DependencyInjectionTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SymfonyBundles\JsonRequestBundle\Tests;

Expand Down Expand Up @@ -100,7 +102,7 @@ public function testContentTypesExpectedScalarArrayGiven(): void
$extension->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));
Expand Down
14 changes: 12 additions & 2 deletions tests/EventListener/RequestTransformerListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -23,14 +32,15 @@ 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);

$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());
});

Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions tests/Fixture/App/Controller/ExampleController.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SymfonyBundles\JsonRequestBundle\Tests\Fixture\App\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

final class ExampleController
{

public function index(Request $request): JsonResponse
{
return new JsonResponse($request->request->all());
Expand Down
8 changes: 5 additions & 3 deletions tests/Fixture/App/Kernel.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SymfonyBundles\JsonRequestBundle\Tests\Fixture\App;

use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

final class Kernel extends BaseKernel
{
Expand Down