forked from php-enqueue/enqueue-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageQueueCollectorTest.php
152 lines (128 loc) · 4.92 KB
/
MessageQueueCollectorTest.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
143
144
145
146
147
148
149
150
151
152
<?php
namespace Enqueue\Bundle\Tests\Unit\Profiler;
use DMS\PHPUnitExtensions\ArraySubset\Assert;
use Enqueue\Bundle\Profiler\MessageQueueCollector;
use Enqueue\Client\MessagePriority;
use Enqueue\Client\ProducerInterface;
use Enqueue\Client\TraceableProducer;
use Enqueue\Test\ClassExtensionTrait;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
class MessageQueueCollectorTest extends TestCase
{
use ClassExtensionTrait;
public function testShouldExtendDataCollectorClass()
{
$this->assertClassExtends(DataCollector::class, MessageQueueCollector::class);
}
public function testShouldReturnExpectedName()
{
$collector = new MessageQueueCollector();
$this->assertEquals('enqueue.message_queue', $collector->getName());
}
public function testShouldReturnEmptySentMessageArrayIfNotTraceableProducer()
{
$collector = new MessageQueueCollector();
$collector->addProducer('default', $this->createProducerMock());
$collector->collect(new Request(), new Response());
$this->assertSame([], $collector->getSentMessages());
}
public function testShouldReturnSentMessageArrayTakenFromTraceableProducers()
{
$producer1 = new TraceableProducer($this->createProducerMock());
$producer1->sendEvent('fooTopic1', 'fooMessage');
$producer1->sendCommand('barCommand1', 'barMessage');
$producer2 = new TraceableProducer($this->createProducerMock());
$producer2->sendEvent('fooTopic2', 'fooMessage');
$collector = new MessageQueueCollector();
$collector->addProducer('foo', $producer1);
$collector->addProducer('bar', $producer2);
$collector->collect(new Request(), new Response());
Assert::assertArraySubset(
[
'foo' => [
[
'topic' => 'fooTopic1',
'command' => null,
'body' => 'fooMessage',
'headers' => [],
'properties' => [],
'priority' => null,
'expire' => null,
'delay' => null,
'timestamp' => null,
'contentType' => null,
'messageId' => null,
],
[
'topic' => null,
'command' => 'barCommand1',
'body' => 'barMessage',
'headers' => [],
'properties' => [],
'priority' => null,
'expire' => null,
'delay' => null,
'timestamp' => null,
'contentType' => null,
'messageId' => null,
],
],
'bar' => [
[
'topic' => 'fooTopic2',
'command' => null,
'body' => 'fooMessage',
'headers' => [],
'properties' => [],
'priority' => null,
'expire' => null,
'delay' => null,
'timestamp' => null,
'contentType' => null,
'messageId' => null,
],
],
],
$collector->getSentMessages()
);
}
public function testShouldPrettyPrintKnownPriority()
{
$collector = new MessageQueueCollector();
$this->assertEquals('normal', $collector->prettyPrintPriority(MessagePriority::NORMAL));
}
public function testShouldPrettyPrintUnknownPriority()
{
$collector = new MessageQueueCollector();
$this->assertEquals('unknownPriority', $collector->prettyPrintPriority('unknownPriority'));
}
public function testShouldEnsureStringKeepStringSame()
{
$collector = new MessageQueueCollector();
$this->assertEquals('foo', $collector->ensureString('foo'));
$this->assertEquals('bar baz', $collector->ensureString('bar baz'));
}
public function testShouldEnsureStringEncodeArrayToJson()
{
$collector = new MessageQueueCollector();
$this->assertEquals('["foo","bar"]', $collector->ensureString(['foo', 'bar']));
}
/**
* @return MockObject|ProducerInterface
*/
protected function createProducerMock()
{
return $this->createMock(ProducerInterface::class);
}
/**
* @return MockObject|TraceableProducer
*/
protected function createTraceableProducerMock()
{
return $this->createMock(TraceableProducer::class);
}
}