Skip to content

Commit d47155e

Browse files
committed
Revert "Revert "[symfony] dsn transport factory.""
This reverts commit e4306f5ee691c9911342a6434aa0c6aebe274469.
1 parent 1ff6a4b commit d47155e

File tree

4 files changed

+154
-90
lines changed

4 files changed

+154
-90
lines changed

DependencyInjection/EnqueueExtension.php

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

55
use Enqueue\Client\TraceableProducer;
66
use Enqueue\JobQueue\Job;
7+
use Enqueue\Symfony\DsnTransportFactory;
78
use Enqueue\Symfony\TransportFactoryInterface;
89
use Symfony\Component\Config\FileLocator;
910
use Symfony\Component\Config\Resource\FileResource;
@@ -47,6 +48,8 @@ public function addTransportFactory(TransportFactoryInterface $transportFactory)
4748
*/
4849
public function load(array $configs, ContainerBuilder $container)
4950
{
51+
$this->factories['dsn'] = new DsnTransportFactory($this->factories);
52+
5053
$config = $this->processConfiguration(new Configuration($this->factories), $configs);
5154

5255
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

Tests/Functional/UseCasesTest.php

Lines changed: 92 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -16,87 +16,105 @@ class UseCasesTest extends WebTestCase
1616
{
1717
public function provideEnqueueConfigs()
1818
{
19-
return [
20-
['amqp' => [
21-
'transport' => [
22-
'default' => 'amqp',
23-
'amqp' => [
24-
'host' => getenv('SYMFONY__RABBITMQ__HOST'),
25-
'port' => getenv('SYMFONY__RABBITMQ__AMQP__PORT'),
26-
'user' => getenv('SYMFONY__RABBITMQ__USER'),
27-
'pass' => getenv('SYMFONY__RABBITMQ__PASSWORD'),
28-
'vhost' => getenv('SYMFONY__RABBITMQ__VHOST'),
29-
'lazy' => false,
30-
]
19+
yield 'amqp' => [[
20+
'transport' => [
21+
'default' => 'amqp',
22+
'amqp' => [
23+
'host' => getenv('SYMFONY__RABBITMQ__HOST'),
24+
'port' => getenv('SYMFONY__RABBITMQ__AMQP__PORT'),
25+
'user' => getenv('SYMFONY__RABBITMQ__USER'),
26+
'pass' => getenv('SYMFONY__RABBITMQ__PASSWORD'),
27+
'vhost' => getenv('SYMFONY__RABBITMQ__VHOST'),
28+
'lazy' => false,
3129
]
32-
]],
33-
['stomp' => [
34-
'transport' => [
35-
'default' => 'stomp',
36-
'stomp' => [
37-
'host' => getenv('SYMFONY__RABBITMQ__HOST'),
38-
'port' => getenv('SYMFONY__RABBITMQ__STOMP__PORT'),
39-
'login' => getenv('SYMFONY__RABBITMQ__USER'),
40-
'password' => getenv('SYMFONY__RABBITMQ__PASSWORD'),
41-
'vhost' => getenv('SYMFONY__RABBITMQ__VHOST'),
42-
'lazy' => false,
43-
]
30+
]
31+
]];
32+
33+
yield 'amqp_dsn' => [[
34+
'transport' => [
35+
'default' => 'amqp',
36+
'amqp' => getenv('AMQP_DSN'),
37+
]
38+
]];
39+
40+
yield 'dsn_amqp' => [[
41+
'transport' => [
42+
'default' => 'dsn',
43+
'dsn' => getenv('AMQP_DSN'),
44+
]
45+
]];
46+
47+
yield 'stomp' => [[
48+
'transport' => [
49+
'default' => 'stomp',
50+
'stomp' => [
51+
'host' => getenv('SYMFONY__RABBITMQ__HOST'),
52+
'port' => getenv('SYMFONY__RABBITMQ__STOMP__PORT'),
53+
'login' => getenv('SYMFONY__RABBITMQ__USER'),
54+
'password' => getenv('SYMFONY__RABBITMQ__PASSWORD'),
55+
'vhost' => getenv('SYMFONY__RABBITMQ__VHOST'),
56+
'lazy' => false,
4457
]
45-
]],
46-
['predis' => [
47-
'transport' => [
48-
'default' => 'redis',
49-
'redis' => [
50-
'host' => getenv('SYMFONY__REDIS__HOST'),
51-
'port' => (int) getenv('SYMFONY__REDIS__PORT'),
52-
'vendor' => 'predis',
53-
'lazy' => false,
54-
]
58+
]
59+
]];
60+
61+
yield 'predis' => [[
62+
'transport' => [
63+
'default' => 'redis',
64+
'redis' => [
65+
'host' => getenv('SYMFONY__REDIS__HOST'),
66+
'port' => (int) getenv('SYMFONY__REDIS__PORT'),
67+
'vendor' => 'predis',
68+
'lazy' => false,
5569
]
56-
]],
57-
['phpredis' => [
58-
'transport' => [
59-
'default' => 'redis',
60-
'redis' => [
61-
'host' => getenv('SYMFONY__REDIS__HOST'),
62-
'port' => (int) getenv('SYMFONY__REDIS__PORT'),
63-
'vendor' => 'phpredis',
64-
'lazy' => false,
65-
]
70+
]
71+
]];
72+
73+
yield 'phpredis' => [[
74+
'transport' => [
75+
'default' => 'redis',
76+
'redis' => [
77+
'host' => getenv('SYMFONY__REDIS__HOST'),
78+
'port' => (int) getenv('SYMFONY__REDIS__PORT'),
79+
'vendor' => 'phpredis',
80+
'lazy' => false,
6681
]
67-
]],
68-
['fs' => [
69-
'transport' => [
70-
'default' => 'fs',
71-
'fs' => [
72-
'store_dir' => sys_get_temp_dir(),
73-
]
82+
]
83+
]];
84+
85+
yield 'fs' => [[
86+
'transport' => [
87+
'default' => 'fs',
88+
'fs' => [
89+
'store_dir' => sys_get_temp_dir(),
7490
]
75-
]],
76-
['dbal' => [
77-
'transport' => [
78-
'default' => 'dbal',
79-
'dbal' => [
80-
'dbname' => getenv('SYMFONY__DB__NAME'),
81-
'user' => getenv('SYMFONY__DB__USER'),
82-
'password' => getenv('SYMFONY__DB__PASSWORD'),
83-
'host' => getenv('SYMFONY__DB__HOST'),
84-
'port' => getenv('SYMFONY__DB__PORT'),
85-
'driver' => getenv('SYMFONY__DB__DRIVER'),
86-
]
91+
]
92+
]];
93+
94+
yield 'dbal' => [[
95+
'transport' => [
96+
'default' => 'dbal',
97+
'dbal' => [
98+
'dbname' => getenv('SYMFONY__DB__NAME'),
99+
'user' => getenv('SYMFONY__DB__USER'),
100+
'password' => getenv('SYMFONY__DB__PASSWORD'),
101+
'host' => getenv('SYMFONY__DB__HOST'),
102+
'port' => getenv('SYMFONY__DB__PORT'),
103+
'driver' => getenv('SYMFONY__DB__DRIVER'),
87104
]
88-
]],
89-
['sqs' => [
90-
'transport' => [
91-
'default' => 'sqs',
92-
'sqs' => [
93-
'key' => getenv('AWS__SQS__KEY'),
94-
'secret' => getenv('AWS__SQS__SECRET'),
95-
'region' => getenv('AWS__SQS__REGION'),
96-
]
105+
]
106+
]];
107+
108+
yield 'sqs' => [[
109+
'transport' => [
110+
'default' => 'sqs',
111+
'sqs' => [
112+
'key' => getenv('AWS__SQS__KEY'),
113+
'secret' => getenv('AWS__SQS__SECRET'),
114+
'region' => getenv('AWS__SQS__REGION'),
97115
]
98-
]],
99-
];
116+
]
117+
]];
100118
}
101119

102120
/**

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Enqueue\Bundle\Tests\Unit\Mocks\FooTransportFactory;
77
use Enqueue\Symfony\DefaultTransportFactory;
88
use Enqueue\Null\Symfony\NullTransportFactory;
9+
use Enqueue\Symfony\DsnTransportFactory;
910
use Enqueue\Test\ClassExtensionTrait;
1011
use Symfony\Component\Config\Definition\ConfigurationInterface;
1112
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
@@ -28,10 +29,8 @@ public function testCouldBeConstructedWithFactoriesAsFirstArgument()
2829

2930
public function testThrowIfTransportNotConfigured()
3031
{
31-
$this->setExpectedException(
32-
InvalidConfigurationException::class,
33-
'The child node "transport" at path "enqueue" must be configured.'
34-
);
32+
$this->expectException(InvalidConfigurationException::class);
33+
$this->expectExceptionMessage('The child node "transport" at path "enqueue" must be configured.');
3534

3635
$configuration = new Configuration([]);
3736

@@ -59,10 +58,8 @@ public function testThrowExceptionIfFooTransportConfigInvalid()
5958

6059
$processor = new Processor();
6160

62-
$this->setExpectedException(
63-
InvalidConfigurationException::class,
64-
'The path "enqueue.transport.foo.foo_param" cannot contain an empty value, but got null.'
65-
);
61+
$this->expectException(InvalidConfigurationException::class);
62+
$this->expectExceptionMessage('The path "enqueue.transport.foo.foo_param" cannot contain an empty value, but got null.');
6663

6764
$processor->processConfiguration($configuration, [[
6865
'transport' => [
@@ -103,6 +100,31 @@ public function testShouldAllowConfigureNullTransport()
103100
], $config);
104101
}
105102

103+
public function testShouldAllowConfigureNullTransportViaDsnTransport()
104+
{
105+
$nullFactory = new NullTransportFactory();
106+
107+
$configuration = new Configuration([
108+
$nullFactory,
109+
new DsnTransportFactory([$nullFactory])
110+
]);
111+
112+
$processor = new Processor();
113+
$config = $processor->processConfiguration($configuration, [[
114+
'transport' => [
115+
'dsn' => 'null://',
116+
],
117+
]]);
118+
119+
$this->assertArraySubset([
120+
'transport' => [
121+
'dsn' => [
122+
'dsn' => 'null://'
123+
],
124+
],
125+
], $config);
126+
}
127+
106128
public function testShouldAllowConfigureSeveralTransportsSameTime()
107129
{
108130
$configuration = new Configuration([
@@ -160,10 +182,8 @@ public function testShouldSetDefaultConfigurationForClient()
160182

161183
public function testThrowExceptionIfRouterTopicIsEmpty()
162184
{
163-
$this->setExpectedException(
164-
InvalidConfigurationException::class,
165-
'The path "enqueue.client.router_topic" cannot contain an empty value, but got "".'
166-
);
185+
$this->expectException(InvalidConfigurationException::class);
186+
$this->expectExceptionMessage('The path "enqueue.client.router_topic" cannot contain an empty value, but got "".');
167187

168188
$configuration = new Configuration([new DefaultTransportFactory()]);
169189

@@ -180,10 +200,8 @@ public function testThrowExceptionIfRouterTopicIsEmpty()
180200

181201
public function testThrowExceptionIfRouterQueueIsEmpty()
182202
{
183-
$this->setExpectedException(
184-
InvalidConfigurationException::class,
185-
'The path "enqueue.client.router_queue" cannot contain an empty value, but got "".'
186-
);
203+
$this->expectException(InvalidConfigurationException::class);
204+
$this->expectExceptionMessage('The path "enqueue.client.router_queue" cannot contain an empty value, but got "".');
187205

188206
$configuration = new Configuration([new DefaultTransportFactory()]);
189207

Tests/Unit/DependencyInjection/EnqueueExtensionTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,31 @@ public function testShouldUseNullTransportAsDefault()
9595
);
9696
}
9797

98+
public function testShouldUseNullTransportAsDefaultConfiguredViaDSN()
99+
{
100+
$container = new ContainerBuilder();
101+
102+
$extension = new EnqueueExtension();
103+
$extension->addTransportFactory(new NullTransportFactory());
104+
$extension->addTransportFactory(new DefaultTransportFactory());
105+
106+
$extension->load([[
107+
'transport' => [
108+
'default' => 'dsn',
109+
'dsn' => 'null://',
110+
],
111+
]], $container);
112+
113+
self::assertEquals(
114+
'enqueue.transport.default.context',
115+
(string) $container->getAlias('enqueue.transport.context')
116+
);
117+
self::assertEquals(
118+
'enqueue.transport.dsn.context',
119+
(string) $container->getAlias('enqueue.transport.default.context')
120+
);
121+
}
122+
98123
public function testShouldConfigureFooTransport()
99124
{
100125
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)