Skip to content
Permalink
Browse files

Improved `BodySubscriber` to handle eg. null content.

  • Loading branch information...
tarlepp committed Jul 21, 2019
1 parent eed0d66 commit fc56c1cf5e43e924f9c44aefce3eed03db3e4e00
Showing with 34 additions and 2 deletions.
  1. +4 −2 src/EventSubscriber/BodySubscriber.php
  2. +30 −0 tests/Integration/EventSubscriber/BodySubscriberTest.php
@@ -101,10 +101,12 @@ private function isJsonRequest(Request $request): bool
*/
private function transformJsonBody(Request $request): void
{
/** @var string $content */
$data = null;
$content = $request->getContent();
$data = JSON::decode($content, true);
if (is_string($content)) {
$data = JSON::decode($content, true);
}
if (is_array($data)) {
$request->request->replace($data);
@@ -11,7 +11,9 @@
use App\EventSubscriber\BodySubscriber;
use Generator;
use LogicException;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -104,6 +106,34 @@ public function testThatInvalidJsonContentThrowsAnException(): void
$subscriber->onKernelRequest($event);
}
public function testThatWithNullBodyReplaceIsNotCalled(): void
{
static::bootKernel();
/**
* @var MockObject|Request $request
* @var MockObject|ParameterBag $paramegerBag
*/
$request = $this->getMockBuilder(Request::class)->getMock();
$paramegerBag = $this->getMockBuilder(ParameterBag::class)->getMock();
$request->request = $paramegerBag;
$request
->expects(static::exactly(2))
->method('getContent')
->willReturn(null);
$paramegerBag
->expects(static::never())
->method('replace');
$event = new GetResponseEvent(static::$kernel, $request, HttpKernelInterface::MASTER_REQUEST);
$subscriber = new BodySubscriber();
$subscriber->onKernelRequest($event);
}
/**
* @return Generator
*/

0 comments on commit fc56c1c

Please sign in to comment.
You can’t perform that action at this time.