-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathPrepareBodyExtensionTest.php
142 lines (100 loc) · 4.57 KB
/
PrepareBodyExtensionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace Enqueue\Tests\Client\Extension;
use ArrayObject;
use Enqueue\Client\DriverInterface;
use Enqueue\Client\Extension\PrepareBodyExtension;
use Enqueue\Client\Message;
use Enqueue\Client\PreSend;
use Enqueue\Client\PreSendCommandExtensionInterface;
use Enqueue\Client\PreSendEventExtensionInterface;
use Enqueue\Client\ProducerInterface;
use Enqueue\Tests\Mocks\JsonSerializableObject;
use PHPUnit\Framework\TestCase;
class PrepareBodyExtensionTest extends TestCase
{
public function testShouldImplementExtensionInterface()
{
$rc = new \ReflectionClass(PrepareBodyExtension::class);
$this->assertTrue($rc->implementsInterface(PreSendEventExtensionInterface::class));
$this->assertTrue($rc->implementsInterface(PreSendCommandExtensionInterface::class));
}
public function testCouldConstructedWithoutAnyArguments()
{
new PrepareBodyExtension();
}
/**
* @dataProvider provideMessages
*
* @param mixed $body
* @param mixed|null $contentType
*/
public function testShouldSendStringUnchangedAndAddPlainTextContentTypeIfEmpty(
$body,
$contentType,
string $expectedBody,
string $expectedContentType
) {
$message = new Message($body);
$message->setContentType($contentType);
$context = $this->createDummyPreSendContext('aTopic', $message);
$extension = new PrepareBodyExtension();
$extension->onPreSendEvent($context);
$this->assertSame($expectedBody, $message->getBody());
$this->assertSame($expectedContentType, $message->getContentType());
}
public function testThrowIfBodyIsObject()
{
$message = new Message(new \stdClass());
$context = $this->createDummyPreSendContext('aTopic', $message);
$extension = new PrepareBodyExtension();
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The message\'s body must be either null, scalar, array or object (implements \JsonSerializable). Got: stdClass');
$extension->onPreSendEvent($context);
}
public function testThrowIfBodyIsArrayWithObjectsInsideOnSend()
{
$message = new Message(['foo' => new \stdClass()]);
$context = $this->createDummyPreSendContext('aTopic', $message);
$extension = new PrepareBodyExtension();
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The message\'s body must be an array of scalars. Found not scalar in the array: stdClass');
$extension->onPreSendEvent($context);
}
public function testShouldThrowExceptionIfBodyIsArrayWithObjectsInSubArraysInsideOnSend()
{
$message = new Message(['foo' => ['bar' => new \stdClass()]]);
$context = $this->createDummyPreSendContext('aTopic', $message);
$extension = new PrepareBodyExtension();
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The message\'s body must be an array of scalars. Found not scalar in the array: stdClass');
$extension->onPreSendEvent($context);
}
public static function provideMessages()
{
yield ['theBody', null, 'theBody', 'text/plain'];
yield ['theBody', 'foo/bar', 'theBody', 'foo/bar'];
yield [12345, null, '12345', 'text/plain'];
yield [12345, 'foo/bar', '12345', 'foo/bar'];
yield [12.345, null, '12.345', 'text/plain'];
yield [12.345, 'foo/bar', '12.345', 'foo/bar'];
yield [true, null, '1', 'text/plain'];
yield [true, 'foo/bar', '1', 'foo/bar'];
yield [null, null, '', 'text/plain'];
yield [null, 'foo/bar', '', 'foo/bar'];
yield [['foo' => 'fooVal'], null, '{"foo":"fooVal"}', 'application/json'];
yield [['foo' => 'fooVal'], 'foo/bar', '{"foo":"fooVal"}', 'foo/bar'];
yield [new ArrayObject(['foo' => 'fooVal']), null, '{"foo":"fooVal"}', 'application/json'];
yield [['foo' => 'fooVal', 'bar' => new ArrayObject(['barOne', 'barTwo'])], null, '{"foo":"fooVal","bar":["barOne","barTwo"]}', 'application/json'];
yield [new JsonSerializableObject(), null, '{"foo":"fooVal"}', 'application/json'];
yield [new JsonSerializableObject(), 'foo/bar', '{"foo":"fooVal"}', 'foo/bar'];
}
private function createDummyPreSendContext($commandOrTopic, $message): PreSend
{
return new PreSend(
$commandOrTopic,
$message,
$this->createMock(ProducerInterface::class),
$this->createMock(DriverInterface::class)
);
}
}