|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Macpaw\SchemaContextBundle\Tests\Messenger\Transport; |
| 6 | + |
| 7 | +use Macpaw\SchemaContextBundle\DependencyInjection\SchemaContextCompilerPass; |
| 8 | +use Macpaw\SchemaContextBundle\Messenger\Transport\DoctrineTransportFactoryDecorator; |
| 9 | +use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 12 | +use Symfony\Component\DependencyInjection\Definition; |
| 13 | +use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface; |
| 14 | +use Symfony\Component\Messenger\Transport\TransportFactoryInterface; |
| 15 | + |
| 16 | +final class DoctrineTransportFactoryDecoratorTest extends TestCase |
| 17 | +{ |
| 18 | + public function testCompilerPassNotRegisterDecoratorService(): void |
| 19 | + { |
| 20 | + $compilerPass = new SchemaContextCompilerPass(); |
| 21 | + |
| 22 | + $containerBuilder = $this->createMock(ContainerBuilder::class); |
| 23 | + |
| 24 | + $containerBuilder->expects(self::once()) |
| 25 | + ->method('hasDefinition') |
| 26 | + ->willReturn(false); |
| 27 | + |
| 28 | + $containerBuilder->expects(self::never()) |
| 29 | + ->method('setDefinition'); |
| 30 | + |
| 31 | + $compilerPass->process($containerBuilder); |
| 32 | + } |
| 33 | + |
| 34 | + public function testCompilerPassRegisterDecoratorService(): void |
| 35 | + { |
| 36 | + $compilerPass = new SchemaContextCompilerPass(); |
| 37 | + |
| 38 | + $containerBuilder = $this->createMock(ContainerBuilder::class); |
| 39 | + |
| 40 | + $containerBuilder->expects(self::once()) |
| 41 | + ->method('hasDefinition') |
| 42 | + ->willReturn(true); |
| 43 | + |
| 44 | + $containerBuilder->expects(self::once()) |
| 45 | + ->method('setDefinition') |
| 46 | + ->with( |
| 47 | + self::equalTo(SchemaContextCompilerPass::DECORATOR_ID), |
| 48 | + self::callback(function (Definition $definition): bool { |
| 49 | + // You can assert partial properties here |
| 50 | + self::assertSame( |
| 51 | + DoctrineTransportFactoryDecorator::class, |
| 52 | + $definition->getClass(), |
| 53 | + ); |
| 54 | + |
| 55 | + self::assertFalse($definition->isPublic()); |
| 56 | + self::assertTrue($definition->isAutowired()); |
| 57 | + self::assertTrue($definition->isAutoconfigured()); |
| 58 | + self::assertIsArray($definition->getDecoratedService()); |
| 59 | + self::assertArrayHasKey(0, $definition->getDecoratedService()); |
| 60 | + self::assertEquals( |
| 61 | + SchemaContextCompilerPass::TARGET_ID, |
| 62 | + $definition->getDecoratedService()[0], |
| 63 | + ); |
| 64 | + |
| 65 | + // Optional: check arguments only if needed |
| 66 | + $args = $definition->getArguments(); |
| 67 | + self::assertArrayHasKey('$decoratedFactory', $args); |
| 68 | + |
| 69 | + return true; |
| 70 | + }), |
| 71 | + ); |
| 72 | + |
| 73 | + $compilerPass->process($containerBuilder); |
| 74 | + } |
| 75 | + |
| 76 | + public function testSchemaIsOverride(): void |
| 77 | + { |
| 78 | + $doctrineTransportMock = $this->createMock(TransportFactoryInterface::class); |
| 79 | + $baggage = new BaggageSchemaResolver(); |
| 80 | + $baggage->setSchema('test_schema'); |
| 81 | + |
| 82 | + $decorator = new DoctrineTransportFactoryDecorator( |
| 83 | + $doctrineTransportMock, |
| 84 | + $baggage, |
| 85 | + ); |
| 86 | + |
| 87 | + $doctrineTransportMock->expects(self::once()) |
| 88 | + ->method('createTransport') |
| 89 | + ->with( |
| 90 | + self::equalTo(''), |
| 91 | + self::callback(function (array $options): bool { |
| 92 | + self::assertArrayHasKey('table_name', $options); |
| 93 | + self::assertEquals('"test_schema"."messenger_messages"', $options['table_name']); |
| 94 | + |
| 95 | + return true; |
| 96 | + }), |
| 97 | + ); |
| 98 | + |
| 99 | + $decorator->createTransport('', [], $this->createMock(SerializerInterface::class)); |
| 100 | + } |
| 101 | + |
| 102 | + public function testSchemaIsDefault(): void |
| 103 | + { |
| 104 | + $doctrineTransportMock = $this->createMock(TransportFactoryInterface::class); |
| 105 | + $baggage = new BaggageSchemaResolver(); |
| 106 | + $baggage->setSchema('default'); |
| 107 | + |
| 108 | + $decorator = new DoctrineTransportFactoryDecorator( |
| 109 | + $doctrineTransportMock, |
| 110 | + $baggage, |
| 111 | + 'default', |
| 112 | + ); |
| 113 | + |
| 114 | + $doctrineTransportMock->expects(self::once()) |
| 115 | + ->method('createTransport') |
| 116 | + ->with( |
| 117 | + self::equalTo(''), |
| 118 | + self::callback(function (array $options): bool { |
| 119 | + self::assertArrayHasKey('table_name', $options); |
| 120 | + self::assertEquals('messenger_messages', $options['table_name']); |
| 121 | + |
| 122 | + return true; |
| 123 | + }), |
| 124 | + ); |
| 125 | + |
| 126 | + $decorator->createTransport( |
| 127 | + '', |
| 128 | + ['table_name' => 'messenger_messages'], |
| 129 | + $this->createMock(SerializerInterface::class), |
| 130 | + ); |
| 131 | + } |
| 132 | +} |
0 commit comments