Skip to content

Commit c3740ef

Browse files
committed
fix enqueue tests.
1 parent 93f9165 commit c3740ef

5 files changed

Lines changed: 118 additions & 73 deletions

File tree

‎pkg/enqueue/Client/Amqp/AmqpDriver.php‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function createTransportMessage(Message $message)
145145
$transportMessage->setDeliveryMode(AmqpMessage::DELIVERY_MODE_PERSISTENT);
146146

147147
if ($message->getExpire()) {
148-
$transportMessage->setExpiration((string) ($message->getExpire() * 1000));
148+
$transportMessage->setExpiration($message->getExpire() * 1000);
149149
}
150150

151151
return $transportMessage;
@@ -166,11 +166,7 @@ public function createClientMessage(PsrMessage $message)
166166
$clientMessage->setContentType($message->getContentType());
167167

168168
if ($expiration = $message->getExpiration()) {
169-
if (false == is_numeric($expiration)) {
170-
throw new \LogicException(sprintf('expiration header is not numeric. "%s"', $expiration));
171-
}
172-
173-
$clientMessage->setExpire((int) ((int) $expiration) / 1000);
169+
$clientMessage->setExpire((int) ($expiration / 1000));
174170
}
175171

176172
$clientMessage->setMessageId($message->getMessageId());

‎pkg/enqueue/Tests/Client/Amqp/AmqpDriverTest.php‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,6 @@ public function testShouldConvertTransportMessageToClientMessage()
127127
$this->assertSame('theCorrelationId', $clientMessage->getCorrelationId());
128128
}
129129

130-
public function testShouldThrowExceptionIfExpirationIsNotNumeric()
131-
{
132-
$transportMessage = new AmqpMessage();
133-
$transportMessage->setHeader('expiration', 'is-not-numeric');
134-
135-
$driver = new AmqpDriver(
136-
$this->createAmqpContextMock(),
137-
$this->createDummyConfig(),
138-
$this->createDummyQueueMetaRegistry()
139-
);
140-
141-
$this->expectException(\LogicException::class);
142-
$this->expectExceptionMessage('expiration header is not numeric. "is-not-numeric"');
143-
144-
$driver->createClientMessage($transportMessage);
145-
}
146-
147130
public function testShouldConvertClientMessageToTransportMessage()
148131
{
149132
$clientMessage = new Message();

‎pkg/enqueue/Tests/Client/Amqp/RabbitMqDriverTest.php‎

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -157,40 +157,6 @@ public function testShouldThrowExceptionIfXDelayIsNotNumeric()
157157
$driver->createClientMessage($transportMessage);
158158
}
159159

160-
public function testShouldThrowExceptionIfExpirationIsNotNumeric()
161-
{
162-
$transportMessage = new AmqpMessage();
163-
$transportMessage->setHeader('expiration', 'is-not-numeric');
164-
165-
$driver = new RabbitMqDriver(
166-
$this->createAmqpContextMock(),
167-
Config::create(),
168-
$this->createDummyQueueMetaRegistry()
169-
);
170-
171-
$this->expectException(\LogicException::class);
172-
$this->expectExceptionMessage('expiration header is not numeric. "is-not-numeric"');
173-
174-
$driver->createClientMessage($transportMessage);
175-
}
176-
177-
public function testShouldThrowExceptionIfCantConvertTransportPriorityToClientPriority()
178-
{
179-
$transportMessage = new AmqpMessage();
180-
$transportMessage->setHeader('priority', 'unknown');
181-
182-
$driver = new RabbitMqDriver(
183-
$this->createAmqpContextMock(),
184-
Config::create(),
185-
$this->createDummyQueueMetaRegistry()
186-
);
187-
188-
$this->expectException(\LogicException::class);
189-
$this->expectExceptionMessage('Cant convert transport priority to client: "unknown"');
190-
191-
$driver->createClientMessage($transportMessage);
192-
}
193-
194160
public function testShouldThrowExceptionIfCantConvertClientPriorityToTransportPriority()
195161
{
196162
$clientMessage = new Message();

‎pkg/enqueue/Tests/Consumption/Mock/DummySubscriptionConsumer.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class DummySubscriptionConsumer implements PsrSubscriptionConsumer
1515
/**
1616
* @param float|int $timeout milliseconds 1000 is 1 second, a zero is consume endlessly
1717
*/
18-
public function consume($timeout = 0)
18+
public function consume(int $timeout = 0): void
1919
{
2020
foreach ($this->messages as list($message, $queueName)) {
2121
/** @var PsrMessage $message */
@@ -26,22 +26,22 @@ public function consume($timeout = 0)
2626
}
2727
}
2828

29-
public function subscribe(PsrConsumer $consumer, callable $callback)
29+
public function subscribe(PsrConsumer $consumer, callable $callback): void
3030
{
3131
$this->subscriptions[$consumer->getQueue()->getQueueName()] = [$consumer, $callback];
3232
}
3333

34-
public function unsubscribe(PsrConsumer $consumer)
34+
public function unsubscribe(PsrConsumer $consumer): void
3535
{
3636
unset($this->subscriptions[$consumer->getQueue()->getQueueName()]);
3737
}
3838

39-
public function unsubscribeAll()
39+
public function unsubscribeAll(): void
4040
{
4141
$this->subscriptions = [];
4242
}
4343

44-
public function addMessage(PsrMessage $message, string $queueName)
44+
public function addMessage(PsrMessage $message, string $queueName): void
4545
{
4646
$this->messages[] = [$message, $queueName];
4747
}

‎pkg/enqueue/Tests/Consumption/QueueConsumerTest.php‎

Lines changed: 111 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Interop\Queue\PsrProcessor;
1919
use Interop\Queue\PsrQueue;
2020
use Interop\Queue\PsrSubscriptionConsumer;
21+
use Interop\Queue\SubscriptionConsumerNotSupportedException;
2122
use PHPUnit\Framework\TestCase;
2223
use Psr\Log\NullLogger;
2324

@@ -127,7 +128,7 @@ public function testShouldAllowBindCallbackToQueueName()
127128
$queueName = 'theQueueName';
128129
$queue = new NullQueue($queueName);
129130

130-
$context = $this->createMock(PsrContext::class);
131+
$context = $this->createContextWithoutSubscriptionConsumerMock();
131132
$context
132133
->expects($this->once())
133134
->method('createQueue')
@@ -160,6 +161,90 @@ public function testShouldReturnSelfOnBind()
160161
$this->assertSame($consumer, $consumer->bind(new NullQueue('foo_queue'), $processorMock));
161162
}
162163

164+
public function testShouldUseContextSubscriptionConsumerIfSupport()
165+
{
166+
$expectedQueue = new NullQueue('theQueueName');
167+
168+
$contextSubscriptionConsumer = $this->createSubscriptionConsumerMock();
169+
$contextSubscriptionConsumer
170+
->expects($this->once())
171+
->method('consume')
172+
->willReturn(null)
173+
;
174+
175+
$fallbackSubscriptionConsumer = $this->createSubscriptionConsumerMock();
176+
$fallbackSubscriptionConsumer
177+
->expects($this->never())
178+
->method('consume')
179+
;
180+
181+
$contextMock = $this->createMock(PsrContext::class);
182+
$contextMock
183+
->expects($this->once())
184+
->method('createConsumer')
185+
->with($this->identicalTo($expectedQueue))
186+
->willReturn($this->createConsumerStub())
187+
;
188+
$contextMock
189+
->expects($this->once())
190+
->method('createSubscriptionConsumer')
191+
->willReturn($contextSubscriptionConsumer)
192+
;
193+
194+
$processorMock = $this->createProcessorMock();
195+
$processorMock
196+
->expects($this->never())
197+
->method('process')
198+
;
199+
200+
$queueConsumer = new QueueConsumer($contextMock, new BreakCycleExtension(1));
201+
$queueConsumer->setFallbackSubscriptionConsumer($fallbackSubscriptionConsumer);
202+
$queueConsumer->bind($expectedQueue, $processorMock);
203+
$queueConsumer->consume();
204+
}
205+
206+
public function testShouldUseFallbackSubscriptionConsumerIfNotSupported()
207+
{
208+
$expectedQueue = new NullQueue('theQueueName');
209+
210+
$contextSubscriptionConsumer = $this->createSubscriptionConsumerMock();
211+
$contextSubscriptionConsumer
212+
->expects($this->never())
213+
->method('consume')
214+
;
215+
216+
$fallbackSubscriptionConsumer = $this->createSubscriptionConsumerMock();
217+
$fallbackSubscriptionConsumer
218+
->expects($this->once())
219+
->method('consume')
220+
->willReturn(null)
221+
;
222+
223+
$contextMock = $this->createContextWithoutSubscriptionConsumerMock();
224+
$contextMock
225+
->expects($this->once())
226+
->method('createConsumer')
227+
->with($this->identicalTo($expectedQueue))
228+
->willReturn($this->createConsumerStub())
229+
;
230+
$contextMock
231+
->expects($this->once())
232+
->method('createSubscriptionConsumer')
233+
->willThrowException(SubscriptionConsumerNotSupportedException::providerDoestNotSupportIt())
234+
;
235+
236+
$processorMock = $this->createProcessorMock();
237+
$processorMock
238+
->expects($this->never())
239+
->method('process')
240+
;
241+
242+
$queueConsumer = new QueueConsumer($contextMock, new BreakCycleExtension(1));
243+
$queueConsumer->setFallbackSubscriptionConsumer($fallbackSubscriptionConsumer);
244+
$queueConsumer->bind($expectedQueue, $processorMock);
245+
$queueConsumer->consume();
246+
}
247+
163248
public function testShouldSubscribeToGivenQueueWithExpectedTimeout()
164249
{
165250
$expectedQueue = new NullQueue('theQueueName');
@@ -172,7 +257,7 @@ public function testShouldSubscribeToGivenQueueWithExpectedTimeout()
172257
->willReturn(null)
173258
;
174259

175-
$contextMock = $this->createMock(PsrContext::class);
260+
$contextMock = $this->createContextWithoutSubscriptionConsumerMock();
176261
$contextMock
177262
->expects($this->once())
178263
->method('createConsumer')
@@ -203,7 +288,7 @@ public function testShouldSubscribeToGivenQueueAndQuitAfterFifthIdleCycle()
203288
->willReturn(null)
204289
;
205290

206-
$contextMock = $this->createMock(PsrContext::class);
291+
$contextMock = $this->createContextWithoutSubscriptionConsumerMock();
207292
$contextMock
208293
->expects($this->once())
209294
->method('createConsumer')
@@ -1173,7 +1258,7 @@ public function testShouldCallProcessorAsMessageComeAlong()
11731258
'bar_queue' => $barConsumerStub,
11741259
];
11751260

1176-
$contextStub = $this->createMock(PsrContext::class);
1261+
$contextStub = $this->createContextWithoutSubscriptionConsumerMock();
11771262
$contextStub
11781263
->expects($this->any())
11791264
->method('createQueue')
@@ -1225,12 +1310,27 @@ public function testShouldCallProcessorAsMessageComeAlong()
12251310
$this->assertSame($fooConsumerStub, $actualContexts[2]->getPsrConsumer());
12261311
}
12271312

1313+
/**
1314+
* @return \PHPUnit_Framework_MockObject_MockObject
1315+
*/
1316+
private function createContextWithoutSubscriptionConsumerMock(): PsrContext
1317+
{
1318+
$contextMock = $this->createMock(PsrContext::class);
1319+
$contextMock
1320+
->expects($this->any())
1321+
->method('createSubscriptionConsumer')
1322+
->willThrowException(SubscriptionConsumerNotSupportedException::providerDoestNotSupportIt())
1323+
;
1324+
1325+
return $contextMock;
1326+
}
1327+
12281328
/**
12291329
* @return \PHPUnit_Framework_MockObject_MockObject|PsrContext
12301330
*/
1231-
protected function createPsrContextStub(PsrConsumer $consumer = null)
1331+
private function createPsrContextStub(PsrConsumer $consumer = null)
12321332
{
1233-
$context = $this->createMock(PsrContext::class);
1333+
$context = $this->createContextWithoutSubscriptionConsumerMock();
12341334
$context
12351335
->expects($this->any())
12361336
->method('createQueue')
@@ -1257,15 +1357,15 @@ protected function createPsrContextStub(PsrConsumer $consumer = null)
12571357
/**
12581358
* @return \PHPUnit_Framework_MockObject_MockObject|PsrProcessor
12591359
*/
1260-
protected function createProcessorMock()
1360+
private function createProcessorMock()
12611361
{
12621362
return $this->createMock(PsrProcessor::class);
12631363
}
12641364

12651365
/**
12661366
* @return \PHPUnit_Framework_MockObject_MockObject|PsrProcessor
12671367
*/
1268-
protected function createProcessorStub()
1368+
private function createProcessorStub()
12691369
{
12701370
$processorMock = $this->createProcessorMock();
12711371
$processorMock
@@ -1280,15 +1380,15 @@ protected function createProcessorStub()
12801380
/**
12811381
* @return \PHPUnit_Framework_MockObject_MockObject|PsrMessage
12821382
*/
1283-
protected function createMessageMock()
1383+
private function createMessageMock()
12841384
{
12851385
return $this->createMock(PsrMessage::class);
12861386
}
12871387

12881388
/**
12891389
* @return \PHPUnit_Framework_MockObject_MockObject|ExtensionInterface
12901390
*/
1291-
protected function createExtension()
1391+
private function createExtension()
12921392
{
12931393
return $this->createMock(ExtensionInterface::class);
12941394
}
@@ -1298,7 +1398,7 @@ protected function createExtension()
12981398
*
12991399
* @return \PHPUnit_Framework_MockObject_MockObject|PsrConsumer
13001400
*/
1301-
protected function createConsumerStub($queue = null)
1401+
private function createConsumerStub($queue = null)
13021402
{
13031403
if (is_string($queue)) {
13041404
$queue = new NullQueue($queue);

0 commit comments

Comments
 (0)