Skip to content

Commit 24b885c

Browse files
authored
feat: Add configuration message name to command class mapping (FRAM-164) (#7)
* feat: Add bind configuration for configure message name to command class mapping (#FRAM-164) * style: run php-cs-fixer
1 parent 8d425d2 commit 24b885c

8 files changed

Lines changed: 161 additions & 0 deletions

File tree

Consumption/ReceiverLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ private function configure(ReceiverBuilder $builder, array $config)
107107
if ($config['auto_handle'] ?? false) {
108108
$builder->jobProcessor();
109109
}
110+
111+
if (isset($config['bind'])) {
112+
$builder->bind((array) $config['bind']);
113+
}
110114
}
111115

112116
/**

DependencyInjection/Configuration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ private function getDestinationsNode()
156156
->booleanNode('auto_handle')
157157
->info('Set auto discover as outlet receiver. The message should contain target hint.')
158158
->defaultNull()->end()
159+
->arrayNode('bind')
160+
->info('Define the mapping of message name to class name for hydration. The key is the message name and the value is the class name.')
161+
->requiresAtLeastOneElement()
162+
->useAttributeAsKey('name')
163+
->scalarPrototype()->end()
164+
->end()
159165
->arrayNode('middlewares')
160166
->useAttributeAsKey('name')
161167
->variablePrototype()->end()

Tests/BdfQueueBundleTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
use Bdf\Queue\Testing\QueueHelper;
2929
use Bdf\QueueBundle\BdfQueueBundle;
3030
use Bdf\QueueBundle\Consumption\ReceiverLoader;
31+
use Bdf\QueueBundle\Tests\Fixtures\Bar;
32+
use Bdf\QueueBundle\Tests\Fixtures\Foo;
3133
use Bdf\QueueBundle\Tests\Fixtures\GetDestinationFactory;
3234
use Bdf\QueueBundle\Tests\Fixtures\TestHandler;
3335
use PHPUnit\Framework\TestCase;
@@ -82,6 +84,33 @@ public function testFunctional()
8284
$this->assertEquals([['foo' => 'bar']], $handler->messages);
8385
}
8486

87+
public function testWithBind()
88+
{
89+
$kernel = new \TestKernel(__DIR__.'/Fixtures/conf_with_bind.yaml');
90+
$kernel->boot();
91+
92+
/** @var DestinationManager $destinations */
93+
$destinations = $kernel->getContainer()->get('bdf_queue.destination_manager');
94+
$destination = $destinations->create('test');
95+
96+
$this->assertInstanceOf(DestinationInterface::class, $destination);
97+
98+
/** @var TestHandler $handler */
99+
$handler = $kernel->getContainer()->get(TestHandler::class);
100+
101+
$helper = new QueueHelper($kernel->getContainer());
102+
$destination->send((new Message(new Foo('azerty')))->setName('Foo'));
103+
$destination->send((new Message(new Bar(42)))->setName('Bar'));
104+
$destination->send((new Message(new Bar(42)))->setName('Baz'));
105+
106+
$helper->consume(3, 'test');
107+
$this->assertEquals([
108+
new Foo('azerty'),
109+
new Bar(42),
110+
['value' => 42], // Not bound to a class
111+
], $handler->messages);
112+
}
113+
85114
public function testWithJsonSerializer()
86115
{
87116
$kernel = new \TestKernel(__DIR__.'/Fixtures/conf_with_json_serializer.yaml');

Tests/Consumption/ReceiverLoaderTest.php

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

55
use Bdf\Instantiator\Instantiator;
66
use Bdf\Instantiator\InstantiatorInterface;
7+
use Bdf\Queue\Consumer\Receiver\Binder\BinderReceiver;
78
use Bdf\Queue\Consumer\Receiver\Builder\ReceiverFactory;
89
use Bdf\Queue\Consumer\Receiver\MemoryLimiterReceiver;
910
use Bdf\Queue\Consumer\Receiver\MessageCountLimiterReceiver;
@@ -71,6 +72,23 @@ public function testFullConfig()
7172
$this->assertEquals($chain, (string) $builder->build());
7273
}
7374

75+
public function testWithBind()
76+
{
77+
$loader = $this->getLoader([
78+
'foo' => [
79+
'bind' => [
80+
'Foo' => 'App\Event\Foo',
81+
'Bar' => 'App\Event\Bar',
82+
],
83+
],
84+
]);
85+
$builder = $loader->load('foo');
86+
87+
$chain = BinderReceiver::class.'->'.MessageLoggerReceiver::class.'->'.ProcessorReceiver::class;
88+
89+
$this->assertEquals($chain, (string) $builder->build());
90+
}
91+
7492
public function testWithoutServiceResetterConfig()
7593
{
7694
$loader = $this->getLoader([

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,49 @@ public function configDataProvider(): array
5555
'stop_when_empty' => true,
5656
'auto_handle' => false,
5757
'handler' => null,
58+
'bind' => [],
59+
'middlewares' => [],
60+
],
61+
],
62+
],
63+
'default_connection' => null,
64+
'default_serializer' => 'bdf',
65+
'failer' => 'memory:',
66+
'connections' => [],
67+
],
68+
],
69+
[
70+
'with bind',
71+
[
72+
'destinations' => [
73+
'foo' => [
74+
'consumer' => [
75+
'bind' => [
76+
'Foo' => 'App\Event\Foo',
77+
'Bar' => 'App\Event\Bar',
78+
],
79+
],
80+
],
81+
],
82+
],
83+
[
84+
'destinations' => [
85+
'foo' => [
86+
'consumer' => [
87+
'bind' => [
88+
'Foo' => 'App\Event\Foo',
89+
'Bar' => 'App\Event\Bar',
90+
],
91+
'handler' => null,
92+
'retry' => null,
93+
'max' => null,
94+
'limit' => null,
95+
'memory' => null,
96+
'save' => null,
97+
'no_failure' => null,
98+
'no_reset' => null,
99+
'stop_when_empty' => null,
100+
'auto_handle' => null,
58101
'middlewares' => [],
59102
],
60103
],

Tests/Fixtures/Bar.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Bdf\QueueBundle\Tests\Fixtures;
4+
5+
class Bar
6+
{
7+
/**
8+
* @var int
9+
*/
10+
public $value;
11+
12+
public function __construct(int $value)
13+
{
14+
$this->value = $value;
15+
}
16+
}

Tests/Fixtures/Foo.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Bdf\QueueBundle\Tests\Fixtures;
4+
5+
class Foo
6+
{
7+
/**
8+
* @var string
9+
*/
10+
public $value;
11+
12+
public function __construct(string $value)
13+
{
14+
$this->value = $value;
15+
}
16+
}

Tests/Fixtures/conf_with_bind.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
bdf_queue:
2+
default_connection: 'gearman'
3+
default_serializer: 'native'
4+
connections:
5+
memory:
6+
url: 'memory:'
7+
serializer: 'json'
8+
destinations:
9+
test:
10+
url: 'queue://memory/test'
11+
consumer:
12+
handler: '@Bdf\QueueBundle\Tests\Fixtures\TestHandler'
13+
bind:
14+
Foo: Bdf\QueueBundle\Tests\Fixtures\Foo
15+
Bar: Bdf\QueueBundle\Tests\Fixtures\Bar
16+
17+
prime:
18+
default_connection: 'my_connection'
19+
connections:
20+
my_connection: 'sqlite::memory:'
21+
22+
migration:
23+
connection: 'my_connection'
24+
path: '%kernel.project_dir%/src/Migration'
25+
26+
services:
27+
Bdf\QueueBundle\Tests\Fixtures\TestHandler:
28+
class: Bdf\QueueBundle\Tests\Fixtures\TestHandler
29+
public: true

0 commit comments

Comments
 (0)