Починил тесты

master
Sipachev Igor 3 years ago
parent 36d4a935b2
commit 87c31135cd
  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();
@ -43,7 +45,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);
} }
} }
} }

@ -18,7 +18,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
@ -34,14 +34,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(3, $request->get('form')); $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