Skip to content

Commit 08e3187

Browse files
committed
Add tests for autowiring
1 parent b4b22b5 commit 08e3187

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Tests\Torr\SimpleNormalizer\DependencyInjection;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
7+
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Tests\Torr\SimpleNormalizer\Fixture\DummyVO;
10+
use Torr\SimpleNormalizer\Normalizer\SimpleNormalizer;
11+
use Torr\SimpleNormalizer\Normalizer\SimpleObjectNormalizerInterface;
12+
use Torr\SimpleNormalizer\SimpleNormalizerBundle;
13+
14+
/**
15+
* @internal
16+
*/
17+
final class BundleWiringTest extends TestCase
18+
{
19+
/**
20+
*/
21+
public function testBundleWiresAutoconfiguredObjectNormalizers () : void
22+
{
23+
$container = new ContainerBuilder();
24+
25+
$bundle = new SimpleNormalizerBundle();
26+
$bundle->build($container);
27+
28+
$container
29+
->register(BundleWiringDummyNormalizer::class, BundleWiringDummyNormalizer::class)
30+
->setAutoconfigured(true)
31+
->setAutowired(true);
32+
33+
$container
34+
->register(SimpleNormalizer::class, SimpleNormalizer::class)
35+
->setPublic(true)
36+
->setArguments([
37+
new ServiceLocatorArgument(
38+
new TaggedIteratorArgument(
39+
"torr.normalizer.simple-object-normalizer",
40+
defaultIndexMethod: "getNormalizedType",
41+
needsIndexes: true,
42+
),
43+
),
44+
false,
45+
null,
46+
null,
47+
]);
48+
49+
$container->compile();
50+
51+
$normalizer = $container->get(SimpleNormalizer::class);
52+
\assert($normalizer instanceof SimpleNormalizer);
53+
54+
self::assertSame(
55+
["id" => 42],
56+
$normalizer->normalize(new DummyVO(42)),
57+
);
58+
}
59+
}
60+
61+
final class BundleWiringDummyNormalizer implements SimpleObjectNormalizerInterface
62+
{
63+
/**
64+
* @inheritDoc
65+
*/
66+
public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : mixed
67+
{
68+
\assert($value instanceof DummyVO);
69+
70+
return [
71+
"id" => $value->id,
72+
];
73+
}
74+
75+
/**
76+
* @inheritDoc
77+
*/
78+
public static function getNormalizedType () : string
79+
{
80+
return DummyVO::class;
81+
}
82+
}

0 commit comments

Comments
 (0)