Skip to content

Commit

Permalink
chore(test): allow to run symfony app in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joelwurtz committed Mar 22, 2024
1 parent 472649f commit 9e749fd
Show file tree
Hide file tree
Showing 35 changed files with 173 additions and 124 deletions.
4 changes: 4 additions & 0 deletions composer.json
Expand Up @@ -31,8 +31,12 @@
"symfony/filesystem": "^5.4 || ^6.0 || ^7.0",
"symfony/framework-bundle": "*",
"symfony/http-kernel": "^5.4 || ^6.0 || ^7.0",
"symfony/property-access": "^5.4 || ^6.0 || ^7.0",
"symfony/serializer": "*",
"symfony/stopwatch": "^7.0",
"symfony/twig-bundle": "^5.4 || ^6.0 || ^7.0",
"symfony/uid": "^5.4 || ^6.0 || ^7.0",
"symfony/web-profiler-bundle": "^5.4 || ^6.0 || ^7.0",
"symfony/yaml": "^5.4 || ^6.0 || ^7.0"
},
"suggest": {
Expand Down
9 changes: 0 additions & 9 deletions src/AutoMapper.php
Expand Up @@ -86,15 +86,6 @@ public function getMapper(string $source, string $target): MapperInterface
return $this->mapperRegistry[$className];
}

/**
* @template Source of object
* @template Target of object
*
* @param Source|array<mixed> $source
* @param class-string<Target>|'array'|array<mixed>|Target $target
*
* @return ($target is class-string|Target ? Target|null : array<mixed>|null)
*/
public function map(array|object $source, string|array|object $target, array $context = []): array|object|null
{
$sourceType = $targetType = null;
Expand Down
11 changes: 6 additions & 5 deletions src/AutoMapperInterface.php
Expand Up @@ -12,13 +12,14 @@
interface AutoMapperInterface
{
/**
* Maps data from a source to a target.
* @template Source of object
* @template Target of object
*
* @param array|object $source Any data object, which may be an object or an array
* @param 'array'|class-string|array|object $target To which type of data, or data, the source should be mapped
* @param array $context Mapper context
* @param Source|array<mixed> $source
* @param class-string<Target>|'array'|array<mixed>|Target $target
* @param array<mixed> $context
*
* @return array|object|null The mapped object
* @return ($target is class-string|Target ? Target|null : array<mixed>|null)
*/
public function map(array|object $source, string|array|object $target, array $context = []): array|object|null;
}
7 changes: 7 additions & 0 deletions src/Normalizer/AutoMapperNormalizer.php
Expand Up @@ -39,6 +39,13 @@ public function normalize(mixed $object, string $format = null, array $context =
return $this->autoMapper->map($object, 'array', $this->createAutoMapperContext($context));
}

/**
* @template T of object
*
* @param class-string<T> $type
*
* @return T|null
*/
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
{
return $this->autoMapper->map($data, $type, $this->createAutoMapperContext($context));
Expand Down
19 changes: 0 additions & 19 deletions tests/Bundle/Resources/App/AppKernel.php
Expand Up @@ -4,32 +4,13 @@

namespace AutoMapper\Tests\Bundle\Resources\App;

use AutoMapper\Symfony\Bundle\AutoMapperBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;

class AppKernel extends Kernel
{
use MicroKernelTrait;

public function registerBundles(): array
{
$bundles = [
new FrameworkBundle(),
new AutoMapperBundle(),
];

return $bundles;
}

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
$loader->load(__DIR__ . '/config.yml');
}

public function getProjectDir(): string
{
return __DIR__ . '/..';
Expand Down
42 changes: 42 additions & 0 deletions tests/Bundle/Resources/App/Controller/HomeController.php
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Resources\App\Controller;

use AutoMapper\AutoMapperInterface;
use AutoMapper\Tests\Bundle\Resources\App\Entity\FooMapTo;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class HomeController extends AbstractController
{
public function __construct(
private AutoMapperInterface $autoMapper,
#[Autowire('@serializer.normalizer.object')]
private NormalizerInterface $serializer
) {
}

#[Route('/')]
public function __invoke(Request $request): Response
{
$output = [];
$data = new FooMapTo('value');

for ($i = 0; $i < 10000; ++$i) {
if ($request->query->has('serializer')) {
$output[] = $this->serializer->normalize($data, 'json');
} else {
$output[] = $this->autoMapper->map($data, 'array');
}
}

return new JsonResponse($output);
}
}
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

class Address
{
Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

class AddressDTO
{
Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

class BaseUser
{
Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

class Cat extends Pet
{
Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

use AutoMapper\Attribute\MapToContext;

Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

class ClassWithPrivateProperty
{
Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

final class DTOWithEnum
{
Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

class Dog extends Pet
{
Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

use AutoMapper\Attribute\MapTo;
use AutoMapper\Tests\Fixtures\MapTo\Bar;
Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

class NestedObject
{
Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

use Money\Money;

Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

use Symfony\Component\Serializer\Annotation\DiscriminatorMap;

Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

enum SomeEnum: string
{
Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

class User extends BaseUser
{
Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Fixtures;
namespace AutoMapper\Tests\Bundle\Resources\App\Entity;

class UserDTO
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Bundle/Resources/App/Service/YearOfBirthTransformer.php
Expand Up @@ -8,8 +8,8 @@
use AutoMapper\Metadata\SourcePropertyMetadata;
use AutoMapper\Metadata\TargetPropertyMetadata;
use AutoMapper\Metadata\TypesMatching;
use AutoMapper\Tests\Bundle\Fixtures\User;
use AutoMapper\Tests\Bundle\Fixtures\UserDTO;
use AutoMapper\Tests\Bundle\Resources\App\Entity\User;
use AutoMapper\Tests\Bundle\Resources\App\Entity\UserDTO;
use AutoMapper\Transformer\PropertyTransformer\PropertyTransformerInterface;
use AutoMapper\Transformer\PropertyTransformer\PropertyTransformerSupportInterface;

Expand Down
22 changes: 0 additions & 22 deletions tests/Bundle/Resources/App/config.yml

This file was deleted.

10 changes: 10 additions & 0 deletions tests/Bundle/Resources/config/bundles.php
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['dev' => true],
AutoMapper\Symfony\Bundle\AutoMapperBundle::class => ['all' => true],
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true],
];
7 changes: 7 additions & 0 deletions tests/Bundle/Resources/config/packages/automapper.yaml
@@ -0,0 +1,7 @@
automapper:
normalizer: false
name_converter: AutoMapper\Tests\Bundle\Resources\App\Service\IdNameConverter
map_private_properties: false
check_attributes: false
warmup:
- { source: 'AutoMapper\Tests\Bundle\Resources\App\Entity\NestedObject', target: 'array' }
3 changes: 3 additions & 0 deletions tests/Bundle/Resources/config/packages/dev/web_profiler.yaml
@@ -0,0 +1,3 @@
web_profiler:
toolbar: true
intercept_redirects: false
9 changes: 9 additions & 0 deletions tests/Bundle/Resources/config/packages/framework.yaml
@@ -0,0 +1,9 @@
framework:
secret: automapper
property_info: ~
test: ~
serializer:
enabled: true
profiler:
enabled: "%kernel.debug%"
only_exceptions: false
7 changes: 7 additions & 0 deletions tests/Bundle/Resources/config/routes/dev/profiler.yaml
@@ -0,0 +1,7 @@
web_profiler_wdt:
resource: '@WebProfilerBundle/Resources/config/routing/wdt.xml'
prefix: /_wdt

web_profiler_profiler:
resource: '@WebProfilerBundle/Resources/config/routing/profiler.xml'
prefix: /_profiler
5 changes: 5 additions & 0 deletions tests/Bundle/Resources/config/routes/routes.yaml
@@ -0,0 +1,5 @@
controllers:
resource:
path: ../../App/Controller/
namespace: AutoMapper\Tests\Bundle\Resources\App\Controller
type: attribute
8 changes: 8 additions & 0 deletions tests/Bundle/Resources/config/services.yaml
@@ -0,0 +1,8 @@
services:
_defaults:
autoconfigure: true
autowire: true

AutoMapper\Tests\Bundle\Resources\App\:
resource: '../App/*'
exclude: '../App/{Entity,AppKernel.php}'
16 changes: 16 additions & 0 deletions tests/Bundle/Resources/public/dev.php
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use AutoMapper\Tests\Bundle\Resources\App\AppKernel;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\HttpFoundation\Request;

require __DIR__ . '/../../../../vendor/autoload.php';

Debug::enable();
$kernel = new AppKernel('dev', true);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
14 changes: 14 additions & 0 deletions tests/Bundle/Resources/public/index.php
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use AutoMapper\Tests\Bundle\Resources\App\AppKernel;
use Symfony\Component\HttpFoundation\Request;

require __DIR__ . '/../../../../vendor/autoload.php';

$kernel = new AppKernel('prod', false);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

0 comments on commit 9e749fd

Please sign in to comment.