|
5 | 5 | namespace Consistence\JmsSerializer\SymfonyBundle\DependencyInjection; |
6 | 6 |
|
7 | 7 | use Consistence\JmsSerializer\Enum\EnumSerializerHandler; |
| 8 | +use PHPUnit\Framework\Assert; |
| 9 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 10 | +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
8 | 11 |
|
9 | | -class ConsistenceJmsSerializerExtensionTest extends \Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase |
| 12 | +class ConsistenceJmsSerializerExtensionTest extends \PHPUnit\Framework\TestCase |
10 | 13 | { |
11 | 14 |
|
12 | | - /** |
13 | | - * @return \Symfony\Component\DependencyInjection\Extension\ExtensionInterface[] |
14 | | - */ |
15 | | - protected function getContainerExtensions(): array |
| 15 | + public function testRegisterSerializerHandler(): void |
16 | 16 | { |
17 | | - return [ |
18 | | - new ConsistenceJmsSerializerExtension(), |
19 | | - ]; |
| 17 | + $container = self::createContainer(); |
| 18 | + $container->registerExtension(new ConsistenceJmsSerializerExtension()); |
| 19 | + |
| 20 | + foreach ($container->getExtensions() as $extension) { |
| 21 | + $extension->load([], $container); |
| 22 | + } |
| 23 | + |
| 24 | + $serviceId = 'consistence.jms_serializer.enum.enum_serializer_handler'; |
| 25 | + self::assertContainerHasService($container, $serviceId); |
| 26 | + self::assertContainerServiceIsOfType($container, $serviceId, EnumSerializerHandler::class); |
| 27 | + self::assertContainerServiceHasTagWithAttributes( |
| 28 | + $container, |
| 29 | + $serviceId, |
| 30 | + 'jms_serializer.subscribing_handler', |
| 31 | + [] |
| 32 | + ); |
| 33 | + |
| 34 | + $container->compile(); |
20 | 35 | } |
21 | 36 |
|
22 | | - public function testRegisterSerializerHandler(): void |
| 37 | + private static function createContainer(): ContainerBuilder |
23 | 38 | { |
24 | | - $this->load(); |
| 39 | + $container = new ContainerBuilder(new ParameterBag([])); |
| 40 | + $container->getCompilerPassConfig()->setOptimizationPasses([]); |
| 41 | + $container->getCompilerPassConfig()->setRemovingPasses([]); |
| 42 | + $container->getCompilerPassConfig()->setAfterRemovingPasses([]); |
| 43 | + |
| 44 | + return $container; |
| 45 | + } |
25 | 46 |
|
26 | | - $this->assertContainerBuilderHasService( |
27 | | - 'consistence.jms_serializer.enum.enum_serializer_handler', |
28 | | - EnumSerializerHandler::class |
| 47 | + private static function assertContainerHasService(ContainerBuilder $container, string $serviceId): void |
| 48 | + { |
| 49 | + Assert::assertTrue( |
| 50 | + $container->has($serviceId), |
| 51 | + sprintf('Container is missing required service `%s`.', $serviceId) |
29 | 52 | ); |
30 | | - $this->assertContainerBuilderHasServiceDefinitionWithTag( |
31 | | - 'consistence.jms_serializer.enum.enum_serializer_handler', |
32 | | - 'jms_serializer.subscribing_handler' |
| 53 | + } |
| 54 | + |
| 55 | + private static function assertContainerServiceIsOfType( |
| 56 | + ContainerBuilder $container, |
| 57 | + string $serviceId, |
| 58 | + string $expectedClassString |
| 59 | + ): void |
| 60 | + { |
| 61 | + $serviceDefinition = $container->findDefinition($serviceId); |
| 62 | + |
| 63 | + Assert::assertSame( |
| 64 | + $expectedClassString, |
| 65 | + $container->getParameterBag()->resolveValue($serviceDefinition->getClass()) |
33 | 66 | ); |
| 67 | + } |
| 68 | + |
| 69 | + private static function assertContainerServiceHasTagWithAttributes( |
| 70 | + ContainerBuilder $container, |
| 71 | + string $serviceId, |
| 72 | + string $tagName |
| 73 | + ): void |
| 74 | + { |
| 75 | + $tagExists = false; |
| 76 | + $serviceDefinition = $container->findDefinition($serviceId); |
| 77 | + |
| 78 | + foreach ($serviceDefinition->getTags() as $name => $tagsAttributes) { |
| 79 | + if ($name !== $tagName) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + $tagExists = true; |
| 84 | + } |
34 | 85 |
|
35 | | - $this->compile(); |
| 86 | + if (!$tagExists) { |
| 87 | + Assert::fail(sprintf('Service `%s` does not have any tag `%s`.', $serviceId, $tagName)); |
| 88 | + } |
36 | 89 | } |
37 | 90 |
|
38 | 91 | } |
0 commit comments