Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94db7d4

Browse files
committed
bug #32392 [Messenger] Doctrine Transport: Support setting auto_setup from DSN (bendavies)
This PR was squashed before being merged into the 4.3 branch (closes #32392). Discussion ---------- [Messenger] Doctrine Transport: Support setting auto_setup from DSN | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | <!-- required for new features --> It was not possible to set `auto_setup` via the dsn, as the result would always be a string, resulting in setup always being performed because a bool is required. The same was true if `auto_setup` was provided in `options` as a string. I've fixed ensuring that the final configuration contains a bool for `auto_setup`. Additionally, constructing the `configuration` was overly complex and hard to grok, so I've hopefully simplified it as part of this PR. As an aside the three transports all do configuration construction in different ways with varying styles. It would be nice to neaten them up. Commits ------- 213dfd1 [Messenger] Doctrine Transport: Support setting auto_setup from DSN
2 parents 4ef1f2f + 213dfd1 commit 94db7d4

File tree

2 files changed

+76
-53
lines changed

2 files changed

+76
-53
lines changed

src/Symfony/Component/Messenger/Tests/Transport/Doctrine/ConnectionTest.php

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -157,62 +157,88 @@ private function getSchemaSynchronizerMock()
157157
/**
158158
* @dataProvider buildConfigurationProvider
159159
*/
160-
public function testBuildConfiguration($dsn, $options, $expectedManager, $expectedTableName, $expectedRedeliverTimeout, $expectedQueue)
160+
public function testBuildConfiguration($dsn, $options, $expectedConnection, $expectedTableName, $expectedRedeliverTimeout, $expectedQueue, $expectedAutoSetup)
161161
{
162162
$config = Connection::buildConfiguration($dsn, $options);
163-
$this->assertEquals($expectedManager, $config['connection']);
163+
$this->assertEquals($expectedConnection, $config['connection']);
164164
$this->assertEquals($expectedTableName, $config['table_name']);
165165
$this->assertEquals($expectedRedeliverTimeout, $config['redeliver_timeout']);
166166
$this->assertEquals($expectedQueue, $config['queue_name']);
167+
$this->assertEquals($expectedAutoSetup, $config['auto_setup']);
167168
}
168169

169170
public function buildConfigurationProvider()
170171
{
171-
return [
172-
[
173-
'dsn' => 'doctrine://default',
174-
'options' => [],
175-
'expectedManager' => 'default',
176-
'expectedTableName' => 'messenger_messages',
177-
'expectedRedeliverTimeout' => 3600,
178-
'expectedQueue' => 'default',
179-
],
180-
// test options from options array
181-
[
182-
'dsn' => 'doctrine://default',
183-
'options' => [
184-
'table_name' => 'name_from_options',
185-
'redeliver_timeout' => 1800,
186-
'queue_name' => 'important',
187-
],
188-
'expectedManager' => 'default',
189-
'expectedTableName' => 'name_from_options',
190-
'expectedRedeliverTimeout' => 1800,
191-
'expectedQueue' => 'important',
192-
],
193-
// tests options from dsn
194-
[
195-
'dsn' => 'doctrine://default?table_name=name_from_dsn&redeliver_timeout=1200&queue_name=normal',
196-
'options' => [],
197-
'expectedManager' => 'default',
198-
'expectedTableName' => 'name_from_dsn',
199-
'expectedRedeliverTimeout' => 1200,
200-
'expectedQueue' => 'normal',
172+
yield 'no options' => [
173+
'dsn' => 'doctrine://default',
174+
'options' => [],
175+
'expectedConnection' => 'default',
176+
'expectedTableName' => 'messenger_messages',
177+
'expectedRedeliverTimeout' => 3600,
178+
'expectedQueue' => 'default',
179+
'expectedAutoSetup' => true,
180+
];
181+
182+
yield 'test options array' => [
183+
'dsn' => 'doctrine://default',
184+
'options' => [
185+
'table_name' => 'name_from_options',
186+
'redeliver_timeout' => 1800,
187+
'queue_name' => 'important',
188+
'auto_setup' => false,
201189
],
202-
// test options from options array wins over options from dsn
203-
[
204-
'dsn' => 'doctrine://default?table_name=name_from_dsn&redeliver_timeout=1200&queue_name=normal',
205-
'options' => [
206-
'table_name' => 'name_from_options',
207-
'redeliver_timeout' => 1800,
208-
'queue_name' => 'important',
209-
],
210-
'expectedManager' => 'default',
211-
'expectedTableName' => 'name_from_options',
212-
'expectedRedeliverTimeout' => 1800,
213-
'expectedQueue' => 'important',
190+
'expectedConnection' => 'default',
191+
'expectedTableName' => 'name_from_options',
192+
'expectedRedeliverTimeout' => 1800,
193+
'expectedQueue' => 'important',
194+
'expectedAutoSetup' => false,
195+
];
196+
197+
yield 'options from dsn' => [
198+
'dsn' => 'doctrine://default?table_name=name_from_dsn&redeliver_timeout=1200&queue_name=normal&auto_setup=false',
199+
'options' => [],
200+
'expectedConnection' => 'default',
201+
'expectedTableName' => 'name_from_dsn',
202+
'expectedRedeliverTimeout' => 1200,
203+
'expectedQueue' => 'normal',
204+
'expectedAutoSetup' => false,
205+
];
206+
207+
yield 'options from options array wins over options from dsn' => [
208+
'dsn' => 'doctrine://default?table_name=name_from_dsn&redeliver_timeout=1200&queue_name=normal&auto_setup=true',
209+
'options' => [
210+
'table_name' => 'name_from_options',
211+
'redeliver_timeout' => 1800,
212+
'queue_name' => 'important',
213+
'auto_setup' => false,
214214
],
215+
'expectedConnection' => 'default',
216+
'expectedTableName' => 'name_from_options',
217+
'expectedRedeliverTimeout' => 1800,
218+
'expectedQueue' => 'important',
219+
'expectedAutoSetup' => false,
220+
];
221+
222+
yield 'options from dsn with falsey boolean' => [
223+
'dsn' => 'doctrine://default?auto_setup=0',
224+
'options' => [],
225+
'expectedConnection' => 'default',
226+
'expectedTableName' => 'messenger_messages',
227+
'expectedRedeliverTimeout' => 3600,
228+
'expectedQueue' => 'default',
229+
'expectedAutoSetup' => false,
215230
];
231+
232+
yield 'options from dsn with thruthy boolean' => [
233+
'dsn' => 'doctrine://default?auto_setup=1',
234+
'options' => [],
235+
'expectedConnection' => 'default',
236+
'expectedTableName' => 'messenger_messages',
237+
'expectedRedeliverTimeout' => 3600,
238+
'expectedQueue' => 'default',
239+
'expectedAutoSetup' => true,
240+
];
241+
216242
}
217243

218244
/**

src/Symfony/Component/Messenger/Transport/Doctrine/Connection.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,19 @@ public static function buildConfiguration($dsn, array $options = [])
7676
parse_str($components['query'], $query);
7777
}
7878

79-
$configuration = [
80-
'connection' => $components['host'],
81-
'table_name' => $options['table_name'] ?? ($query['table_name'] ?? self::DEFAULT_OPTIONS['table_name']),
82-
'queue_name' => $options['queue_name'] ?? ($query['queue_name'] ?? self::DEFAULT_OPTIONS['queue_name']),
83-
'redeliver_timeout' => $options['redeliver_timeout'] ?? ($query['redeliver_timeout'] ?? self::DEFAULT_OPTIONS['redeliver_timeout']),
84-
'auto_setup' => $options['auto_setup'] ?? ($query['auto_setup'] ?? self::DEFAULT_OPTIONS['auto_setup']),
85-
];
79+
$configuration = ['connection' => $components['host']];
80+
$configuration += $options + $query + self::DEFAULT_OPTIONS;
81+
82+
$configuration['auto_setup'] = filter_var($configuration['auto_setup'], FILTER_VALIDATE_BOOLEAN);
8683

8784
// check for extra keys in options
88-
$optionsExtraKeys = array_diff(array_keys($options), array_keys($configuration));
85+
$optionsExtraKeys = array_diff(array_keys($options), array_keys(self::DEFAULT_OPTIONS));
8986
if (0 < \count($optionsExtraKeys)) {
9087
throw new InvalidArgumentException(sprintf('Unknown option found : [%s]. Allowed options are [%s]', implode(', ', $optionsExtraKeys), implode(', ', self::DEFAULT_OPTIONS)));
9188
}
9289

9390
// check for extra keys in options
94-
$queryExtraKeys = array_diff(array_keys($query), array_keys($configuration));
91+
$queryExtraKeys = array_diff(array_keys($query), array_keys(self::DEFAULT_OPTIONS));
9592
if (0 < \count($queryExtraKeys)) {
9693
throw new InvalidArgumentException(sprintf('Unknown option found in DSN: [%s]. Allowed options are [%s]', implode(', ', $queryExtraKeys), implode(', ', self::DEFAULT_OPTIONS)));
9794
}

0 commit comments

Comments
 (0)