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

master
Sipachev Igor 3 years ago
parent 36d4a935b2
commit 87c31135cd
  1. 6
      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 базовая заготовка. Требуется рефакторинг
class RequestListener
{
public const REQUEST_DATA = 'request_data';
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
@ -25,7 +27,7 @@ class RequestListener
$controller = explode('::', $controller);
$method = new ReflectionMethod($controller[0], $controller[1]);
}
if (is_callable($controller)){
if (is_callable($controller)) {
$method = new ReflectionMethod($controller[0], $controller[1]);
}
if (!isset($method)) {
@ -43,7 +45,7 @@ class RequestListener
if ($errorCollection->hasErrors()) {
$event->setResponse(new JsonResponse(['errors' => $errorCollection->format()], Response::HTTP_BAD_REQUEST));
} 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;
class GenerateTest extends \Codeception\Test\Unit
class FillTest extends \Codeception\Test\Unit
{
/**
* @var \UnitTester
@ -34,14 +34,14 @@ class GenerateTest extends \Codeception\Test\Unit
}
// tests
public function testGenerateEnum()
public function testSuccessFillRequestData()
{
$request = Request::create('/hello/igor', 'GET', [
'surname' => 'Surname'
]);
$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

@ -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'))
);
}
#[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