8000 refactoring and consume command tests · enumag/enqueue-dev@342127c · GitHub
[go: up one dir, main page]

Skip to content

Commit 342127c

Browse files
committed
refactoring and consume command tests
1 parent 954450a commit 342127c

File tree

6 files changed

+235
-21
lines changed

6 files changed

+235
-21
lines changed

pkg/enqueue/Consumption/Extension/CaptureExitStatusExtension.php renamed to pkg/enqueue/Consumption/Extension/ExitStatusExtension.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,20 @@
55
use Enqueue\Consumption\Context\End;
66
use Enqueue\Consumption\EndExtensionInterface;
77

8-
class CaptureExitStatusExtension implements EndExtensionInterface
8+
class ExitStatusExtension implements EndExtensionInterface
99
{
1010
/**
1111
* @var int
1212
*/
1313
private $exitStatus;
1414

15-
/**
16-
* @var bool
17-
*/
18-
private $isExitStatusCaptured = false;
19-
2015
public function onEnd(End $context): void
2116
{
2217
$this->exitStatus = $context->getExitStatus();
23-
$this->isExitStatusCaptured = true;
2418
}
2519

2620
public function getExitStatus(): ?int
2721
{
2822
return $this->exitStatus;
2923
}
30-
31-
public function isExitStatusCaptured(): bool
32-
{
33-
return $this->isExitStatusCaptured;
34-
}
3524
}

pkg/enqueue/Symfony/Client/ConsumeCommand.php

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

55
use Enqueue\Client\DriverInterface;
66
use Enqueue\Consumption\ChainExtension;
7+
use Enqueue\Consumption\Extension\ExitStatusExtension;
78
use Enqueue\Consumption\Extension\LoggerExtension;
89
use Enqueue\Consumption\ExtensionInterface;
910
use Enqueue\Consumption\QueueConsumerInterface;
@@ -143,9 +144,12 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
143144
$consumer->bind($queue, $processor);
144145
}
145146

146-
$consumer->consume($this->getRuntimeExtensions($input, $output));
147+
$runtimeExtensionChain = $this->getRuntimeExtensions($input, $output);
148+
$exitStatusExtension = new ExitStatusExtension();
147149

148-
return null;
150+
$consumer->consume(new ChainExtension([$runtimeExtensionChain, $exitStatusExtension]));
151+
152+
return $exitStatusExtension->getExitStatus();
149153
}
150154

151155
protected function getRuntimeExtensions(InputInterface $input, OutputInterface $output): ExtensionInterface

pkg/enqueue/Symfony/Consumption/ConsumeCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Enqueue\Symfony\Consumption;
44

55
use Enqueue\Consumption\ChainExtension;
6-
use Enqueue\Consumption\Extension\CaptureExitStatusExtension;
6+
use Enqueue\Consumption\Extension\ExitStatusExtension;
77
use Enqueue\Consumption\QueueConsumerInterface;
88
use Psr\Container\ContainerInterface;
99
use Psr\Container\NotFoundExceptionInterface;
@@ -76,12 +76,12 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
7676
array_unshift($extensions, $loggerExtension);
7777
}
7878

79-
$captureExitStatusExtension = new CaptureExitStatusExtension();
80-
array_unshift($extensions, $captureExitStatusExtension);
79+
$exitStatusExtension = new ExitStatusExtension();
80+
array_unshift($extensions, $exitStatusExtension);
8181

8282
$consumer->consume(new ChainExtension($extensions));
8383

84-
return $captureExitStatusExtension->getExitStatus();
84+
return $exitStatusExtension->getExitStatus();
8585
}
8686

8787
private function getQueueConsumer(string $name): QueueConsumerInterface

pkg/enqueue/Tests/Consumption/QueueConsumerTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Enqueue\Consumption\Context\ProcessorException;
1717
use Enqueue\Consumption\Context\Start;
1818
use Enqueue\Consumption\Exception\InvalidArgumentException;
19-
use Enqueue\Consumption\Extension\CaptureExitStatusExtension;
19+
use Enqueue\Consumption\Extension\ExitStatusExtension;
2020
use Enqueue\Consumption\ExtensionInterface;
2121
use Enqueue\Consumption\QueueConsumer;
2222
use Enqueue\Consumption\Result;
@@ -1445,13 +1445,12 @@ public function testCaptureExitStatus()
14451445
})
14461446
;
14471447

1448-
$exitExtension = new CaptureExitStatusExtension();
1448+
$exitExtension = new ExitStatusExtension();
14491449

14501450
$consumer = new QueueConsumer($this->createContextStub(), $stubExtension);
14511451
$consumer->consume(new ChainExtension([$exitExtension]));
14521452

14531453
$this->assertEquals($testExitCode, $exitExtension->getExitStatus());
1454-
$this->assertTrue($exitExtension->isExitStatusCaptured());
14551454
}
14561455

14571456
/**

pkg/enqueue/Tests/Symfony/Client/ConsumeCommandTest.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88
use Enqueue\Client\Route;
99
use Enqueue\Client\RouteCollection;
1010
use Enqueue\Consumption\ChainExtension;
11+
use Enqueue\Consumption\Context\Start;
12+
use Enqueue\Consumption\ExtensionInterface;
13+
use Enqueue\Consumption\QueueConsumer;
1114
use Enqueue\Consumption\QueueConsumerInterface;
1215
use Enqueue\Container\Container;
1316
use Enqueue\Null\NullQueue;
1417
use Enqueue\Symfony\Client\ConsumeCommand;
1518
use Enqueue\Test\ClassExtensionTrait;
19+
use Interop\Queue\Consumer;
20+
use Interop\Queue\Context as InteropContext;
21+
use Interop\Queue\Exception\SubscriptionConsumerNotSupportedException;
22+
use Interop\Queue\Queue;
1623
use PHPUnit\Framework\TestCase;
1724
use Psr\Container\ContainerInterface;
1825
use Symfony\Component\Console\Command\Command;
@@ -536,6 +543,49 @@ public function testShouldSkipQueueConsumptionAndUseCustomClientDestinationName(
536543
]);
537544
}
538545

546+
public function testShouldReturnExitStatusIfSet()
547+
{
548+
$testExitCode = 678;
549+
550+
$stubExtension = $this->createExtension();
551+
552+
$stubExtension
553+
->expects($this->once())
554+
->method('onStart')
555+
->with($this->isInstanceOf(Start::class))
556+
->willReturnCallback(function (Start $context) use ($testExitCode) {
557+
$context->interruptExecution($testExitCode);
558+
})
559+
;
560+
561+
$defaultQueue = new NullQueue('default');
562+
563+
$routeCollection = new RouteCollection([]);
564+
565+
$processor = $this->createDelegateProcessorMock();
566+
567+
$driver = $this->createDriverStub($routeCollection);
568+
$driver
569+
->expects($this->exactly(1))
570+
->method('createQueue')
571+
->with('default', true)
572+
->willReturn($defaultQueue)
573+
;
574+
575+
$consumer = new QueueConsumer($this->createContextStub(), $stubExtension);
576+
577+
$command = new ConsumeCommand(new Container([
578+
'enqueue.client.default.queue_consumer' => $consumer,
579+
'enqueue.client.default.driver' => $driver,
580+
'enqueue.client.default.delegate_processor' => $processor,
581+
]), 'default');
582+
583+
$tester = new CommandTester($command);
584+
$tester->execute([]);
585+
586+
$this->assertEquals($testExitCode, $tester->getStatusCode());
587+
}
588+
539589
/**
540590
* @return \PHPUnit_Framework_MockObject_MockObject|DelegateProcessor
541591
*/
@@ -572,4 +622,72 @@ private function createDriverStub(RouteCollection $routeCollection = null): Driv
572622

573623
return $driverMock;
574624
}
625+
626+
/**
627+
* @return \PHPUnit_Framework_MockObject_MockObject
628+
*/
629+
private function createContextWithoutSubscriptionConsumerMock(): InteropContext
630+
{
631+
$contextMock = $this->createMock(InteropContext::class);
632+
$contextMock
633+
->expects($this->any())
634+
->method('createSubscriptionConsumer')
635+
->willThrowException(SubscriptionConsumerNotSupportedException::providerDoestNotSupportIt())
636+
;
637+
638+
return $contextMock;
639+
}
640+
641+
/**
642+
* @return \PHPUnit_Framework_MockObject_MockObject|InteropContext
643+
*/
644+
private function createContextStub(Consumer $consumer = null): InteropContext
645+
{
646+
$context = $this->createContextWithoutSubscriptionConsumerMock();
647+
$context
648+
->expects($this->any())
649+
->method('createQueue')
650+
->willReturnCallback(function (string $queueName) {
651+
return new NullQueue($queueName);
652+
})
653+
;
654+
$context
655+
->expects($this->any())
656+
->method('createConsumer')
657+
->willReturnCallback(function (Queue $queue) use ($consumer) {
658+
return $consumer ?: $this->createConsumerStub($queue);
659+
})
660+
;
661+
662+
return $context;
663+
}
664+
665+
/**
666+
* @return \PHPUnit_Framework_MockObject_MockObject|ExtensionInterface
667+
*/
668+
private function createExtension()
669+
{
670+
return $this->createMock(ExtensionInterface::class);
671+
}
672+
673+
/**
674+
* @param null|mixed $queue
675+
*
676+
* @return \PHPUnit_Framework_MockObject_MockObject|Consumer
677+
*/
678+
private function createConsumerStub($queue = null): Consumer
679+
{
680+
if (is_string($queue)) {
681+
$queue = new NullQueue($queue);
682+
}
683+
684+
$consumerMock = $this->createMock(Consumer::class);
685+
$consumerMock
686+
->expects($this->any())
687+
->method('getQueue')
688+
->willReturn($queue)
689+
;
690+
691+
return $consumerMock;
692+
}
575693
}

pkg/enqueue/Tests/Symfony/Consumption/ConsumeCommandTest.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
namespace Enqueue\Tests\Symfony\Consumption;
44

55
use Enqueue\Consumption\ChainExtension;
6+
use Enqueue\Consumption\Context\Start;
7+
use Enqueue\Consumption\ExtensionInterface;
8+
use Enqueue\Consumption\QueueConsumer;
69
use Enqueue\Consumption\QueueConsumerInterface;
710
use Enqueue\Container\Container;
11+
use Enqueue\Null\NullQueue;
812
use Enqueue\Symfony\Consumption\ConsumeCommand;
913
use Enqueue\Test\ClassExtensionTrait;
14+
use Interop\Queue\Consumer;
15+
use Interop\Queue\Context as InteropContext;
16+
use Interop\Queue\Exception\SubscriptionConsumerNotSupportedException;
17+
use Interop\Queue\Queue;
1018
use PHPUnit\Framework\TestCase;
1119
use Psr\Container\ContainerInterface;
1220
use Symfony\Component\Console\Command\Command;
@@ -123,11 +131,107 @@ public function testThrowIfNotDefinedTransportRequested()
123131
$tester->execute(['--transport' => 'not-defined']);
124132
}
125133

134+
public function testShouldReturnExitStatusIfSet()
135+
{
136+
$testExitCode = 678;
137+
138+
$stubExtension = $this->createExtension();
139+
140+
$stubExtension
141+
->expects($this->once())
142+
->method('onStart')
143+
->with($this->isInstanceOf(Start::class))
144+
->willReturnCallback(function (Start $context) use ($testExitCode) {
145+
$context->interruptExecution($testExitCode);
146+
})
147+
;
148+
149+
$consumer = new QueueConsumer($this->createContextStub(), $stubExtension);
150+
151+
$command = new ConsumeCommand(new Container([
152+
'enqueue.transport.default.queue_consumer' => $consumer,
153+
]), 'default');
154+
155+
$tester = new CommandTester($command);
156+
157+
$tester->execute([]);
158+
159+
$this->assertEquals($testExitCode, $tester->getStatusCode());
160+
}
161+
126162
/**
127163
* @return \PHPUnit_Framework_MockObject_MockObject|QueueConsumerInterface
128164
*/
129165
private function createQueueConsumerMock()
130166
{
131167
return $this->createMock(QueueConsumerInterface::class);
132168
}
169+
170+
/**
171+
* @return \PHPUnit_Framework_MockObject_MockObject
172+
*/
173+
private function createContextWithoutSubscriptionConsumerMock(): InteropContext
174+
{
175+
$contextMock = $this->createMock(InteropContext::class);
176+
$contextMock
177+
->expects($this->any())
178+
->method('createSubscriptionConsumer')
179+
->willThrowException(SubscriptionConsumerNotSupportedException::providerDoestNotSupportIt())
180+
;
181+
182+
return $contextMock;
183+
}
184+
185+ 10000
/**
186+
* @return \PHPUnit_Framework_MockObject_MockObject|InteropContext
187+
*/
188+
private function createContextStub(Consumer $consumer = null): InteropContext
189+
{
190+
$context = $this->createContextWithoutSubscriptionConsumerMock();
191+
$context
192+
->expects($this->any())
193+
->method('createQueue')
194+
->willReturnCallback(function (string $queueName) {
195+
return new NullQueue($queueName);
196+
})
197+
;
198+
$context
199+
->expects($this->any())
200+
->method('createConsumer')
201+
->willReturnCallback(function (Queue $queue) use ($consumer) {
202+
return $consumer ?: $this->createConsumerStub($queue);
203+
})
204+
;
205+
206+
return $context;
207+
}
208+
209+
/**
210+
* @return \PHPUnit_Framework_MockObject_MockObject|ExtensionInterface
211+
*/
212+
private function createExtension()
213+
{
214+
return $this->createMock(ExtensionInterface::class);
215+
}
216+
217+
/**
218+
* @param null|mixed $queue
219+
*
220+
* @return \PHPUnit_Framework_MockObject_MockObject|Consumer
221+
*/
222+
private function createConsumerStub($queue = null): Consumer
223+
{
224+
if (is_string($queue)) {
225+
$queue = new NullQueue($queue);
226+
}
227+
228+
$consumerMock = $this->createMock(Consumer::class);
229+
$consumerMock
230+
->expects($this->any())
231+
->method('getQueue')
232+
->willReturn($queue)
233+
;
234+
235+
return $consumerMock;
236+
}
133237
}

0 commit comments

Comments
 (0)
0