-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathIronMqProviderTest.php
More file actions
executable file
·205 lines (172 loc) · 6.24 KB
/
IronMqProviderTest.php
File metadata and controls
executable file
·205 lines (172 loc) · 6.24 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace Uecode\Bundle\QPushBundle\IntegrationTests\Provider;
use IronMQ\IronMQ;
use Uecode\Bundle\QPushBundle\Event\MessageEvent;
use Uecode\Bundle\QPushBundle\Event\NotificationEvent;
use Uecode\Bundle\QPushBundle\Message\Notification;
use Uecode\Bundle\QPushBundle\Provider\IronMqProvider;
class IronMqProviderTest extends \PHPUnit_Framework_TestCase
{
/**
* Mock Client
*
* @var IronMqProvider
*/
protected $provider;
private $project_id;
/**
* @var IronMQ
*/
private $client;
public function setUp()
{
if (!defined("IRONMQ_TOKEN") || IRONMQ_TOKEN == 'CHANGE_ME') {
throw new \RuntimeException('"IRONMQ_TOKEN" must be defined in tests/bootstrap.php');
}
if (!defined("IRONMQ_PROJECT_ID") || IRONMQ_PROJECT_ID == 'CHANGE_ME') {
throw new \RuntimeException('"IRONMQ_PROJECT_ID" must be defined in tests/bootstrap.php');
}
if (!defined("IRONMQ_HOST")) {
throw new \RuntimeException('"IRONMQ_HOST" must be defined in tests/bootstrap.php');
}
$this->client = new IronMQ([
'token' => IRONMQ_TOKEN,
'project_id' => IRONMQ_PROJECT_ID,
'host' => IRONMQ_HOST
]);
$this->provider = $this->getIronMqProvider();
}
public function tearDown()
{
if (!is_null($this->provider)) {
$this->provider->destroy();
$this->provider = null;
}
}
private function getIronMqProvider(array $options = [])
{
$options = array_merge(
[
'logging_enabled' => false,
'push_notifications' => true,
'push_notifications_only' => false,
'push_type' => 'multicast',
'notification_retries' => 3,
'notification_retries_delay' => 60,
'message_delay' => 0,
'message_timeout' => 30,
'message_expiration' => 604800,
'messages_to_receive' => 1,
'rate_limit' => -1,
'receive_wait_time' => 3,
'subscribers' => [
[ 'protocol' => 'http', 'endpoint' => 'http://fake.com' ]
]
],
$options
);
return new IronMqProvider(
'test',
$options,
$this->client,
$this->getMock(
'Doctrine\Common\Cache\PhpFileCache',
[],
['/tmp', 'qpush.ironmq.test.php']
),
$this->getMock(
'Symfony\Bridge\Monolog\Logger',
[],
['qpush.test']
)
);
}
public function testGetProviderReturnsTheNameOfTheProvider()
{
$provider = $this->provider->getProvider();
$this->assertEquals('IronMQ', $provider);
}
public function testCreateWillCreateAQueue()
{
$this->assertFalse($this->provider->queueExists());
$this->assertTrue($this->provider->create());
$this->assertTrue($this->provider->queueExists());
}
public function testCreateFailsWithEmailTypeSubscriber()
{
$provider = $this->getIronMqProvider([
'subscribers' => [
[ 'protocol' => 'email', 'endpoint' => 'test@foo.com' ]
]
]);
$this->setExpectedException('InvalidArgumentException', 'IronMQ only supports `http` or `https` subscribers!');
$provider->create();
$this->assertTrue($this->provider->queueExists());
}
public function testDestroyWillDestroyAQueue()
{
$this->provider->create();
$this->assertTrue($this->provider->queueExists(), 'fail1');
$this->assertTrue($this->provider->destroy(), 'fail2');
$this->assertFalse($this->provider->queueExists(), 'fail3');
}
public function testPublishWillPublishAMessage()
{
$message = $this->provider->publish(['foo' => 'bar']);
$this->assertInternalType("int", $message);
}
public function testReceiveWillReserveAndReturnAMessageFromTheQueue()
{
$this->provider = $this->getIronMqProvider(['push_notifications' => false]);
$this->provider->publish(['baz' => 'bar']);
$messages = $this->provider->receive();
$this->assertInternalType('array', $messages);
$this->assertCount(1, $messages);
$this->assertEquals(['baz' => 'bar'], $messages[0]->getBody());
}
public function testDeleteAnUnreservedMessage()
{
$messageId = $this->provider->publish(['bat' => 'ball']);
$this->assertTrue($this->provider->delete($messageId));
}
public function testDeleteAReservedMessage()
{
$this->provider = $this->getIronMqProvider(['push_notifications' => false]);
$this->provider->publish(['Hello' => 'World']);
$messages = $this->provider->receive();
$this->assertTrue($this->provider->delete($messages[0]->getId()));
}
public function testOnNotification()
{
$event = new NotificationEvent(
'test',
NotificationEvent::TYPE_MESSAGE,
new Notification(123, "test", [])
);
$this->provider->onNotification(
$event,
NotificationEvent::TYPE_MESSAGE,
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
);
}
public function testOnMessageReceived()
{
$this->provider = $this->getIronMqProvider(['push_notifications' => false]);
$this->provider->destroy();
$this->provider->publish(['bob' => 'ball']);
$messages = $this->provider->receive();
$this->provider->onMessageReceived(new MessageEvent(
'test',
$messages[0]
));
}
public function testQueueInfo()
{
$this->provider->destroy();
$this->assertNull($this->provider->queueInfo());
$this->provider->create();
$queue = $this->provider->queueInfo();
$this->assertEquals('qpush_test', $queue->name);
$this->assertEquals(IRONMQ_PROJECT_ID, $queue->project_id);
}
}