Skip to content

Commit d3921fb

Browse files
committed
gps
1 parent f3b2c56 commit d3921fb

12 files changed

Lines changed: 556 additions & 2 deletions

‎pkg/gps/GpsConnectionFactory.php‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,27 @@ class GpsConnectionFactory implements PsrConnectionFactory
2121
* 'keyFilePath' => The full path to your service account credentials.json file retrieved from the Google Developers Console.
2222
* 'retries' => Number of retries for a failed request. **Defaults to** `3`.
2323
* 'scopes' => Scopes to be used for the request.
24+
* 'lazy' => 'the connection will be performed as later as possible, if the option set to true'
2425
* ]
2526
*
26-
* @param array $config
27+
* or
28+
*
29+
* gps:
30+
* gps:?projectId=projectName
31+
*
32+
* @param array|string|null $config
2733
*/
28-
public function __construct(array $config = [])
34+
public function __construct($config = 'gps:')
2935
{
36+
if (empty($config) || 'gps:' === $config) {
37+
$config = [];
38+
} elseif (is_string($config)) {
39+
$config = $this->parseDsn($config);
40+
} elseif (is_array($config)) {
41+
} else {
42+
throw new \LogicException('The config must be either an array of options, a DSN string or null');
43+
}
44+
3045
$this->config = array_replace($this->defaultConfig(), $config);
3146
}
3247

@@ -46,6 +61,26 @@ public function createContext()
4661
return new GpsContext($this->establishConnection());
4762
}
4863

64+
/**
65+
* @param string $dsn
66+
*
67+
* @return array
68+
*/
69+
private function parseDsn($dsn)
70+
{
71+
if (false === strpos($dsn, 'gps:')) {
72+
throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "gps:".', $dsn));
73+
}
74+
75+
$config = [];
76+
77+
if ($query = parse_url($dsn, PHP_URL_QUERY)) {
78+
parse_str($query, $config);
79+
}
80+
81+
return $config;
82+
}
83+
4984
/**
5085
* @return PubSubClient
5186
*/

‎pkg/gps/GpsContext.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ public function createProducer()
8989
return new GpsProducer($this);
9090
}
9191

92+
/**
93+
* {@inheritdoc}
94+
*/
9295
public function createConsumer(PsrDestination $destination)
9396
{
9497
InvalidDestinationException::assertDestinationInstanceOf($destination, GpsQueue::class);

‎pkg/gps/Tests/GpsConsumerTest.php‎

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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+
}

‎pkg/gps/Tests/GpsContextTest.php‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace Enqueue\Gps\Tests;
4+
5+
use Enqueue\Gps\GpsContext;
6+
use Enqueue\Gps\GpsQueue;
7+
use Enqueue\Gps\GpsTopic;
8+
use Google\Cloud\Core\Exception\ConflictException;
9+
use Google\Cloud\PubSub\PubSubClient;
10+
use Interop\Queue\InvalidDestinationException;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class GpsContextTest extends TestCase
14+
{
15+
public function testShouldDeclareTopic()
16+
{
17+
$client = $this->createPubSubClientMock();
18+
$client
19+
->expects($this->once())
20+
->method('createTopic')
21+
->with('topic-name')
22+
;
23+
24+
$topic = new GpsTopic('topic-name');
25+
26+
$context = new GpsContext($client);
27+
$context->declareTopic($topic);
28+
}
29+
30+
public function testDeclareTopicShouldCatchConflictException()
31+
{
32+
$client = $this->createPubSubClientMock();
33+
$client
34+
->expects($this->once())
35+
->method('createTopic')
36+
->willThrowException(new ConflictException(''))
37+
;
38+
39+
$topic = new GpsTopic('');
40+
41+
$context = new GpsContext($client);
42+
$context->declareTopic($topic);
43+
}
44+
45+
public function testShouldSubscribeTopicToQueue()
46+
{
47+
$client = $this->createPubSubClientMock();
48+
$client
49+
->expects($this->once())
50+
->method('subscribe')
51+
->with('queue-name', 'topic-name', $this->identicalTo(['ackDeadlineSeconds' => 10]))
52+
;
53+
54+
$topic = new GpsTopic('topic-name');
55+
$queue = new GpsQueue('queue-name');
56+
57+
$context = new GpsContext($client);
58+
59+
$context->subscribe($topic, $queue);
60+
}
61+
62+
public function testSubscribeShouldCatchConflictException()
63+
{
64+
$client = $this->createPubSubClientMock();
65+
$client
66+
->expects($this->once())
67+
->method('subscribe')
68+
->willThrowException(new ConflictException(''))
69+
;
70+
71+
$topic = new GpsTopic('topic-name');
72+
$queue = new GpsQueue('queue-name');
73+
74+
$context = new GpsContext($client);
75+
76+
$context->subscribe($topic, $queue);
77+
}
78+
79+
public function testCreateConsumerShouldThrowExceptionIfInvalidDestination()
80+
{
81+
$context = new GpsContext($this->createPubSubClientMock());
82+
83+
$this->expectException(InvalidDestinationException::class);
84+
$this->expectExceptionMessage('The destination must be an instance of Enqueue\Gps\GpsQueue but got Enqueue\Gps\GpsTopic');
85+
86+
$context->createConsumer(new GpsTopic(''));
87+
}
88+
89+
/**
90+
* @return PubSubClient|\PHPUnit_Framework_MockObject_MockObject|PubSubClient
91+
*/
92+
private function createPubSubClientMock()
93+
{
94+
return $this->createMock(PubSubClient::class);
95+
}
96+
}

0 commit comments

Comments
 (0)