From 8f266d18ebfc1031d79cb0f3a4ad82f95fbb6a8d Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Wed, 28 Jul 2021 23:27:16 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{HeaderKey.php => PropertyPath.php} | 4 +- src/EventListener/RequestListener.php | 8 ++-- tests/_support/Helper/Unit.php | 46 ++++++++++++++----- tests/unit/Listener/FillTest.php | 25 +--------- 4 files changed, 41 insertions(+), 42 deletions(-) rename src/Annotation/{HeaderKey.php => PropertyPath.php} (69%) diff --git a/src/Annotation/HeaderKey.php b/src/Annotation/PropertyPath.php similarity index 69% rename from src/Annotation/HeaderKey.php rename to src/Annotation/PropertyPath.php index 45eda63..288cbb3 100644 --- a/src/Annotation/HeaderKey.php +++ b/src/Annotation/PropertyPath.php @@ -3,9 +3,9 @@ namespace Rinsvent\RequestBundle\Annotation; #[\Attribute] -class HeaderKey +class PropertyPath { public function __construct( - public string $key + public string $path ) {} } \ No newline at end of file diff --git a/src/EventListener/RequestListener.php b/src/EventListener/RequestListener.php index cd602fc..3b86b85 100644 --- a/src/EventListener/RequestListener.php +++ b/src/EventListener/RequestListener.php @@ -5,8 +5,8 @@ namespace Rinsvent\RequestBundle\EventListener; use JMS\Serializer\SerializerBuilder; use ReflectionObject; use Rinsvent\AttributeExtractor\PropertyExtractor; +use Rinsvent\RequestBundle\Annotation\PropertyPath; use Rinsvent\RequestBundle\Annotation\RequestDTO; -use Rinsvent\RequestBundle\Annotation\HeaderKey; use Rinsvent\RequestBundle\DTO\Error; use Rinsvent\RequestBundle\DTO\ErrorCollection; use Rinsvent\AttributeExtractor\MethodExtractor; @@ -90,9 +90,9 @@ class RequestListener $properties = $reflectionObject->getProperties(); foreach ($properties as $property) { $propertyExtractor = new PropertyExtractor($object::class, $property->getName()); - /** @var HeaderKey $headerKey */ - if ($headerKey = $propertyExtractor->fetch(HeaderKey::class)) { - $value = $data[strtolower($headerKey->key)][0] ?? null; + /** @var PropertyPath $propertyPath */ + if ($propertyPath = $propertyExtractor->fetch(PropertyPath::class)) { + $value = $data[strtolower($propertyPath->path)][0] ?? null; } else { $value = $data[$property->getName()] ?? null; } diff --git a/tests/_support/Helper/Unit.php b/tests/_support/Helper/Unit.php index 9b77d42..74dce91 100644 --- a/tests/_support/Helper/Unit.php +++ b/tests/_support/Helper/Unit.php @@ -4,21 +4,43 @@ namespace Rinsvent\RequestBundle\Tests\Helper; // here you can define custom actions // all public methods declared in helper class will be available in $I -use Rinsvent\ApiSDKGenerator\DTO\Writer\Config; -use Rinsvent\ApiSDKGenerator\Service\Writer; +use Rinsvent\RequestBundle\EventListener\RequestListener; +use Rinsvent\RequestBundle\Tests\unit\Listener\fixtures\FillTest\Controller; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Controller\ArgumentResolver; +use Symfony\Component\HttpKernel\Controller\ControllerResolver; +use Symfony\Component\HttpKernel\EventListener\RouterListener; +use Symfony\Component\HttpKernel\HttpKernel; +use Symfony\Component\Routing\Matcher\UrlMatcher; +use Symfony\Component\Routing\RequestContext; +use Symfony\Component\Routing\Route; +use Symfony\Component\Routing\RouteCollection; class Unit extends \Codeception\Module { - public function getWriter(string $lang = 'php'): Writer + public function send(Request $request): Response { - return new Writer( - new Config( - dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'templates', - dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'var/tests/cache', - $lang, - dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'var/tests/result', - 'Rinsvent\\AuthSDK' - ) - ); + $routes = new RouteCollection(); + $controller = new Controller(); + $routes->add('hello', new Route('/hello/{name}', [ + '_controller' => [$controller, 'hello'] + ] + )); + + $matcher = new UrlMatcher($routes, new RequestContext()); + $dispatcher = new EventDispatcher(); + $dispatcher->addSubscriber(new RouterListener($matcher, new RequestStack())); + $listener = new RequestListener(); + $dispatcher->addListener('kernel.request', [$listener, 'onKernelRequest']); + + $controllerResolver = new ControllerResolver(); + $argumentResolver = new ArgumentResolver(); + $kernel = new HttpKernel($dispatcher, $controllerResolver, new RequestStack(), $argumentResolver); + $response = $kernel->handle($request); + $response->send(); + return $response; } } diff --git a/tests/unit/Listener/FillTest.php b/tests/unit/Listener/FillTest.php index 085c04d..1651e54 100644 --- a/tests/unit/Listener/FillTest.php +++ b/tests/unit/Listener/FillTest.php @@ -40,31 +40,8 @@ class FillTest extends \Codeception\Test\Unit $request = Request::create('/hello/igor', 'GET', [ 'surname' => 'Surname' ]); - $response = $this->send($request); + $response = $this->tester->send($request); $this->assertEquals('Surname', $request->get(RequestListener::REQUEST_DATA)->surname); } - - private function send(Request $request): Response - { - $routes = new RouteCollection(); - $controller = new Controller(); - $routes->add('hello', new Route('/hello/{name}', [ - '_controller' => [$controller, 'hello'] - ] - )); - - $matcher = new UrlMatcher($routes, new RequestContext()); - $dispatcher = new EventDispatcher(); - $dispatcher->addSubscriber(new RouterListener($matcher, new RequestStack())); - $listener = new RequestListener(); - $dispatcher->addListener('kernel.request', [$listener, 'onKernelRequest']); - - $controllerResolver = new ControllerResolver(); - $argumentResolver = new ArgumentResolver(); - $kernel = new HttpKernel($dispatcher, $controllerResolver, new RequestStack(), $argumentResolver); - $response = $kernel->handle($request); - $response->send(); - return $response; - } }