|
8 | 8 | use Enqueue\Client\Route; |
9 | 9 | use Enqueue\Client\RouteCollection; |
10 | 10 | use Enqueue\Consumption\ChainExtension; |
| 11 | +use Enqueue\Consumption\Context\Start; |
| 12 | +use Enqueue\Consumption\ExtensionInterface; |
| 13 | +use Enqueue\Consumption\QueueConsumer; |
11 | 14 | use Enqueue\Consumption\QueueConsumerInterface; |
12 | 15 | use Enqueue\Container\Container; |
13 | 16 | use Enqueue\Null\NullQueue; |
14 | 17 | use Enqueue\Symfony\Client\ConsumeCommand; |
15 | 18 | 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; |
16 | 23 | use PHPUnit\Framework\TestCase; |
17 | 24 | use Psr\Container\ContainerInterface; |
18 | 25 | use Symfony\Component\Console\Command\Command; |
@@ -536,6 +543,49 @@ public function testShouldSkipQueueConsumptionAndUseCustomClientDestinationName( |
536 | 543 | ]); |
537 | 544 | } |
538 | 545 |
|
| 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 | + |
539 | 589 | /** |
540 | 590 | * @return \PHPUnit_Framework_MockObject_MockObject|DelegateProcessor |
541 | 591 | */ |
@@ -572,4 +622,72 @@ private function createDriverStub(RouteCollection $routeCollection = null): Driv |
572 | 622 |
|
573 | 623 | return $driverMock; |
574 | 624 | } |
| 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 | + } |
575 | 693 | } |
0 commit comments