|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Interop\Queue\Spec; |
| 4 | + |
| 5 | +use Interop\Queue\PsrConsumer; |
| 6 | +use Interop\Queue\PsrContext; |
| 7 | +use Interop\Queue\PsrMessage; |
| 8 | +use Interop\Queue\PsrQueue; |
| 9 | +use Interop\Queue\PsrSubscriptionConsumerAwareContext; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +/** |
| 13 | + * @group functional |
| 14 | + */ |
| 15 | +abstract class SubscriptionConsumerConsumeFromAllSubscribedQueuesSpec extends TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var PsrContext |
| 19 | + */ |
| 20 | + private $context; |
| 21 | + |
| 22 | + protected function tearDown() |
| 23 | + { |
| 24 | + if ($this->context) { |
| 25 | + $this->context->close(); |
| 26 | + } |
| 27 | + |
| 28 | + parent::tearDown(); |
| 29 | + } |
| 30 | + |
| 31 | + public function test() |
| 32 | + { |
| 33 | + $this->context = $context = $this->createContext(); |
| 34 | + |
| 35 | + $fooQueue = $this->createQueue($context, 'foo_subscription_consumer_consume_from_all_subscribed_queues_spec'); |
| 36 | + $barQueue = $this->createQueue($context, 'bar_subscription_consumer_consume_from_all_subscribed_queues_spec'); |
| 37 | + |
| 38 | + $expectedFooBody = 'fooBody'; |
| 39 | + $expectedBarBody = 'barBody'; |
| 40 | + |
| 41 | + $context->createProducer()->send($fooQueue, $context->createMessage($expectedFooBody)); |
| 42 | + $context->createProducer()->send($barQueue, $context->createMessage($expectedBarBody)); |
| 43 | + |
| 44 | + $fooConsumer = $context->createConsumer($fooQueue); |
| 45 | + $barConsumer = $context->createConsumer($barQueue); |
| 46 | + |
| 47 | + $actualBodies = []; |
| 48 | + $actualQueues = []; |
| 49 | + $callback = function(PsrMessage $message, PsrConsumer $consumer) use (&$actualBodies, &$actualQueues) { |
| 50 | + $actualBodies[] = $message->getBody(); |
| 51 | + $actualQueues[] = $consumer->getQueue()->getQueueName(); |
| 52 | + |
| 53 | + $consumer->acknowledge($message); |
| 54 | + |
| 55 | + return true; |
| 56 | + }; |
| 57 | + |
| 58 | + $subscriptionConsumer = $context->createSubscriptionConsumer(); |
| 59 | + $subscriptionConsumer->subscribe($fooConsumer, $callback); |
| 60 | + $subscriptionConsumer->subscribe($barConsumer, $callback); |
| 61 | + |
| 62 | + $subscriptionConsumer->consume(1000); |
| 63 | + |
| 64 | + $this->assertEquals([$expectedFooBody, $expectedBarBody], $actualBodies); |
| 65 | + $this->assertEquals( |
| 66 | + [ |
| 67 | + 'foo_subscription_consumer_consume_from_all_subscribed_queues_spec', |
| 68 | + 'bar_subscription_consumer_consume_from_all_subscribed_queues_spec' |
| 69 | + ], |
| 70 | + $actualQueues |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @return PsrContext|PsrSubscriptionConsumerAwareContext |
| 76 | + */ |
| 77 | + abstract protected function createContext(); |
| 78 | + |
| 79 | + /** |
| 80 | + * @param PsrContext $context |
| 81 | + * @param string $queueName |
| 82 | + * |
| 83 | + * @return PsrQueue |
| 84 | + */ |
| 85 | + protected function createQueue(PsrContext $context, $queueName) |
| 86 | + { |
| 87 | + return $context->createQueue($queueName); |
| 88 | + } |
| 89 | +} |
0 commit comments