Рефакторинг

master
Rinsvent 3 years ago
parent 4151075d89
commit 8f266d18eb
  1. 4
      src/Annotation/PropertyPath.php
  2. 8
      src/EventListener/RequestListener.php
  3. 46
      tests/_support/Helper/Unit.php
  4. 25
      tests/unit/Listener/FillTest.php

@ -3,9 +3,9 @@
namespace Rinsvent\RequestBundle\Annotation; namespace Rinsvent\RequestBundle\Annotation;
#[\Attribute] #[\Attribute]
class HeaderKey class PropertyPath
{ {
public function __construct( public function __construct(
public string $key public string $path
) {} ) {}
} }

@ -5,8 +5,8 @@ namespace Rinsvent\RequestBundle\EventListener;
use JMS\Serializer\SerializerBuilder; use JMS\Serializer\SerializerBuilder;
use ReflectionObject; use ReflectionObject;
use Rinsvent\AttributeExtractor\PropertyExtractor; use Rinsvent\AttributeExtractor\PropertyExtractor;
use Rinsvent\RequestBundle\Annotation\PropertyPath;
use Rinsvent\RequestBundle\Annotation\RequestDTO; use Rinsvent\RequestBundle\Annotation\RequestDTO;
use Rinsvent\RequestBundle\Annotation\HeaderKey;
use Rinsvent\RequestBundle\DTO\Error; use Rinsvent\RequestBundle\DTO\Error;
use Rinsvent\RequestBundle\DTO\ErrorCollection; use Rinsvent\RequestBundle\DTO\ErrorCollection;
use Rinsvent\AttributeExtractor\MethodExtractor; use Rinsvent\AttributeExtractor\MethodExtractor;
@ -90,9 +90,9 @@ class RequestListener
$properties = $reflectionObject->getProperties(); $properties = $reflectionObject->getProperties();
foreach ($properties as $property) { foreach ($properties as $property) {
$propertyExtractor = new PropertyExtractor($object::class, $property->getName()); $propertyExtractor = new PropertyExtractor($object::class, $property->getName());
/** @var HeaderKey $headerKey */ /** @var PropertyPath $propertyPath */
if ($headerKey = $propertyExtractor->fetch(HeaderKey::class)) { if ($propertyPath = $propertyExtractor->fetch(PropertyPath::class)) {
$value = $data[strtolower($headerKey->key)][0] ?? null; $value = $data[strtolower($propertyPath->path)][0] ?? null;
} else { } else {
$value = $data[$property->getName()] ?? null; $value = $data[$property->getName()] ?? null;
} }

@ -4,21 +4,43 @@ namespace Rinsvent\RequestBundle\Tests\Helper;
// here you can define custom actions // here you can define custom actions
// all public methods declared in helper class will be available in $I // all public methods declared in helper class will be available in $I
use Rinsvent\ApiSDKGenerator\DTO\Writer\Config; use Rinsvent\RequestBundle\EventListener\RequestListener;
use Rinsvent\ApiSDKGenerator\Service\Writer; 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 class Unit extends \Codeception\Module
{ {
public function getWriter(string $lang = 'php'): Writer public function send(Request $request): Response
{ {
return new Writer( $routes = new RouteCollection();
new Config( $controller = new Controller();
dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'templates', $routes->add('hello', new Route('/hello/{name}', [
dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'var/tests/cache', '_controller' => [$controller, 'hello']
$lang, ]
dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'var/tests/result', ));
'Rinsvent\\AuthSDK'
) $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;
} }
} }

@ -40,31 +40,8 @@ class FillTest extends \Codeception\Test\Unit
$request = Request::create('/hello/igor', 'GET', [ $request = Request::create('/hello/igor', 'GET', [
'surname' => 'Surname' 'surname' => 'Surname'
]); ]);
$response = $this->send($request); $response = $this->tester->send($request);
$this->assertEquals('Surname', $request->get(RequestListener::REQUEST_DATA)->surname); $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;
}
} }

Loading…
Cancel
Save