Тестирование entity transformer

master
Sipachev Igor 3 years ago
parent d99f533981
commit f0683ef40d
  1. 5
      src/DependencyInjection/RequestExtension.php
  2. 9
      src/EventListener/RequestListener.php
  3. 2
      src/RequestBundle.php
  4. 4
      src/Resources/config/services.yaml
  5. 4
      src/Service/Transformer/EntityTransformer.php
  6. 4
      src/Service/Transformer/ServiceResolver.php

@ -2,6 +2,7 @@
namespace Rinsvent\RequestBundle\DependencyInjection; namespace Rinsvent\RequestBundle\DependencyInjection;
use Rinsvent\RequestBundle\Service\Transformer\AbstractTransformer;
use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Extension\Extension;
@ -13,5 +14,7 @@ class RequestExtension extends Extension
{ {
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yaml'); $loader->load('services.yaml');
// $container->findTaggedServiceIds(AbstractTransformer::class);
} }
} }

@ -3,10 +3,12 @@
namespace Rinsvent\RequestBundle\EventListener; namespace Rinsvent\RequestBundle\EventListener;
use Rinsvent\Data2DTO\Data2DtoConverter; use Rinsvent\Data2DTO\Data2DtoConverter;
use Rinsvent\Data2DTO\Resolver\TransformerResolverStorage;
use Rinsvent\RequestBundle\Annotation\RequestDTO; use Rinsvent\RequestBundle\Annotation\RequestDTO;
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;
use Rinsvent\RequestBundle\Service\Transformer\ServiceResolver;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\Event\RequestEvent;
@ -15,10 +17,15 @@ use Symfony\Component\Validator\Validation;
// todo базовая заготовка. Требуется рефакторинг // todo базовая заготовка. Требуется рефакторинг
class RequestListener class RequestListener
{ {
public const REQUEST_DATA = 'request_data'; public function __construct(
private ServiceResolver $serviceResolver
) {}
public function onKernelRequest(RequestEvent $event) public function onKernelRequest(RequestEvent $event)
{ {
$storage = TransformerResolverStorage::getInstance();
$storage->add(ServiceResolver::TYPE, $this->serviceResolver);
$request = $event->getRequest(); $request = $event->getRequest();
$controller = $request->get('_controller'); $controller = $request->get('_controller');
if (is_string($controller)) { if (is_string($controller)) {

@ -7,4 +7,4 @@ use Symfony\Component\HttpKernel\Bundle\Bundle;
class RequestBundle extends Bundle class RequestBundle extends Bundle
{ {
} }

@ -5,10 +5,12 @@ services:
autowire: true autowire: true
Rinsvent\RequestBundle\EventListener\RequestListener: Rinsvent\RequestBundle\EventListener\RequestListener:
autowire: true
tags: tags:
- { name: kernel.event_listener, event: kernel.request } - { name: kernel.event_listener, event: kernel.request }
Rinsvent\RequestBundle\Service\Transformer\:
resource: '../../../src/Service/Transformer'
Rinsvent\RequestBundle\Service\Transformer\ServiceResolver: Rinsvent\RequestBundle\Service\Transformer\ServiceResolver:
bind: bind:
$transformerLocator: !tagged_locator { tag: 'rinsvent.transformer', index_by: 'key', default_index_method: 'getLocatorKey' } $transformerLocator: !tagged_locator { tag: 'rinsvent.transformer', index_by: 'key', default_index_method: 'getLocatorKey' }

@ -17,10 +17,10 @@ class EntityTransformer extends AbstractTransformer
*/ */
public function transform(&$data, Meta $meta): void public function transform(&$data, Meta $meta): void
{ {
if (!is_int($data)) { if (!is_string($data)) {
return; return;
} }
$repository = $this->em->getRepository($meta->class); $repository = $this->em->getRepository($meta->class);
$data = $repository->find((int)$data); $data = $repository->find((string)$data);
} }
} }

@ -9,6 +9,8 @@ use Symfony\Component\DependencyInjection\ServiceLocator;
class ServiceResolver implements TransformerResolverInterface class ServiceResolver implements TransformerResolverInterface
{ {
public const TYPE = 'service';
public function __construct( public function __construct(
private ServiceLocator $transformerLocator private ServiceLocator $transformerLocator
) {} ) {}
@ -18,4 +20,4 @@ class ServiceResolver implements TransformerResolverInterface
$transformerClass = $meta::class . 'Transformer'; $transformerClass = $meta::class . 'Transformer';
return $this->transformerLocator->get($transformerClass); return $this->transformerLocator->get($transformerClass);
} }
} }

Loading…
Cancel
Save