Skip to content

Commit 3f8d951

Browse files
committed
add deprecated getMessage method
1 parent 3d0250c commit 3f8d951

5 files changed

Lines changed: 84 additions & 56 deletions

File tree

‎pkg/enqueue/Client/RpcClient.php‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ public function callAsync($topic, $message, $timeout)
7878

7979
$correlationId = $message->getCorrelationId();
8080

81-
$receive = function() use ($replyQueue, $timeout, $correlationId) {
82-
81+
$receive = function () use ($replyQueue, $timeout, $correlationId) {
8382
$endTime = time() + ((int) ($timeout / 1000));
8483
$consumer = $this->context->createConsumer($replyQueue);
8584

@@ -98,8 +97,7 @@ public function callAsync($topic, $message, $timeout)
9897
throw TimeoutException::create($timeout, $correlationId);
9998
};
10099

101-
$receiveNoWait = function() use ($replyQueue, $correlationId) {
102-
100+
$receiveNoWait = function () use ($replyQueue, $correlationId) {
103101
static $consumer;
104102

105103
if (null === $consumer) {
@@ -117,7 +115,7 @@ public function callAsync($topic, $message, $timeout)
117115
}
118116
};
119117

120-
$finally = function(Promise $promise) use ($replyQueue) {
118+
$finally = function (Promise $promise) use ($replyQueue) {
121119
if ($promise->isDeleteReplyQueue()) {
122120
if (false == method_exists($this->context, 'deleteQueue')) {
123121
throw new \RuntimeException(sprintf('Context does not support delete queues: "%s"', get_class($this->context)));

‎pkg/enqueue/Rpc/Promise.php‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ public function __construct(\Closure $receiveCallback, \Closure $receiveNoWaitCa
4545
$this->deleteReplyQueue = true;
4646
}
4747

48+
/**
49+
* Blocks until message received or timeout expired.
50+
*
51+
* @deprecated use "receive" instead
52+
*
53+
* @throws TimeoutException if the wait timeout is reached
54+
*
55+
* @return PsrMessage
56+
*/
57+
public function getMessage()
58+
{
59+
return $this->receive();
60+
}
61+
4862
/**
4963
* Blocks until message received or timeout expired.
5064
*
@@ -86,24 +100,7 @@ public function receiveNoWait()
86100
}
87101

88102
/**
89-
* @param \Closure $cb
90-
*
91-
* @return PsrMessage
92-
*/
93-
private function doReceive(\Closure $cb)
94-
{
95-
$message = call_user_func($cb, $this);
96-
97-
if (null !== $message && false == $message instanceof PsrMessage) {
98-
throw new \RuntimeException(sprintf(
99-
'Expected "%s" but got: "%s"', PsrMessage::class, is_object($message) ? get_class($message) : gettype($message)));
100-
}
101-
102-
return $message;
103-
}
104-
105-
/**
106-
* On TRUE deletes reply queue after getMessage call
103+
* On TRUE deletes reply queue after getMessage call.
107104
*
108105
* @param bool $delete
109106
*/
@@ -119,4 +116,21 @@ public function isDeleteReplyQueue()
119116
{
120117
return $this->deleteReplyQueue;
121118
}
119+
120+
/**
121+
* @param \Closure $cb
122+
*
123+
* @return PsrMessage
124+
*/
125+
private function doReceive(\Closure $cb)
126+
{
127+
$message = call_user_func($cb, $this);
128+
129+
if (null !== $message && false == $message instanceof PsrMessage) {
130+
throw new \RuntimeException(sprintf(
131+
'Expected "%s" but got: "%s"', PsrMessage::class, is_object($message) ? get_class($message) : gettype($message)));
132+
}
133+
134+
return $message;
135+
}
122136
}

‎pkg/enqueue/Rpc/RpcClient.php‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public function callAsync(PsrDestination $destination, PsrMessage $message, $tim
6666

6767
$correlationId = $message->getCorrelationId();
6868

69-
$receive = function() use ($replyQueue, $timeout, $correlationId) {
70-
69+
$receive = function () use ($replyQueue, $timeout, $correlationId) {
7170
$endTime = time() + ((int) ($timeout / 1000));
7271
$consumer = $this->context->createConsumer($replyQueue);
7372

@@ -86,8 +85,7 @@ public function callAsync(PsrDestination $destination, PsrMessage $message, $tim
8685
throw TimeoutException::create($timeout, $correlationId);
8786
};
8887

89-
$receiveNoWait = function() use ($replyQueue, $correlationId) {
90-
88+
$receiveNoWait = function () use ($replyQueue, $correlationId) {
9189
static $consumer;
9290

9391
if (null === $consumer) {
@@ -105,7 +103,7 @@ public function callAsync(PsrDestination $destination, PsrMessage $message, $tim
105103
}
106104
};
107105

108-
$finally = function(Promise $promise) use ($replyQueue) {
106+
$finally = function (Promise $promise) use ($replyQueue) {
109107
if ($promise->isDeleteReplyQueue()) {
110108
if (false == method_exists($this->context, 'deleteQueue')) {
111109
throw new \RuntimeException(sprintf('Context does not support delete queue: "%s"', get_class($this->context)));

‎pkg/enqueue/Tests/Client/RpcClientTest.php‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ public function testShouldNotSetCorrelationIdIfSet()
134134
$rpc->callAsync('aTopic', $message, 2);
135135
}
136136

137-
138137
public function testShouldDoSyncCall()
139138
{
140139
$timeout = 123;

‎pkg/enqueue/Tests/Rpc/PromiseTest.php‎

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ class PromiseTest extends TestCase
1010
{
1111
public function testIsDeleteReplyQueueShouldReturnTrueByDefault()
1212
{
13-
$promise = new Promise(function(){}, function(){}, function(){});
13+
$promise = new Promise(function () {}, function () {}, function () {});
1414

1515
$this->assertTrue($promise->isDeleteReplyQueue());
1616
}
1717

1818
public function testCouldSetGetDeleteReplyQueue()
1919
{
20-
$promise = new Promise(function(){}, function(){}, function(){});
20+
$promise = new Promise(function () {}, function () {}, function () {});
2121

2222
$promise->setDeleteReplyQueue(false);
2323
$this->assertFalse($promise->isDeleteReplyQueue());
@@ -29,11 +29,11 @@ public function testCouldSetGetDeleteReplyQueue()
2929
public function testOnReceiveShouldCallReceiveCallBack()
3030
{
3131
$receiveInvoked = false;
32-
$receivecb = function() use (&$receiveInvoked) {
32+
$receivecb = function () use (&$receiveInvoked) {
3333
$receiveInvoked = true;
3434
};
3535

36-
$promise = new Promise($receivecb, function(){}, function(){});
36+
$promise = new Promise($receivecb, function () {}, function () {});
3737
$promise->receive();
3838

3939
$this->assertTrue($receiveInvoked);
@@ -42,11 +42,11 @@ public function testOnReceiveShouldCallReceiveCallBack()
4242
public function testOnReceiveNoWaitShouldCallReceiveNoWaitCallBack()
4343
{
4444
$receiveInvoked = false;
45-
$receivecb = function() use (&$receiveInvoked) {
45+
$receivecb = function () use (&$receiveInvoked) {
4646
$receiveInvoked = true;
4747
};
4848

49-
$promise = new Promise(function(){}, $receivecb, function(){});
49+
$promise = new Promise(function () {}, $receivecb, function () {});
5050
$promise->receiveNoWait();
5151

5252
$this->assertTrue($receiveInvoked);
@@ -55,11 +55,11 @@ public function testOnReceiveNoWaitShouldCallReceiveNoWaitCallBack()
5555
public function testOnReceiveShouldCallFinallyCallback()
5656
{
5757
$invoked = false;
58-
$cb = function() use (&$invoked) {
58+
$cb = function () use (&$invoked) {
5959
$invoked = true;
6060
};
6161

62-
$promise = new Promise(function(){}, function(){}, $cb);
62+
$promise = new Promise(function () {}, function () {}, $cb);
6363
$promise->receive();
6464

6565
$this->assertTrue($invoked);
@@ -68,32 +68,33 @@ public function testOnReceiveShouldCallFinallyCallback()
6868
public function testOnReceiveShouldCallFinallyCallbackEvenIfExceptionThrown()
6969
{
7070
$invokedFinally = false;
71-
$finallycb = function() use (&$invokedFinally) {
71+
$finallycb = function () use (&$invokedFinally) {
7272
$invokedFinally = true;
7373
};
7474

7575
$invokedReceive = false;
76-
$receivecb = function() use (&$invokedReceive) {
76+
$receivecb = function () use (&$invokedReceive) {
7777
$invokedReceive = true;
7878
throw new \Exception();
7979
};
8080

8181
try {
82-
$promise = new Promise($receivecb, function(){}, $finallycb);
82+
$promise = new Promise($receivecb, function () {}, $finallycb);
8383
$promise->receive();
84-
} catch (\Exception $e) {}
84+
} catch (\Exception $e) {
85+
}
8586

8687
$this->assertTrue($invokedReceive);
8788
$this->assertTrue($invokedFinally);
8889
}
8990

9091
public function testOnReceiveShouldThrowExceptionIfCallbackReturnNotMessageInstance()
9192
{
92-
$receivecb = function() {
93+
$receivecb = function () {
9394
return new \stdClass();
9495
};
9596

96-
$promise = new Promise($receivecb, function(){}, function(){});
97+
$promise = new Promise($receivecb, function () {}, function () {});
9798

9899
$this->expectException(\RuntimeException::class);
99100
$this->expectExceptionMessage('Expected "Enqueue\Psr\PsrMessage" but got: "stdClass"');
@@ -103,11 +104,11 @@ public function testOnReceiveShouldThrowExceptionIfCallbackReturnNotMessageInsta
103104

104105
public function testOnReceiveNoWaitShouldThrowExceptionIfCallbackReturnNotMessageInstance()
105106
{
106-
$receivecb = function() {
107+
$receivecb = function () {
107108
return new \stdClass();
108109
};
109110

110-
$promise = new Promise(function(){}, $receivecb, function(){});
111+
$promise = new Promise(function () {}, $receivecb, function () {});
111112

112113
$this->expectException(\RuntimeException::class);
113114
$this->expectExceptionMessage('Expected "Enqueue\Psr\PsrMessage" but got: "stdClass"');
@@ -118,16 +119,16 @@ public function testOnReceiveNoWaitShouldThrowExceptionIfCallbackReturnNotMessag
118119
public function testOnReceiveNoWaitShouldCallFinallyCallbackOnlyIfMessageReceived()
119120
{
120121
$invokedReceive = false;
121-
$receivecb = function() use (&$invokedReceive) {
122+
$receivecb = function () use (&$invokedReceive) {
122123
$invokedReceive = true;
123124
};
124125

125126
$invokedFinally = false;
126-
$finallycb = function() use (&$invokedFinally) {
127+
$finallycb = function () use (&$invokedFinally) {
127128
$invokedFinally = true;
128129
};
129130

130-
$promise = new Promise(function(){}, $receivecb, $finallycb);
131+
$promise = new Promise(function () {}, $receivecb, $finallycb);
131132
$promise->receiveNoWait();
132133

133134
$this->assertTrue($invokedReceive);
@@ -136,12 +137,13 @@ public function testOnReceiveNoWaitShouldCallFinallyCallbackOnlyIfMessageReceive
136137
// now should call finally too
137138

138139
$invokedReceive = false;
139-
$receivecb = function() use (&$invokedReceive) {
140+
$receivecb = function () use (&$invokedReceive) {
140141
$invokedReceive = true;
142+
141143
return new NullMessage();
142144
};
143145

144-
$promise = new Promise(function(){}, $receivecb, $finallycb);
146+
$promise = new Promise(function () {}, $receivecb, $finallycb);
145147
$promise->receiveNoWait();
146148

147149
$this->assertTrue($invokedReceive);
@@ -153,18 +155,18 @@ public function testOnReceiveShouldNotCallCallbackIfMessageReceivedByReceiveNoWa
153155
$message = new NullMessage();
154156

155157
$invokedReceive = false;
156-
$receivecb = function() use (&$invokedReceive) {
158+
$receivecb = function () use (&$invokedReceive) {
157159
$invokedReceive = true;
158160
};
159161

160162
$invokedReceiveNoWait = false;
161-
$receiveNoWaitCb = function() use (&$invokedReceiveNoWait, $message) {
163+
$receiveNoWaitCb = function () use (&$invokedReceiveNoWait, $message) {
162164
$invokedReceiveNoWait = true;
163165

164166
return $message;
165167
};
166168

167-
$promise = new Promise($receivecb, $receiveNoWaitCb, function(){});
169+
$promise = new Promise($receivecb, $receiveNoWaitCb, function () {});
168170

169171
$this->assertSame($message, $promise->receiveNoWait());
170172
$this->assertTrue($invokedReceiveNoWait);
@@ -183,17 +185,18 @@ public function testOnReceiveNoWaitShouldNotCallCallbackIfMessageReceivedByRecei
183185
$message = new NullMessage();
184186

185187
$invokedReceive = false;
186-
$receivecb = function() use (&$invokedReceive, $message) {
188+
$receivecb = function () use (&$invokedReceive, $message) {
187189
$invokedReceive = true;
190+
188191
return $message;
189192
};
190193

191194
$invokedReceiveNoWait = false;
192-
$receiveNoWaitCb = function() use (&$invokedReceiveNoWait) {
195+
$receiveNoWaitCb = function () use (&$invokedReceiveNoWait) {
193196
$invokedReceiveNoWait = true;
194197
};
195198

196-
$promise = new Promise($receivecb, $receiveNoWaitCb, function(){});
199+
$promise = new Promise($receivecb, $receiveNoWaitCb, function () {});
197200

198201
$this->assertSame($message, $promise->receive());
199202
$this->assertTrue($invokedReceive);
@@ -206,4 +209,20 @@ public function testOnReceiveNoWaitShouldNotCallCallbackIfMessageReceivedByRecei
206209
$this->assertFalse($invokedReceiveNoWait);
207210
$this->assertFalse($invokedReceive);
208211
}
212+
213+
public function testDeprecatedGetMessageShouldCallReceiveMethod()
214+
{
215+
$promise = $this->getMockBuilder(Promise::class)
216+
->disableOriginalConstructor()
217+
->setMethods(['receive'])
218+
->getMock()
219+
;
220+
221+
$promise
222+
->expects($this->once())
223+
->method('receive')
224+
;
225+
226+
$promise->getMessage();
227+
}
209228
}

0 commit comments

Comments
 (0)