Skip to content

Commit 3d9ef15

Browse files
author
Daniele Scibilia
committed
up
1 parent c400834 commit 3d9ef15

File tree

5 files changed

+75
-27
lines changed

5 files changed

+75
-27
lines changed

docs/Documentation.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,23 @@ namespace App\Services;
109109
110110
use Facile\MongoDbBundle\Services\UriOptions\UriOptionsInterface;
111111
112-
final class MyCustomUriOptionsProvider implements DriverOptionsInterface
112+
final class MyCustomUriOptionsProvider implements UriOptionsInterface
113113
{
114-
/** @var string */
115-
private $appname;
116-
117-
public function __construct(string $appname) {
118-
$this->appname = $appname;
119-
}
114+
private const APPNAME = 'APPNAME';
120115
121-
public function buildDriverOptions(array $clientConfiguration) : array {
122-
$clientConfiguration['appname'] = $this->appname;
123-
return $clientConfiguration;
116+
public function buildUriOptions(array $clientConfiguration): array
117+
{
118+
return array_merge(
119+
$clientConfiguration,
120+
['appname' => self::APPNAME]
121+
);
124122
}
125123
}
126124
```
127125

128126
```yaml
129127
# config/services.yaml
130128
App\Services\MyCustomUriOptionsProvider:
131-
arguments:
132-
$appname: 'APPNAME'
133129
```
134130

135131
Then use its service id as value of `uriOptions` in the bundle configuration.

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ parameters:
22
ignoreErrors:
33
-
44
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition\\:\\:children\\(\\)\\.$#"
5-
count: 7
5+
count: 8
66
path: src/DependencyInjection/Configuration.php
77

88
-

src/DependencyInjection/Configuration.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ private function addClientsHosts(NodeBuilder $builder): void
107107
->defaultValue(27_017);
108108
}
109109

110+
private function addUriOptions(NodeBuilder $builder): void
111+
{
112+
$builder
113+
->scalarNode('uriOptions');
114+
}
115+
110116
private function addDriversOption(NodeBuilder $builder): void
111117
{
112118
$builder
@@ -133,17 +139,4 @@ private function addConnections(NodeBuilder $builder): void
133139
->isRequired()
134140
->info('Database name');
135141
}
136-
137-
private function addUriOptions(NodeBuilder $builder): void
138-
{
139-
$uriOptionsBuilder = $builder
140-
->arrayNode('uriOptions')
141-
->info('Additional connection string options')
142-
->children();
143-
144-
$uriOptionsBuilder
145-
->variableNode('context')
146-
->defaultValue([])
147-
->info('Overwrite any options with the same name in the uri parameter');
148-
}
149142
}

src/Services/ClientRegistry.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private function buildClientConfiguration(array $conf): ClientConfiguration
6565
'readPreference' => $conf['readPreference'],
6666
];
6767
if ($this->uriOptionsService instanceof UriOptionsInterface) {
68-
$conf['options'] = $this->uriOptionsService->buildUriOptions($conf['uriOptions']);
68+
$conf['uriOptions'] = $this->uriOptionsService->buildUriOptions($conf['uriOptions']);
6969
}
7070

7171
$conf['driverOptions'] = [];
@@ -132,4 +132,14 @@ private function buildClient(string $clientName, string $uri, array $options, ar
132132

133133
return new Client($uri, $options, $driverOptions);
134134
}
135+
136+
/**
137+
* @return ClientConfiguration[]
138+
*/
139+
public function getConfigurations(): array
140+
{
141+
return $this->configurations;
142+
}
143+
144+
135145
}

tests/Unit/Services/ClientRegistryTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Facile\MongoDbBundle\Tests\Unit\Services;
66

7+
use Facile\MongoDbBundle\Services\UriOptions\UriOptionsInterface;
78
use Prophecy\PhpUnit\ProphecyTrait;
89
use Facile\MongoDbBundle\Event\ConnectionEvent;
910
use 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

Comments
 (0)