Skip to content

Commit 0bf1b27

Browse files
committed
Add message spec
1 parent 20be1c2 commit 0bf1b27

20 files changed

+322
-601
lines changed

pkg/amqp-ext/AmqpMessage.php

Lines changed: 118 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@
33
namespace Enqueue\AmqpExt;
44

55
use Enqueue\Psr\PsrMessage;
6-
use Enqueue\Psr\PsrMessageTrait;
76

87
class AmqpMessage implements PsrMessage
98
{
10-
use PsrMessageTrait;
9+
/**
10+
* @var string
11+
*/
12+
private $body;
13+
14+
/**
15+
* @var array
16+
*/
17+
private $properties;
18+
19+
/**
20+
* @var array
21+
*/
22+
private $headers;
1123

1224
/**
1325
* @var string|null
@@ -19,6 +31,11 @@ class AmqpMessage implements PsrMessage
1931
*/
2032
private $consumerTag;
2133

34+
/**
35+
* @var bool
36+
*/
37+
private $redelivered;
38+
2239
/**
2340
* @var int
2441
*/
@@ -39,6 +56,102 @@ public function __construct($body = '', array $properties = [], array $headers =
3956
$this->flags = AMQP_NOPARAM;
4057
}
4158

59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function getBody()
63+
{
64+
return $this->body;
65+
}
66+
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
public function setBody($body)
71+
{
72+
$this->body = $body;
73+
}
74+
75+
/**
76+
* {@inheritdoc}
77+
*/
78+
public function setProperties(array $properties)
79+
{
80+
$this->properties = $properties;
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function getProperties()
87+
{
88+
return $this->properties;
89+
}
90+
91+
/**
92+
* {@inheritdoc}
93+
*/
94+
public function setProperty($name, $value)
95+
{
96+
$this->properties[$name] = $value;
97+
}
98+
99+
/**
100+
* {@inheritdoc}
101+
*/
102+
public function getProperty($name, $default = null)
103+
{
104+
return array_key_exists($name, $this->properties) ? $this->properties[$name] : $default;
105+
}
106+
107+
/**
108+
* {@inheritdoc}
109+
*/
110+
public function setHeaders(array $headers)
111+
{
112+
$this->headers = $headers;
113+
}
114+
115+
/**
116+
* {@inheritdoc}
117+
*/
118+
public function getHeaders()
119+
{
120+
return $this->headers;
121+
}
122+
123+
/**
124+
* {@inheritdoc}
125+
*/
126+
public function setHeader($name, $value)
127+
{
128+
$this->headers[$name] = $value;
129+
}
130+
131+
/**
132+
* {@inheritdoc}
133+
*/
134+
public function getHeader($name, $default = null)
135+
{
136+
return array_key_exists($name, $this->headers) ? $this->headers[$name] : $default;
137+
}
138+
139+
/**
140+
* {@inheritdoc}
141+
*/
142+
public function setRedelivered($redelivered)
143+
{
144+
$this->redelivered = (bool) $redelivered;
145+
}
146+
147+
/**
148+
* {@inheritdoc}
149+
*/
150+
public function isRedelivered()
151+
{
152+
return $this->redelivered;
153+
}
154+
42155
/**
43156
* {@inheritdoc}
44157
*/
@@ -76,7 +189,9 @@ public function getMessageId()
76189
*/
77190
public function getTimestamp()
78191
{
79-
return $this->getHeader('timestamp');
192+
$value = $this->getHeader('timestamp');
193+
194+
return $value === null ? null : (int) $value;
80195
}
81196

82197
/**

pkg/amqp-ext/Tests/AmqpMessageTest.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use Enqueue\AmqpExt\AmqpMessage;
66
use Enqueue\Psr\PsrMessage;
7-
use Enqueue\Psr\Tests\BasePsrMessageTest;
87
use Enqueue\Test\ClassExtensionTrait;
8+
use PHPUnit\Framework\TestCase;
99

10-
class AmqpMessageTest extends BasePsrMessageTest
10+
class AmqpMessageTest extends TestCase
1111
{
1212
use ClassExtensionTrait;
1313

@@ -41,39 +41,35 @@ public function testShouldSetNoParamFlagInConstructor()
4141
$this->assertSame(\AMQP_NOPARAM, $message->getFlags());
4242
}
4343

44-
public function testShouldReturnPreviouslySetCorrelationId()
44+
public function testShouldSetCorrelationIdAsHeader()
4545
{
4646
$message = new AmqpMessage();
4747
$message->setCorrelationId('theCorrelationId');
4848

49-
$this->assertSame('theCorrelationId', $message->getCorrelationId());
5049
$this->assertSame(['correlation_id' => 'theCorrelationId'], $message->getHeaders());
5150
}
5251

53-
public function testShouldReturnPreviouslySetMessageId()
52+
public function testShouldSetSetMessageIdAsHeader()
5453
{
5554
$message = new AmqpMessage();
5655
$message->setMessageId('theMessageId');
5756

58-
$this->assertSame('theMessageId', $message->getMessageId());
5957
$this->assertSame(['message_id' => 'theMessageId'], $message->getHeaders());
6058
}
6159

62-
public function testShouldReturnPreviouslySetTimestamp()
60+
public function testShouldSetTimestampAsHeader()
6361
{
6462
$message = new AmqpMessage();
6563
$message->setTimestamp('theTimestamp');
6664

67-
$this->assertSame('theTimestamp', $message->getTimestamp());
6865
$this->assertSame(['timestamp' => 'theTimestamp'], $message->getHeaders());
6966
}
7067

71-
public function testShouldReturnPreviouslySetReplyTo()
68+
public function testShouldSetReplyToAsHeader()
7269
{
7370
$message = new AmqpMessage();
7471
$message->setReplyTo('theReply');
7572

76-
$this->assertSame('theReply', $message->getReplyTo());
7773
$this->assertSame(['reply_to' => 'theReply'], $message->getHeaders());
7874
}
7975

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace Enqueue\AmqpExt\Tests\Spec;
3+
4+
use Enqueue\AmqpExt\AmqpMessage;
5+
use Enqueue\Psr\Spec\PsrMessageSpec;
6+
7+
class AmqpMessageTest extends PsrMessageSpec
8+
{
9+
/**
10+
* {@inheritdoc}
11+
*/
12+
protected function createMessage()
13+
{
14+
return new AmqpMessage();
15+
}
16+
}

pkg/dbal/DbalMessage.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class DbalMessage implements PsrMessage
4141
* @param array $properties
4242
* @param array $headers
4343
*/
44-
public function __construct($body = null, array $properties = [], array $headers = [])
44+
public function __construct($body = '', array $properties = [], array $headers = [])
4545
{
4646
$this->body = $body;
4747
$this->properties = $properties;
@@ -210,7 +210,7 @@ public function setCorrelationId($correlationId)
210210
*/
211211
public function getCorrelationId()
212212
{
213-
return $this->getHeader('correlation_id', '');
213+
return $this->getHeader('correlation_id', null);
214214
}
215215

216216
/**
@@ -226,22 +226,24 @@ public function setMessageId($messageId)
226226
*/
227227
public function getMessageId()
228228
{
229-
return $this->getHeader('message_id', '');
229+
return $this->getHeader('message_id', null);
230230
}
231231

232232
/**
233233
* {@inheritdoc}
234234
*/
235235
public function getTimestamp()
236236
{
237-
return $this->getHeader('timestamp');
237+
$value = $this->getHeader('timestamp');
238+
239+
return $value === null ? null : (int) $value;
238240
}
239241

240242
/**
241243
* {@inheritdoc}
242244
*/
243245
public function setTimestamp($timestamp)
244246
{
245-
$this->setHeader('timestamp', (int) $timestamp);
247+
$this->setHeader('timestamp', $timestamp);
246248
}
247249
}

0 commit comments

Comments
 (0)