|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\AmqpTools\Tests; |
| 4 | + |
| 5 | +use Enqueue\AmqpTools\SubscriptionConsumer; |
| 6 | +use Interop\Amqp\AmqpConsumer; |
| 7 | +use Interop\Amqp\AmqpContext; |
| 8 | +use Interop\Queue\PsrSubscriptionConsumer; |
| 9 | + |
| 10 | +class SubscriptionConsumerTest extends \PHPUnit_Framework_TestCase |
| 11 | +{ |
| 12 | + public function testShouldImplementPsrSubscriptionConsumerInterface() |
| 13 | + { |
| 14 | + $rc = new \ReflectionClass(SubscriptionConsumer::class); |
| 15 | + |
| 16 | + $this->assertTrue($rc->implementsInterface(PsrSubscriptionConsumer::class)); |
| 17 | + } |
| 18 | + |
| 19 | + public function testCouldBeConstructedWithAmqpContextAsFirstArgument() |
| 20 | + { |
| 21 | + new SubscriptionConsumer($this->createContext()); |
| 22 | + } |
| 23 | + |
| 24 | + public function testShouldProxySubscribeCallToContextMethod() |
| 25 | + { |
| 26 | + $consumer = $this->createConsumer(); |
| 27 | + $callback = function () {}; |
| 28 | + |
| 29 | + $context = $this->createContext(); |
| 30 | + $context |
| 31 | + ->expects($this->once()) |
| 32 | + ->method('subscribe') |
| 33 | + ->with($this->identicalTo($consumer), $this->identicalTo($callback)) |
| 34 | + ; |
| 35 | + |
| 36 | + $subscriptionConsumer = new SubscriptionConsumer($context); |
| 37 | + $subscriptionConsumer->subscribe($consumer, $callback); |
| 38 | + } |
| 39 | + |
| 40 | + public function testShouldProxyUnsubscribeCallToContextMethod() |
| 41 | + { |
| 42 | + $consumer = $this->createConsumer(); |
| 43 | + |
| 44 | + $context = $this->createContext(); |
| 45 | + $context |
| 46 | + ->expects($this->once()) |
| 47 | + ->method('unsubscribe') |
| 48 | + ->with($this->identicalTo($consumer)) |
| 49 | + ; |
| 50 | + |
| 51 | + $subscriptionConsumer = new SubscriptionConsumer($context); |
| 52 | + $subscriptionConsumer->unsubscribe($consumer); |
| 53 | + } |
| 54 | + |
| 55 | + public function testShouldProxyConsumeCallToContextMethod() |
| 56 | + { |
| 57 | + $timeout = 123.456; |
| 58 | + |
| 59 | + $context = $this->createContext(); |
| 60 | + $context |
| 61 | + ->expects($this->once()) |
| 62 | + ->method('consume') |
| 63 | + ->with($this->identicalTo($timeout)) |
| 64 | + ; |
| 65 | + |
| 66 | + $subscriptionConsumer = new SubscriptionConsumer($context); |
| 67 | + $subscriptionConsumer->consume($timeout); |
| 68 | + } |
| 69 | + |
| 70 | + public function testThrowsNotImplementedOnUnsubscribeAllCall() |
| 71 | + { |
| 72 | + $context = $this->createContext(); |
| 73 | + |
| 74 | + $subscriptionConsumer = new SubscriptionConsumer($context); |
| 75 | + |
| 76 | + $this->expectException(\LogicException::class); |
| 77 | + $this->expectExceptionMessage('Not implemented'); |
| 78 | + $subscriptionConsumer->unsubscribeAll(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return AmqpConsumer|\PHPUnit_Framework_MockObject_MockObject |
| 83 | + */ |
| 84 | + private function createConsumer() |
| 85 | + { |
| 86 | + return $this->createMock(AmqpConsumer::class); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return AmqpContext|\PHPUnit_Framework_MockObject_MockObject |
| 91 | + */ |
| 92 | + private function createContext() |
| 93 | + { |
| 94 | + return $this->createMock(AmqpContext::class); |
| 95 | + } |
| 96 | +} |
0 commit comments