44
55namespace Facile \MongoDbBundle \Tests \Unit \Services ;
66
7+ use Facile \MongoDbBundle \Services \UriOptions \UriOptionsInterface ;
78use Prophecy \PhpUnit \ProphecyTrait ;
89use Facile \MongoDbBundle \Event \ConnectionEvent ;
910use Facile \MongoDbBundle \Services \ClientRegistry ;
@@ -96,6 +97,37 @@ public function test_client_connection_url_generation_multihost(): void
9697 $ this ->assertEquals ('mongodb://host1:8080,host2:8081 ' , $ client ->__debugInfo ()['uri ' ]);
9798 }
9899
100+ public function test_client_connection_url_generation_with_custom_uri_options (): void
101+ {
102+ $ customUriOptions = ['appname ' => 'APPNAME ' ];
103+ $ uriOptionsService = $ this ->getUriOptionsService ($ customUriOptions );
104+ $ registry = new ClientRegistry ($ this ->createEventDispatcherMock (), false , $ uriOptionsService , null );
105+
106+ $ testConf = [
107+ 'test_client ' => [
108+ 'hosts ' => [],
109+ 'uri ' => 'mongodb://user:password@host1:27017 ' ,
110+ 'username ' => '' ,
111+ 'password ' => '' ,
112+ 'authSource ' => null ,
113+ 'replicaSet ' => 'testReplica ' ,
114+ 'ssl ' => true ,
115+ 'connectTimeoutMS ' => 3_000 ,
116+ 'readPreference ' => 'primary ' ,
117+ ],
118+ ];
119+
120+ $ registry ->addClientsConfigurations ($ testConf );
121+ $ client = $ registry ->getClient ('test_client ' , 'testdb ' );
122+
123+ $ this ->assertEquals ('mongodb://user:password@host1:27017 ' , $ client ->__debugInfo ()['uri ' ]);
124+ $ this ->assertEquals (['test_client.testdb ' ], $ registry ->getClientNames ());
125+ self ::assertArrayHasKey ('test_client ' , $ registry ->getConfigurations ());
126+ self ::assertObjectHasProperty ('options ' , $ registry ->getConfigurations ()['test_client ' ]);
127+ self ::assertArrayHasKey ('appname ' , $ registry ->getConfigurations ()['test_client ' ]->getOptions ());
128+ $ this ->assertEquals ('APPNAME ' , $ registry ->getConfigurations ()['test_client ' ]->getOptions ()['appname ' ]);
129+ }
130+
99131 private function createEventDispatcherMock (): EventDispatcherInterface
100132 {
101133 $ eventDispatcher = $ this ->prophesize (EventDispatcherInterface::class);
@@ -113,4 +145,21 @@ private function createEventDispatcherMock(): EventDispatcherInterface
113145
114146 return $ eventDispatcher ->reveal ();
115147 }
148+
149+ private function getUriOptionsService ($ customUriOptions ): UriOptionsInterface
150+ {
151+ return new class ($ customUriOptions ) implements UriOptionsInterface {
152+ private array $ customUriOptions ;
153+
154+ public function __construct ($ customUriOptions )
155+ {
156+ $ this ->customUriOptions = $ customUriOptions ;
157+ }
158+
159+ public function buildUriOptions (array $ clientConfiguration ): array
160+ {
161+ return array_merge ($ clientConfiguration , $ this ->customUriOptions );
162+ }
163+ };
164+ }
116165}
0 commit comments