|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Gps\Tests; |
| 4 | + |
| 5 | +use Enqueue\Gps\GpsConsumer; |
| 6 | +use Enqueue\Gps\GpsContext; |
| 7 | +use Enqueue\Gps\GpsMessage; |
| 8 | +use Enqueue\Gps\GpsQueue; |
| 9 | +use Google\Cloud\PubSub\Message; |
| 10 | +use Google\Cloud\PubSub\PubSubClient; |
| 11 | +use Google\Cloud\PubSub\Subscription; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +class GpsConsumerTest extends TestCase |
| 15 | +{ |
| 16 | + public function testAcknowledgeShouldThrowExceptionIfNativeMessageNotSet() |
| 17 | + { |
| 18 | + $consumer = new GpsConsumer($this->createContextMock(), new GpsQueue('')); |
| 19 | + |
| 20 | + $this->expectException(\LogicException::class); |
| 21 | + $this->expectExceptionMessage('Native google pub/sub message required but it is empty'); |
| 22 | + |
| 23 | + $consumer->acknowledge(new GpsMessage('')); |
| 24 | + } |
| 25 | + |
| 26 | + public function testShouldAcknowledgeMessage() |
| 27 | + { |
| 28 | + $nativeMessage = new Message([], []); |
| 29 | + |
| 30 | + $subscription = $this->createSubscriptionMock(); |
| 31 | + $subscription |
| 32 | + ->expects($this->once()) |
| 33 | + ->method('acknowledge') |
| 34 | + ->with($this->identicalTo($nativeMessage)) |
| 35 | + ; |
| 36 | + |
| 37 | + $client = $this->createPubSubClientMock(); |
| 38 | + $client |
| 39 | + ->expects($this->once()) |
| 40 | + ->method('subscription') |
| 41 | + ->willReturn($subscription) |
| 42 | + ; |
| 43 | + |
| 44 | + $context = $this->createContextMock(); |
| 45 | + $context |
| 46 | + ->expects($this->once()) |
| 47 | + ->method('getClient') |
| 48 | + ->willReturn($client) |
| 49 | + ; |
| 50 | + |
| 51 | + $consumer = new GpsConsumer($context, new GpsQueue('queue-name')); |
| 52 | + |
| 53 | + $message = new GpsMessage(''); |
| 54 | + $message->setNativeMessage($nativeMessage); |
| 55 | + |
| 56 | + $consumer->acknowledge($message); |
| 57 | + } |
| 58 | + |
| 59 | + public function testRejectShouldThrowExceptionIfNativeMessageNotSet() |
| 60 | + { |
| 61 | + $consumer = new GpsConsumer($this->createContextMock(), new GpsQueue('')); |
| 62 | + |
| 63 | + $this->expectException(\LogicException::class); |
| 64 | + $this->expectExceptionMessage('Native google pub/sub message required but it is empty'); |
| 65 | + |
| 66 | + $consumer->acknowledge(new GpsMessage('')); |
| 67 | + } |
| 68 | + |
| 69 | + public function testShouldRejectMessage() |
| 70 | + { |
| 71 | + $nativeMessage = new Message([], []); |
| 72 | + |
| 73 | + $subscription = $this->createSubscriptionMock(); |
| 74 | + $subscription |
| 75 | + ->expects($this->once()) |
| 76 | + ->method('acknowledge') |
| 77 | + ->with($this->identicalTo($nativeMessage)) |
| 78 | + ; |
| 79 | + |
| 80 | + $client = $this->createPubSubClientMock(); |
| 81 | + $client |
| 82 | + ->expects($this->once()) |
| 83 | + ->method('subscription') |
| 84 | + ->willReturn($subscription) |
| 85 | + ; |
| 86 | + |
| 87 | + $context = $this->createContextMock(); |
| 88 | + $context |
| 89 | + ->expects($this->once()) |
| 90 | + ->method('getClient') |
| 91 | + ->willReturn($client) |
| 92 | + ; |
| 93 | + |
| 94 | + $consumer = new GpsConsumer($context, new GpsQueue('queue-name')); |
| 95 | + |
| 96 | + $message = new GpsMessage(''); |
| 97 | + $message->setNativeMessage($nativeMessage); |
| 98 | + |
| 99 | + $consumer->reject($message); |
| 100 | + } |
| 101 | + |
| 102 | + public function testShouldReceiveMessageNoWait() |
| 103 | + { |
| 104 | + $message = new GpsMessage('the body'); |
| 105 | + $nativeMessage = new Message([ |
| 106 | + 'data' => json_encode($message), |
| 107 | + ], []); |
| 108 | + |
| 109 | + $subscription = $this->createSubscriptionMock(); |
| 110 | + $subscription |
| 111 | + ->expects($this->once()) |
| 112 | + ->method('pull') |
| 113 | + ->with($this->identicalTo([ |
| 114 | + 'maxMessages' => 1, |
| 115 | + 'returnImmediately' => true, |
| 116 | + ])) |
| 117 | + ->willReturn([$nativeMessage]) |
| 118 | + ; |
| 119 | + |
| 120 | + $client = $this->createPubSubClientMock(); |
| 121 | + $client |
| 122 | + ->expects($this->once()) |
| 123 | + ->method('subscription') |
| 124 | + ->willReturn($subscription) |
| 125 | + ; |
| 126 | + |
| 127 | + $context = $this->createContextMock(); |
| 128 | + $context |
| 129 | + ->expects($this->once()) |
| 130 | + ->method('getClient') |
| 131 | + ->willReturn($client) |
| 132 | + ; |
| 133 | + |
| 134 | + $consumer = new GpsConsumer($context, new GpsQueue('queue-name')); |
| 135 | + |
| 136 | + $message = $consumer->receiveNoWait(); |
| 137 | + |
| 138 | + $this->assertInstanceOf(GpsMessage::class, $message); |
| 139 | + $this->assertSame('the body', $message->getBody()); |
| 140 | + } |
| 141 | + |
| 142 | + public function testShouldReceiveMessage() |
| 143 | + { |
| 144 | + $message = new GpsMessage('the body'); |
| 145 | + $nativeMessage = new Message([ |
| 146 | + 'data' => json_encode($message), |
| 147 | + ], []); |
| 148 | + |
| 149 | + $subscription = $this->createSubscriptionMock(); |
| 150 | + $subscription |
| 151 | + ->expects($this->once()) |
| 152 | + ->method('pull') |
| 153 | + ->with($this->identicalTo([ |
| 154 | + 'maxMessages' => 1, |
| 155 | + 'requestTimeout' => 12.345, |
| 156 | + ])) |
| 157 | + ->willReturn([$nativeMessage]) |
| 158 | + ; |
| 159 | + |
| 160 | + $client = $this->createPubSubClientMock(); |
| 161 | + $client |
| 162 | + ->expects($this->once()) |
| 163 | + ->method('subscription') |
| 164 | + ->willReturn($subscription) |
| 165 | + ; |
| 166 | + |
| 167 | + $context = $this->createContextMock(); |
| 168 | + $context |
| 169 | + ->expects($this->once()) |
| 170 | + ->method('getClient') |
| 171 | + ->willReturn($client) |
| 172 | + ; |
| 173 | + |
| 174 | + $consumer = new GpsConsumer($context, new GpsQueue('queue-name')); |
| 175 | + |
| 176 | + $message = $consumer->receive(12345); |
| 177 | + |
| 178 | + $this->assertInstanceOf(GpsMessage::class, $message); |
| 179 | + $this->assertSame('the body', $message->getBody()); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * @return \PHPUnit_Framework_MockObject_MockObject|GpsContext |
| 184 | + */ |
| 185 | + private function createContextMock() |
| 186 | + { |
| 187 | + return $this->createMock(GpsContext::class); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * @return \PHPUnit_Framework_MockObject_MockObject|PubSubClient |
| 192 | + */ |
| 193 | + private function createPubSubClientMock() |
| 194 | + { |
| 195 | + return $this->createMock(PubSubClient::class); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * @return \PHPUnit_Framework_MockObject_MockObject|Subscription |
| 200 | + */ |
| 201 | + private function createSubscriptionMock() |
| 202 | + { |
| 203 | + return $this->createMock(Subscription::class); |
| 204 | + } |
| 205 | +} |
0 commit comments