|
16 | 16 | use Monolog\Handler\ElasticaHandler; |
17 | 17 | use Monolog\Handler\ElasticsearchHandler; |
18 | 18 | use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy; |
| 19 | +use Monolog\Handler\MongoDBHandler; |
19 | 20 | use Monolog\Handler\RollbarHandler; |
20 | 21 | use Monolog\Logger; |
21 | 22 | use Monolog\Processor\UidProcessor; |
@@ -932,6 +933,148 @@ public function testElasticsearchAndElasticaHandlers() |
932 | 933 | $this->assertSame(['hosts' => ['es:9200'], 'transport' => 'Http'], $elasticaClient->getArgument(0)); |
933 | 934 | } |
934 | 935 |
|
| 936 | + /** @group legacy */ |
| 937 | + public function testMongo() |
| 938 | + { |
| 939 | + $this->expectDeprecation('Since symfony/monolog-bundle 3.11: The "mongo" handler type is deprecated in MonologBundle since version 3.11.0, use the "mongodb" type instead.'); |
| 940 | + |
| 941 | + $container = new ContainerBuilder(); |
| 942 | + $container->setDefinition('mongodb.client', new Definition('MongoDB\Client')); |
| 943 | + |
| 944 | + $config = [[ |
| 945 | + 'handlers' => [ |
| 946 | + 'mongo_with_id' => [ |
| 947 | + 'type' => 'mongo', |
| 948 | + 'mongo' => ['id' => 'mongodb.client'], |
| 949 | + ], |
| 950 | + 'mongo_with_string_id' => [ |
| 951 | + 'type' => 'mongo', |
| 952 | + 'mongo' => 'mongodb.client', |
| 953 | + ], |
| 954 | + 'mongo_with_host' => [ |
| 955 | + 'type' => 'mongo', |
| 956 | + 'mongo' => [ |
| 957 | + 'host' => 'localhost', |
| 958 | + 'port' => '27018', |
| 959 | + 'user' => 'username', |
| 960 | + 'pass' => 'password', |
| 961 | + 'database' => 'db', |
| 962 | + 'collection' => 'coll', |
| 963 | + ], |
| 964 | + ], |
| 965 | + 'mongo_with_host_and_default_args' => [ |
| 966 | + 'type' => 'mongo', |
| 967 | + 'mongo' => [ |
| 968 | + 'host' => 'localhost', |
| 969 | + ], |
| 970 | + ], |
| 971 | + ], |
| 972 | + ]]; |
| 973 | + |
| 974 | + $extension = new MonologExtension(); |
| 975 | + $extension->load($config, $container); |
| 976 | + |
| 977 | + $this->assertTrue($container->hasDefinition('monolog.handler.mongo_with_id')); |
| 978 | + $this->assertTrue($container->hasDefinition('monolog.handler.mongo_with_string_id')); |
| 979 | + $this->assertTrue($container->hasDefinition('monolog.handler.mongo_with_host')); |
| 980 | + $this->assertTrue($container->hasDefinition('monolog.handler.mongo_with_host_and_default_args')); |
| 981 | + |
| 982 | + // MongoDB handler should receive the mongodb.client as first argument |
| 983 | + $handler = $container->getDefinition('monolog.handler.mongo_with_id'); |
| 984 | + $this->assertDICDefinitionClass($handler, MongoDBHandler::class); |
| 985 | + $this->assertDICConstructorArguments($handler, [new Reference('mongodb.client'), 'monolog', 'logs', 'DEBUG', true]); |
| 986 | + |
| 987 | + // MongoDB handler should receive the mongodb.client as first argument |
| 988 | + $handler = $container->getDefinition('monolog.handler.mongo_with_string_id'); |
| 989 | + $this->assertDICDefinitionClass($handler, MongoDBHandler::class); |
| 990 | + $this->assertDICConstructorArguments($handler, [new Reference('mongodb.client'), 'monolog', 'logs', 'DEBUG', true]); |
| 991 | + |
| 992 | + // MongoDB handler with host and arguments |
| 993 | + $handler = $container->getDefinition('monolog.handler.mongo_with_host'); |
| 994 | + $this->assertDICDefinitionClass($handler, MongoDBHandler::class); |
| 995 | + $client = $handler->getArgument(0); |
| 996 | + $this->assertDICDefinitionClass($client, 'MongoDB\Client'); |
| 997 | + $this->assertDICConstructorArguments($client, ['mongodb://username:password@localhost:27018', ['appname' => 'monolog-bundle']]); |
| 998 | + $this->assertDICConstructorArguments($handler, [$client, 'db', 'coll', 'DEBUG', true]); |
| 999 | + |
| 1000 | + // MongoDB handler with host and default arguments |
| 1001 | + $handler = $container->getDefinition('monolog.handler.mongo_with_host_and_default_args'); |
| 1002 | + $this->assertDICDefinitionClass($handler, MongoDBHandler::class); |
| 1003 | + $client = $handler->getArgument(0); |
| 1004 | + $this->assertDICDefinitionClass($client, 'MongoDB\Client'); |
| 1005 | + $this->assertDICConstructorArguments($client, ['mongodb://localhost:27017', ['appname' => 'monolog-bundle']]); |
| 1006 | + $this->assertDICConstructorArguments($handler, [$client, 'monolog', 'logs', 'DEBUG', true]); |
| 1007 | + } |
| 1008 | + |
| 1009 | + public function testMongoDB() |
| 1010 | + { |
| 1011 | + $container = new ContainerBuilder(); |
| 1012 | + $container->setDefinition('mongodb.client', new Definition('MongoDB\Client')); |
| 1013 | + |
| 1014 | + $config = [[ |
| 1015 | + 'handlers' => [ |
| 1016 | + 'mongodb_with_id' => [ |
| 1017 | + 'type' => 'mongodb', |
| 1018 | + 'mongodb' => ['id' => 'mongodb.client'], |
| 1019 | + ], |
| 1020 | + 'mongodb_with_string_id' => [ |
| 1021 | + 'type' => 'mongodb', |
| 1022 | + 'mongodb' => 'mongodb.client', |
| 1023 | + ], |
| 1024 | + 'mongodb_with_uri' => [ |
| 1025 | + 'type' => 'mongodb', |
| 1026 | + 'mongodb' => [ |
| 1027 | + 'uri' => 'mongodb://localhost:27018', |
| 1028 | + 'username' => 'username', |
| 1029 | + 'password' => 'password', |
| 1030 | + 'database' => 'db', |
| 1031 | + 'collection' => 'coll', |
| 1032 | + ], |
| 1033 | + ], |
| 1034 | + 'mongodb_with_uri_and_default_args' => [ |
| 1035 | + 'type' => 'mongodb', |
| 1036 | + 'mongodb' => [ |
| 1037 | + 'uri' => 'mongodb://localhost:27018', |
| 1038 | + ], |
| 1039 | + ], |
| 1040 | + ], |
| 1041 | + ]]; |
| 1042 | + |
| 1043 | + $extension = new MonologExtension(); |
| 1044 | + $extension->load($config, $container); |
| 1045 | + |
| 1046 | + $this->assertTrue($container->hasDefinition('monolog.handler.mongodb_with_id')); |
| 1047 | + $this->assertTrue($container->hasDefinition('monolog.handler.mongodb_with_string_id')); |
| 1048 | + $this->assertTrue($container->hasDefinition('monolog.handler.mongodb_with_uri')); |
| 1049 | + $this->assertTrue($container->hasDefinition('monolog.handler.mongodb_with_uri_and_default_args')); |
| 1050 | + |
| 1051 | + // MongoDB handler should receive the mongodb.client as first argument |
| 1052 | + $handler = $container->getDefinition('monolog.handler.mongodb_with_id'); |
| 1053 | + $this->assertDICDefinitionClass($handler, MongoDBHandler::class); |
| 1054 | + $this->assertDICConstructorArguments($handler, [new Reference('mongodb.client'), 'monolog', 'logs', 'DEBUG', true]); |
| 1055 | + |
| 1056 | + // MongoDB handler should receive the mongodb.client as first argument |
| 1057 | + $handler = $container->getDefinition('monolog.handler.mongodb_with_string_id'); |
| 1058 | + $this->assertDICDefinitionClass($handler, MongoDBHandler::class); |
| 1059 | + $this->assertDICConstructorArguments($handler, [new Reference('mongodb.client'), 'monolog', 'logs', 'DEBUG', true]); |
| 1060 | + |
| 1061 | + // MongoDB handler with arguments |
| 1062 | + $handler = $container->getDefinition('monolog.handler.mongodb_with_uri'); |
| 1063 | + $this->assertDICDefinitionClass($handler, MongoDBHandler::class); |
| 1064 | + $client = $handler->getArgument(0); |
| 1065 | + $this->assertDICDefinitionClass($client, 'MongoDB\Client'); |
| 1066 | + $this->assertDICConstructorArguments($client, ['mongodb://localhost:27018', ['appname' => 'monolog-bundle', 'username' => 'username', 'password' => 'password']]); |
| 1067 | + $this->assertDICConstructorArguments($handler, [$client, 'db', 'coll', 'DEBUG', true]); |
| 1068 | + |
| 1069 | + // MongoDB handler with host and default arguments |
| 1070 | + $handler = $container->getDefinition('monolog.handler.mongodb_with_uri_and_default_args'); |
| 1071 | + $this->assertDICDefinitionClass($handler, MongoDBHandler::class); |
| 1072 | + $client = $handler->getArgument(0); |
| 1073 | + $this->assertDICDefinitionClass($client, 'MongoDB\Client'); |
| 1074 | + $this->assertDICConstructorArguments($client, ['mongodb://localhost:27018', ['appname' => 'monolog-bundle']]); |
| 1075 | + $this->assertDICConstructorArguments($handler, [$client, 'monolog', 'logs', 'DEBUG', true]); |
| 1076 | + } |
| 1077 | + |
935 | 1078 | protected function getContainer(array $config = [], array $thirdPartyDefinitions = []): ContainerBuilder |
936 | 1079 | { |
937 | 1080 | $container = new ContainerBuilder(new EnvPlaceholderParameterBag()); |
|
0 commit comments