|
13 | 13 | use Doctrine\Common\Cache\Psr6\DoctrineProvider;
|
14 | 14 | use Doctrine\DBAL\Connection;
|
15 | 15 | use Doctrine\DBAL\Driver\Connection as DriverConnection;
|
| 16 | +use Doctrine\DBAL\Driver\Middleware; |
16 | 17 | use Doctrine\DBAL\Sharding\PoolingShardManager;
|
17 | 18 | use Doctrine\DBAL\Sharding\SQLAzure\SQLAzureShardManager;
|
18 | 19 | use Doctrine\ORM\EntityManagerInterface;
|
|
24 | 25 | use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerWorkerSubscriber;
|
25 | 26 | use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
26 | 27 | use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
|
| 28 | +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
27 | 29 | use Symfony\Component\DependencyInjection\ChildDefinition;
|
28 | 30 | use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
|
29 | 31 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
33 | 35 | use Symfony\Component\HttpKernel\Kernel;
|
34 | 36 | use Symfony\Component\Messenger\MessageBusInterface;
|
35 | 37 |
|
| 38 | +use function array_filter; |
36 | 39 | use function array_values;
|
37 | 40 | use function class_exists;
|
38 | 41 | use function interface_exists;
|
@@ -1143,6 +1146,118 @@ public function testAsEntityListenerAttribute()
|
1143 | 1146 | $this->assertSame([$expected], $definition->getTag('doctrine.orm.entity_listener'));
|
1144 | 1147 | }
|
1145 | 1148 |
|
| 1149 | + public function testUseMiddlewaresNotActivated(): void |
| 1150 | + { |
| 1151 | + $container = $this->getContainer(); |
| 1152 | + $extension = new DoctrineExtension(); |
| 1153 | + |
| 1154 | + $config = BundleConfigurationBuilder::createBuilderWithBaseValues() |
| 1155 | + ->addConnection([ |
| 1156 | + 'connections' => [ |
| 1157 | + 'default' => [ |
| 1158 | + 'password' => 'foo', |
| 1159 | + 'logging' => true, |
| 1160 | + ], |
| 1161 | + ], |
| 1162 | + ]) |
| 1163 | + ->addBaseEntityManager() |
| 1164 | + ->build(); |
| 1165 | + |
| 1166 | + $extension->load([$config], $container); |
| 1167 | + |
| 1168 | + $loggerDef = $container->getDefinition('doctrine.dbal.logger'); |
| 1169 | + $tags = $loggerDef->getTag('monolog.logger'); |
| 1170 | + $doctrineLoggerTags = array_filter($tags, static function (array $tag): bool { |
| 1171 | + return ($tag['channel'] ?? null) === 'doctrine'; |
| 1172 | + }); |
| 1173 | + $this->assertCount(1, $doctrineLoggerTags); |
| 1174 | + |
| 1175 | + $this->assertInstanceOf(Reference::class, $loggerDef->getArgument(0)); |
| 1176 | + $this->assertSame('logger', (string) $loggerDef->getArgument(0)); |
| 1177 | + |
| 1178 | + $this->assertFalse($container->hasDefinition('doctrine.dbal.default_connection.logging_middleware')); |
| 1179 | + } |
| 1180 | + |
| 1181 | + public function testUseMiddlewaresActivated(): void |
| 1182 | + { |
| 1183 | + /** @psalm-suppress UndefinedClass */ |
| 1184 | + if (! interface_exists(Middleware::class)) { |
| 1185 | + $this->markTestSkipped(sprintf('%s needs %s to exist', __METHOD__, Middleware::class)); |
| 1186 | + } |
| 1187 | + |
| 1188 | + $container = $this->getContainer(); |
| 1189 | + $extension = new DoctrineExtension(); |
| 1190 | + |
| 1191 | + $config = BundleConfigurationBuilder::createBuilderWithBaseValues() |
| 1192 | + ->addConnection([ |
| 1193 | + 'connections' => [ |
| 1194 | + 'default' => [ |
| 1195 | + 'password' => 'foo', |
| 1196 | + 'logging' => true, |
| 1197 | + 'use_middlewares' => true, |
| 1198 | + ], |
| 1199 | + ], |
| 1200 | + ]) |
| 1201 | + ->addBaseEntityManager() |
| 1202 | + ->build(); |
| 1203 | + |
| 1204 | + $extension->load([$config], $container); |
| 1205 | + |
| 1206 | + $loggerDef = $container->getDefinition('doctrine.dbal.logger'); |
| 1207 | + $this->assertNull($loggerDef->getArgument(0)); |
| 1208 | + |
| 1209 | + $loggingMiddlewareDef = $container->getDefinition('doctrine.dbal.default_connection.logging_middleware'); |
| 1210 | + $tags = $loggingMiddlewareDef->getTag('monolog.logger'); |
| 1211 | + $doctrineLoggerTags = array_filter($tags, static function (array $tag): bool { |
| 1212 | + return ($tag['channel'] ?? null) === 'doctrine'; |
| 1213 | + }); |
| 1214 | + $this->assertCount(1, $doctrineLoggerTags); |
| 1215 | + |
| 1216 | + $this->assertInstanceOf(ChildDefinition::class, $loggingMiddlewareDef); |
| 1217 | + $parentDef = $container->getDefinition($loggingMiddlewareDef->getParent()); |
| 1218 | + $this->assertInstanceOf(Reference::class, $parentDef->getArgument(0)); |
| 1219 | + $this->assertSame('logger', (string) $parentDef->getArgument(0)); |
| 1220 | + |
| 1221 | + $connectionConfiguration = $container->getDefinition('doctrine.dbal.default_connection.configuration'); |
| 1222 | + $setMiddlewareCalls = array_filter($connectionConfiguration->getMethodCalls(), static function (array $call) { |
| 1223 | + return $call[0] === 'setMiddlewares'; |
| 1224 | + }); |
| 1225 | + $this->assertCount(1, $setMiddlewareCalls); |
| 1226 | + $callArgs = $setMiddlewareCalls[0][1]; |
| 1227 | + $this->assertCount(1, $callArgs); |
| 1228 | + $this->assertCount(1, $callArgs[0]); |
| 1229 | + $this->assertInstanceOf(Definition::class, $callArgs[0][0]); |
| 1230 | + } |
| 1231 | + |
| 1232 | + public function testUseMiddlewaresWithoutMiddlewareInterface(): void |
| 1233 | + { |
| 1234 | + /** @psalm-suppress UndefinedClass */ |
| 1235 | + if (interface_exists(Middleware::class)) { |
| 1236 | + $this->markTestSkipped(sprintf('%s needs %s to not exist', __METHOD__, Middleware::class)); |
| 1237 | + } |
| 1238 | + |
| 1239 | + $this->expectException(InvalidConfigurationException::class); |
| 1240 | + $this->expectExceptionMessage(sprintf('%s must exist to set use_middlewares to true', Middleware::class)); |
| 1241 | + |
| 1242 | + $container = $this->getContainer(); |
| 1243 | + $extension = new DoctrineExtension(); |
| 1244 | + |
| 1245 | + $config = BundleConfigurationBuilder::createBuilderWithBaseValues() |
| 1246 | + ->addConnection([ |
| 1247 | + 'connections' => [ |
| 1248 | + 'default' => [ |
| 1249 | + 'password' => 'foo', |
| 1250 | + 'logging' => true, |
| 1251 | + 'use_middlewares' => true, |
| 1252 | + ], |
| 1253 | + ], |
| 1254 | + ]) |
| 1255 | + ->addBaseEntityManager() |
| 1256 | + ->build(); |
| 1257 | + |
| 1258 | + $extension->load([$config], $container); |
| 1259 | + } |
| 1260 | + |
1146 | 1261 | // phpcs:enable
|
1147 | 1262 |
|
1148 | 1263 | /** @param list<string> $bundles */
|
|
0 commit comments