Skip to content

Commit 3e76da0

Browse files
committed
[client] add tests.
1 parent 4c960f3 commit 3e76da0

6 files changed

Lines changed: 158 additions & 1 deletion

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Enqueue\Bundle\Tests\Functional\App;
4+
5+
use Enqueue\Client\CommandSubscriberInterface;
6+
use Enqueue\Consumption\Result;
7+
use Enqueue\Psr\PsrContext;
8+
use Enqueue\Psr\PsrMessage;
9+
use Enqueue\Psr\PsrProcessor;
10+
11+
class TestCommandSubscriberProcessor implements PsrProcessor, CommandSubscriberInterface
12+
{
13+
public $calls = [];
14+
15+
public function process(PsrMessage $message, PsrContext $context)
16+
{
17+
$this->calls[] = $message;
18+
19+
return Result::reply(
20+
$context->createMessage($message->getBody().'Reply')
21+
);
22+
}
23+
24+
public static function getSubscribedCommand()
25+
{
26+
return 'test_command_subscriber';
27+
}
28+
}

‎pkg/enqueue-bundle/Tests/Functional/App/config/config.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ services:
4747
tags:
4848
- { name: 'kernel.event_listener', async: true, event: 'test_async', method: 'onEvent' }
4949

50+
test_command_subscriber_processor:
51+
class: 'Enqueue\Bundle\Tests\Functional\App\TestCommandSubscriberProcessor'
52+
tags:
53+
- { name: 'enqueue.client.processor' }
54+
5055
test_async_subscriber:
5156
class: 'Enqueue\Bundle\Tests\Functional\App\TestAsyncSubscriber'
5257
tags:
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Enqueue\Bundle\Tests\Functional\Client;
4+
5+
use Enqueue\Bundle\Tests\Functional\WebTestCase;
6+
use Enqueue\Client\Config;
7+
use Enqueue\Client\ProducerV2Interface;
8+
use Enqueue\Client\TraceableProducer;
9+
use Enqueue\Rpc\Promise;
10+
11+
/**
12+
* @group functional
13+
*/
14+
class ProducerV2Test extends WebTestCase
15+
{
16+
public function setUp()
17+
{
18+
parent::setUp();
19+
20+
$this->container->get('enqueue.client.producer')->clearTraces();
21+
}
22+
23+
public function tearDown()
24+
{
25+
parent::tearDown();
26+
27+
$this->container->get('enqueue.client.producer')->clearTraces();
28+
}
29+
30+
public function testCouldBeGetFromContainerAsService()
31+
{
32+
$producer = $this->container->get('enqueue.client.producer_v2');
33+
34+
$this->assertInstanceOf(ProducerV2Interface::class, $producer);
35+
}
36+
37+
public function testShouldSendEvent()
38+
{
39+
/** @var ProducerV2Interface $producer */
40+
$producer = $this->container->get('enqueue.client.producer_v2');
41+
42+
$producer->sendEvent('theTopic', 'theMessage');
43+
44+
$traces = $this->getTraceableProducer()->getTopicTraces('theTopic');
45+
46+
$this->assertCount(1, $traces);
47+
$this->assertEquals('theMessage', $traces[0]['body']);
48+
}
49+
50+
public function testShouldSendCommandWithoutNeedForReply()
51+
{
52+
/** @var ProducerV2Interface $producer */
53+
$producer = $this->container->get('enqueue.client.producer_v2');
54+
55+
$result = $producer->sendCommand('theCommand', 'theMessage', false);
56+
57+
$this->assertNull($result);
58+
59+
$traces = $this->getTraceableProducer()->getTopicTraces(Config::COMMAND_TOPIC);
60+
61+
$this->assertCount(1, $traces);
62+
$this->assertEquals('theMessage', $traces[0]['body']);
63+
$this->assertEquals([
64+
'enqueue.topic_name' => Config::COMMAND_TOPIC,
65+
'enqueue.processor_name' => 'theCommand',
66+
'enqueue.processor_queue_name' => 'default',
67+
], $traces[0]['properties']);
68+
}
69+
70+
public function testShouldSendCommandWithNeedForReply()
71+
{
72+
/** @var ProducerV2Interface $producer */
73+
$producer = $this->container->get('enqueue.client.producer_v2');
74+
75+
$result = $producer->sendCommand('theCommand', 'theMessage', true);
76+
77+
$this->assertInstanceOf(Promise::class, $result);
78+
79+
$traces = $this->getTraceableProducer()->getTopicTraces(Config::COMMAND_TOPIC);
80+
81+
$this->assertCount(1, $traces);
82+
$this->assertEquals('theMessage', $traces[0]['body']);
83+
$this->assertEquals([
84+
'enqueue.topic_name' => Config::COMMAND_TOPIC,
85+
'enqueue.processor_name' => 'theCommand',
86+
'enqueue.processor_queue_name' => 'default',
87+
], $traces[0]['properties']);
88+
}
89+
90+
/**
91+
* @return TraceableProducer|object
92+
*/
93+
private function getTraceableProducer()
94+
{
95+
return $this->container->get('enqueue.client.producer');
96+
}
97+
}

‎pkg/enqueue-bundle/Tests/Functional/QueuesCommandTest.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,16 @@ public function testShouldDisplayRegisteredQueues()
3030
$this->assertContains('enqueue.app.default', $display);
3131
$this->assertContains('enqueue.client.router_processor', $display);
3232
}
33+
34+
public function testShouldDisplayRegisteredCommand()
35+
{
36+
$command = $this->container->get('enqueue.client.meta.queues_command');
37+
38+
$tester = new CommandTester($command);
39+
$tester->execute([]);
40+
41+
$display = $tester->getDisplay();
42+
43+
$this->assertContains('test_command_subscriber', $display);
44+
}
3345
}

‎pkg/enqueue-bundle/Tests/Functional/TopicsCommandTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Bundle\Tests\Functional;
44

5+
use Enqueue\Client\Config;
56
use Enqueue\Symfony\Client\Meta\TopicsCommand;
67
use Symfony\Component\Console\Tester\CommandTester;
78

@@ -29,4 +30,17 @@ public function testShouldDisplayRegisteredTopics()
2930
$this->assertContains('__router__', $display);
3031
$this->assertContains('enqueue.client.router_processor', $display);
3132
}
33+
34+
public function testShouldDisplayCommands()
35+
{
36+
$command = $this->container->get('enqueue.client.meta.topics_command');
37+
38+
$tester = new CommandTester($command);
39+
$tester->execute([]);
40+
41+
$display = $tester->getDisplay();
42+
43+
$this->assertContains(Config::COMMAND_TOPIC, $display);
44+
$this->assertContains('test_command_subscriber', $display);
45+
}
3246
}

‎pkg/enqueue/Client/ProducerV2Interface.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ public function sendEvent($topic, $message);
1818
/**
1919
* @param string $command
2020
* @param string|array|Message $message
21+
* @param bool $needReply
2122
*
2223
* @return Promise|null the promise is returned if needReply argument is true
2324
*/
24-
public function sendCommand($command, $message);
25+
public function sendCommand($command, $message, $needReply = false);
2526
}

0 commit comments

Comments
 (0)