Skip to content

Commit dd55031

Browse files
committed
Add tests.
1 parent 2c84a24 commit dd55031

16 files changed

+1251
-425
lines changed

pkg/enqueue/Client/ChainExtension.php

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

33
namespace Enqueue\Client;
44

5-
class ChainExtension implements ExtensionInterface
5+
final class ChainExtension implements ExtensionInterface
66
{
77
/**
88
* @var ExtensionInterface[]
@@ -14,7 +14,9 @@ class ChainExtension implements ExtensionInterface
1414
*/
1515
public function __construct(array $extensions)
1616
{
17-
$this->extensions = $extensions;
17+
array_walk($extensions, function (ExtensionInterface $extension) {
18+
$this->extensions[] = $extension;
19+
});
1820
}
1921

2022
public function onPreSendEvent(PreSend $event): void

pkg/enqueue/Client/ConsumptionExtension/ExclusiveCommandExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Enqueue\Consumption\EmptyExtensionTrait as ConsumptionEmptyExtensionTrait;
1111
use Enqueue\Consumption\ExtensionInterface as ConsumptionExtensionInterface;
1212

13-
class ExclusiveCommandExtension implements ConsumptionExtensionInterface, ClientExtensionInterface
13+
final class ExclusiveCommandExtension implements ConsumptionExtensionInterface, ClientExtensionInterface
1414
{
1515
use ConsumptionEmptyExtensionTrait, ClientEmptyExtensionTrait;
1616

pkg/enqueue/Client/DriverPreSend.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Enqueue\Client;
44

5-
class DriverPreSend
5+
final class DriverPreSend
66
{
77
private $message;
88

pkg/enqueue/Client/PostSend.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Enqueue\Client;
44

5-
class PostSend
5+
final class PostSend
66
{
77
private $message;
88

pkg/enqueue/Client/PreSend.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Enqueue\Client;
44

5-
class PreSend
5+
final class PreSend
66
{
77
private $message;
88

pkg/enqueue/Client/Producer.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
use Enqueue\Rpc\RpcFactory;
77
use Enqueue\Util\UUID;
88

9-
class Producer implements ProducerInterface
9+
final class Producer implements ProducerInterface
1010
{
1111
/**
1212
* @var DriverInterface
1313
*/
14-
protected $driver;
14+
private $driver;
1515

1616
/**
1717
* @var ExtensionInterface
@@ -23,13 +23,6 @@ class Producer implements ProducerInterface
2323
*/
2424
private $rpcFactory;
2525

26-
/**
27-
* @param DriverInterface $driver
28-
* @param ExtensionInterface|null $extension
29-
* @param RpcFactory $rpcFactory
30-
*
31-
* @internal param RpcClient $rpcClient
32-
*/
3326
public function __construct(
3427
DriverInterface $driver,
3528
RpcFactory $rpcFactory,
@@ -38,8 +31,10 @@ public function __construct(
3831
$this->driver = $driver;
3932
$this->rpcFactory = $rpcFactory;
4033

41-
$prepareBodyExtension = new PrepareBodyExtension();
42-
$this->extension = new ChainExtension([$extension, $prepareBodyExtension]) ?: new ChainExtension([$prepareBodyExtension]);
34+
$this->extension = $extension ?
35+
new ChainExtension([$extension, new PrepareBodyExtension()]) :
36+
new ChainExtension([new PrepareBodyExtension()])
37+
;
4338
}
4439

4540
public function sendEvent($topic, $message)
@@ -66,7 +61,7 @@ public function sendCommand($command, $message, $needReply = false)
6661
}
6762

6863
$preSend = new PreSend($command, $message, $this, $this->driver);
69-
$this->extension->onPreSendEvent($preSend);
64+
$this->extension->onPreSendCommand($preSend);
7065

7166
$command = $preSend->getCommand();
7267
$message = $preSend->getMessage();
@@ -87,7 +82,6 @@ public function sendCommand($command, $message, $needReply = false)
8782

8883
$message->setProperty(Config::PARAMETER_TOPIC_NAME, Config::COMMAND_TOPIC);
8984
$message->setProperty(Config::PARAMETER_COMMAND_NAME, $command);
90-
$message->setProperty(Config::PARAMETER_TOPIC_NAME, Config::COMMAND_TOPIC);
9185
$message->setScope(Message::SCOPE_APP);
9286

9387
$this->doSend($message);
@@ -137,7 +131,7 @@ private function doSend(Message $message)
137131
throw new \LogicException(sprintf('The %s property must not be set for messages that are sent to message bus.', Config::PARAMETER_PROCESSOR_NAME));
138132
}
139133

140-
$this->extension->onPreDriverSend(new DriverPreSend($message, $this, $this->driver));
134+
$this->extension->onDriverPreSend(new DriverPreSend($message, $this, $this->driver));
141135
$this->driver->sendToRouter($message);
142136
} elseif (Message::SCOPE_APP == $message->getScope()) {
143137
if (false == $message->getProperty(Config::PARAMETER_PROCESSOR_NAME)) {
@@ -147,8 +141,8 @@ private function doSend(Message $message)
147141
$message->setProperty(Config::PARAMETER_PROCESSOR_QUEUE_NAME, $this->driver->getConfig()->getRouterQueueName());
148142
}
149143

150-
$this->extension->onPreDriverSend(new DriverPreSend($message, $this, $this->driver));
151-
$this->driver->sendToRouter($message);
144+
$this->extension->onDriverPreSend(new DriverPreSend($message, $this, $this->driver));
145+
$this->driver->sendToProcessor($message);
152146
} else {
153147
throw new \LogicException(sprintf('The message scope "%s" is not supported.', $message->getScope()));
154148
}

pkg/enqueue/Tests/Client/ChainExtensionTest.php

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
namespace Enqueue\Tests\Client;
44

55
use Enqueue\Client\ChainExtension;
6+
use Enqueue\Client\DriverInterface;
7+
use Enqueue\Client\DriverPreSend;
68
use Enqueue\Client\ExtensionInterface;
79
use Enqueue\Client\Message;
10+
use Enqueue\Client\PostSend;
11+
use Enqueue\Client\PreSend;
12+
use Enqueue\Client\ProducerInterface;
813
use Enqueue\Test\ClassExtensionTrait;
914
use PHPUnit\Framework\TestCase;
1015

@@ -17,53 +22,128 @@ public function testShouldImplementExtensionInterface()
1722
$this->assertClassImplements(ExtensionInterface::class, ChainExtension::class);
1823
}
1924

25+
public function testShouldBeFinal()
26+
{
27+
$this->assertClassFinal(ChainExtension::class);
28+
}
29+
2030
public function testCouldBeConstructedWithExtensionsArray()
2131
{
2232
new ChainExtension([$this->createExtension(), $this->createExtension()]);
2333
}
2434

25-
public function testShouldProxyOnPreSendToAllInternalExtensions()
35+
public function testThrowIfArrayContainsNotExtension()
36+
{
37+
$this->expectException(\TypeError::class);
38+
$this->expectExceptionMessage('Argument 1 passed to');
39+
40+
new ChainExtension([$this->createExtension(), new \stdClass()]);
41+
}
42+
43+
public function testShouldProxyOnPreSendEventToAllInternalExtensions()
44+
{
45+
$preSend = new PreSend(
46+
'aCommandOrTopic',
47+
new Message(),
48+
$this->createMock(ProducerInterface::class),
49+
$this->createMock(DriverInterface::class)
50+
);
51+
52+
$fooExtension = $this->createExtension();
53+
$fooExtension
54+
->expects($this->once())
55+
->method('onPreSendEvent')
56+
->with($this->identicalTo($preSend))
57+
;
58+
$barExtension = $this->createExtension();
59+
$barExtension
60+
->expects($this->once())
61+
->method('onPreSendEvent')
62+
->with($this->identicalTo($preSend))
63+
;
64+
65+
$extensions = new ChainExtension([$fooExtension, $barExtension]);
66+
67+
$extensions->onPreSendEvent($preSend);
68+
}
69+
70+
public function testShouldProxyOnPreSendCommandToAllInternalExtensions()
71+
{
72+
$preSend = new PreSend(
73+
'aCommandOrTopic',
74+
new Message(),
75+
$this->createMock(ProducerInterface::class),
76+
$this->createMock(DriverInterface::class)
77+
);
78+
79+
$fooExtension = $this->createExtension();
80+
$fooExtension
81+
->expects($this->once())
82+
->method('onPreSendCommand')
83+
->with($this->identicalTo($preSend))
84+
;
85+
$barExtension = $this->createExtension();
86+
$barExtension
87+
->expects($this->once())
88+
->method('onPreSendCommand')
89+
->with($this->identicalTo($preSend))
90+
;
91+
92+
$extensions = new ChainExtension([$fooExtension, $barExtension]);
93+
94+
$extensions->onPreSendCommand($preSend);
95+
}
96+
97+
public function testShouldProxyOnDriverPreSendToAllInternalExtensions()
2698
{
27-
$message = new Message();
99+
$driverPreSend = new DriverPreSend(
100+
new Message(),
101+
$this->createMock(ProducerInterface::class),
102+
$this->createMock(DriverInterface::class)
103+
);
28104

29105
$fooExtension = $this->createExtension();
30106
$fooExtension
31107
->expects($this->once())
32-
->method('onPreSend')
33-
->with('topic', $this->identicalTo($message))
108+
->method('onDriverPreSend')
109+
->with($this->identicalTo($driverPreSend))
34110
;
35111
$barExtension = $this->createExtension();
36112
$barExtension
37113
->expects($this->once())
38-
->method('onPreSend')
39-
->with('topic', $this->identicalTo($message))
114+
->method('onDriverPreSend')
115+
->with($this->identicalTo($driverPreSend))
40116
;
41117

42118
$extensions = new ChainExtension([$fooExtension, $barExtension]);
43119

44-
$extensions->onPreSend('topic', $message);
120+
$extensions->onDriverPreSend($driverPreSend);
45121
}
46122

47-
public function testShouldProxyOnPostSendToAllInternalExtensions()
123+
public function testShouldProxyOnPostSentToAllInternalExtensions()
48124
{
49-
$message = new Message();
125+
$postSend = new PostSend(
126+
new Message(),
127+
$this->createMock(ProducerInterface::class),
128+
$this->createMock(DriverInterface::class)
129+
);
50130

51131
$fooExtension = $this->createExtension();
52132
$fooExtension
53133
->expects($this->once())
54134
->method('onPostSend')
55-
->with('topic', $this->identicalTo($message))
135+
->with($this->identicalTo($postSend))
56136
;
57137
$barExtension = $this->createExtension();
58138
$barExtension
59139
->expects($this->once())
60140
->method('onPostSend')
61-
->with('topic', $this->identicalTo($message))
141+
->with($this->identicalTo($postSend))
62142
;
63143

64144
$extensions = new ChainExtension([$fooExtension, $barExtension]);
65145

66-
$extensions->onPostSend('topic', $message);
146+
$extensions->onPostSend($postSend);
67147
}
68148

69149
/**

pkg/enqueue/Tests/Client/ConsumptionExtension/ExclusiveCommandExtensionTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
use Enqueue\Client\Config;
66
use Enqueue\Client\ConsumptionExtension\ExclusiveCommandExtension;
7+
use Enqueue\Client\DriverInterface;
78
use Enqueue\Client\ExtensionInterface as ClientExtensionInterface;
89
use Enqueue\Client\Message;
10+
use Enqueue\Client\PreSend;
11+
use Enqueue\Client\ProducerInterface;
912
use Enqueue\Consumption\Context;
1013
use Enqueue\Consumption\ExtensionInterface as ConsumptionExtensionInterface;
1114
use Enqueue\Null\NullContext;
@@ -24,6 +27,11 @@ public function testShouldImplementConsumptionExtensionInterface()
2427
$this->assertClassImplements(ConsumptionExtensionInterface::class, ExclusiveCommandExtension::class);
2528
}
2629

30+
public function testShouldBeFinal()
31+
{
32+
$this->assertClassFinal(ExclusiveCommandExtension::class);
33+
}
34+
2735
public function testShouldImplementClientExtensionInterface()
2836
{
2937
$this->assertClassImplements(ClientExtensionInterface::class, ExclusiveCommandExtension::class);
@@ -145,15 +153,15 @@ public function testShouldSetCommandPropertiesIfCurrentQueueInTheMap()
145153
], $message->getProperties());
146154
}
147155

148-
public function testShouldDoNothingOnPreSendIfTopicNotCommandOne()
156+
public function testShouldDoNothingOnPreSendEvent()
149157
{
150158
$message = new Message();
151159

152160
$extension = new ExclusiveCommandExtension([
153161
'aFooQueueName' => 'aFooProcessorName',
154162
]);
155163

156-
$extension->onPreSend('aTopic', $message);
164+
$extension->onPreSendEvent($this->createDummyPreSend('aTopic', $message));
157165

158166
$this->assertEquals([], $message->getProperties());
159167
}
@@ -167,7 +175,7 @@ public function testShouldDoNothingIfCommandNotExclusive()
167175
'aFooQueueName' => 'aFooProcessorName',
168176
]);
169177

170-
$extension->onPreSend(Config::COMMAND_TOPIC, $message);
178+
$extension->onPreSendCommand($this->createDummyPreSend('theBarProcessorName', $message));
171179

172180
$this->assertEquals([
173181
'enqueue.command_name' => 'theBarProcessorName',
@@ -183,12 +191,22 @@ public function testShouldForceExclusiveCommandQueue()
183191
'aFooQueueName' => 'aFooProcessorName',
184192
]);
185193

186-
$extension->onPreSend(Config::COMMAND_TOPIC, $message);
194+
$extension->onPreSendCommand($this->createDummyPreSend('aFooProcessorName', $message));
187195

188196
$this->assertEquals([
189197
'enqueue.command_name' => 'aFooProcessorName',
190198
'enqueue.processor_name' => 'aFooProcessorName',
191199
'enqueue.processor_queue_name' => 'aFooQueueName',
192200
], $message->getProperties());
193201
}
202+
203+
private function createDummyPreSend(string $commandOrTopic, Message $message): PreSend
204+
{
205+
return new PreSend(
206+
$commandOrTopic,
207+
$message,
208+
$this->createMock(ProducerInterface::class),
209+
$this->createMock(DriverInterface::class)
210+
);
211+
}
194212
}

pkg/enqueue/Tests/Client/Extension/PrepareBodyExtensionTest.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Enqueue\Client\Message;
99
use Enqueue\Client\PreSend;
1010
use Enqueue\Client\ProducerInterface;
11+
use Enqueue\Tests\Mocks\JsonSerializableObject;
1112
use PHPUnit\Framework\TestCase;
1213

1314
class PrepareBodyExtensionTest extends TestCase
@@ -132,11 +133,3 @@ private function createDummyPreSendContext($commandOrTopic, $message): PreSend
132133
);
133134
}
134135
}
135-
136-
class JsonSerializableObject implements \JsonSerializable
137-
{
138-
public function jsonSerialize()
139-
{
140-
return ['foo' => 'fooVal'];
141-
}
142-
}

0 commit comments

Comments
 (0)