Merge remote-tracking branch 'origin/master'

# Conflicts:
#	src/EventListener/RequestListener.php
#	tests/unit/Listener/FillTest.php
master
Rinsvent 3 years ago
commit 6886244256
  1. 4
      src/EventListener/RequestListener.php
  2. 6
      tests/unit/Listener/FillTest.php
  3. 14
      tests/unit/Listener/fixtures/FillTest/BuyRequest.php
  4. 9
      tests/unit/Listener/fixtures/FillTest/Controller.php

@ -17,6 +17,8 @@ use Symfony\Component\Validator\Validation;
// todo базовая заготовка. Требуется рефакторинг // todo базовая заготовка. Требуется рефакторинг
class RequestListener class RequestListener
{ {
public const REQUEST_DATA = 'request_data';
public function onKernelRequest(RequestEvent $event) public function onKernelRequest(RequestEvent $event)
{ {
$request = $event->getRequest(); $request = $event->getRequest();
@ -39,7 +41,7 @@ class RequestListener
if ($errorCollection->hasErrors()) { if ($errorCollection->hasErrors()) {
$event->setResponse(new JsonResponse(['errors' => $errorCollection->format()], Response::HTTP_BAD_REQUEST)); $event->setResponse(new JsonResponse(['errors' => $errorCollection->format()], Response::HTTP_BAD_REQUEST));
} else { } else {
$request->attributes->set($requestDTOInstance::class, $requestDTOInstance); $request->attributes->set(self::REQUEST_DATA, $requestDTOInstance);
} }
} }
} }

@ -19,7 +19,7 @@ use Symfony\Component\Routing\RouteCollection;
use Rinsvent\RequestBundle\EventListener\RequestListener; use Rinsvent\RequestBundle\EventListener\RequestListener;
class GenerateTest extends \Codeception\Test\Unit class FillTest extends \Codeception\Test\Unit
{ {
/** /**
* @var \UnitTester * @var \UnitTester
@ -35,14 +35,14 @@ class GenerateTest extends \Codeception\Test\Unit
} }
// tests // tests
public function testGenerateEnum() public function testSuccessFillRequestData()
{ {
$request = Request::create('/hello/igor', 'GET', [ $request = Request::create('/hello/igor', 'GET', [
'surname' => 'Surname' 'surname' => 'Surname'
]); ]);
$response = $this->send($request); $response = $this->send($request);
$this->assertEquals('Surname', $request->get(HelloRequest::class)->surname); $this->assertEquals('Surname', $request->get(RequestListener::REQUEST_DATA)->surname);
} }
private function send(Request $request): Response private function send(Request $request): Response

@ -0,0 +1,14 @@
<?php
namespace Rinsvent\RequestBundle\Tests\unit\Listener\fixtures\FillTest;
use Symfony\Component\Validator\Constraints as Assert;
class BuyRequest
{
#[Assert\NotBlank]
public string $surname;
public int $age;
#[Assert\Email]
public string $email;
}

@ -15,4 +15,13 @@ class Controller
sprintf("Hello %s", $request->get('name')) sprintf("Hello %s", $request->get('name'))
); );
} }
#[RequestDTO(className: HelloRequest::class, jsonPath: '$.user')]
#[RequestDTO(className: BuyRequest::class, jsonPath: '$.guest')]
public function hello2(Request $request)
{
return new Response(
sprintf("Hello %s", $request->get('name'))
);
}
} }

Loading…
Cancel
Save