IronMqProviderTest::testCreateWillCreateAQueue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Uecode\Bundle\QPushBundle\IntegrationTests\Provider;
4
5
use IronMQ\IronMQ;
6
use Uecode\Bundle\QPushBundle\Event\MessageEvent;
7
use Uecode\Bundle\QPushBundle\Event\NotificationEvent;
8
use Uecode\Bundle\QPushBundle\Message\Notification;
9
use Uecode\Bundle\QPushBundle\Provider\IronMqProvider;
10
11
class IronMqProviderTest extends \PHPUnit_Framework_TestCase
12
{
13
    /**
14
     * Mock Client
15
     *
16
     * @var IronMqProvider
17
     */
18
    protected $provider;
19
    private $project_id;
20
21
    /**
22
     * @var IronMQ
23
     */
24
    private $client;
25
26
    public function setUp()
27
    {
28
        if (!defined("IRONMQ_TOKEN") || IRONMQ_TOKEN == 'CHANGE_ME') {
29
            throw new \RuntimeException('"IRONMQ_TOKEN" must be defined in tests/bootstrap.php');
30
        }
31
        if (!defined("IRONMQ_PROJECT_ID") || IRONMQ_PROJECT_ID == 'CHANGE_ME') {
32
            throw new \RuntimeException('"IRONMQ_PROJECT_ID" must be defined in tests/bootstrap.php');
33
        }
34
        if (!defined("IRONMQ_HOST")) {
35
            throw new \RuntimeException('"IRONMQ_HOST" must be defined in tests/bootstrap.php');
36
        }
37
        $this->client = new IronMQ([
38
            'token'         => IRONMQ_TOKEN,
39
            'project_id'    => IRONMQ_PROJECT_ID,
40
            'host'          => IRONMQ_HOST
41
        ]);
42
43
        $this->provider = $this->getIronMqProvider();
44
    }
45
46
    public function tearDown()
47
    {
48
        if (!is_null($this->provider)) {
49
            $this->provider->destroy();
50
            $this->provider = null;
51
        }
52
    }
53
54
    private function getIronMqProvider(array $options = [])
55
    {
56
        $options = array_merge(
57
            [
58
                'logging_enabled'            => false,
59
                'push_notifications'         => true,
60
                'push_type'                  => 'multicast',
61
                'notification_retries'       => 3,
62
                'notification_retries_delay' => 60,
63
                'message_delay'              => 0,
64
                'message_timeout'            => 30,
65
                'message_expiration'         => 604800,
66
                'messages_to_receive'        => 1,
67
                'rate_limit'                 => -1,
68
                'receive_wait_time'          => 3,
69
                'subscribers'                => [
70
                    [ 'protocol' => 'http', 'endpoint' => 'http://fake.com' ]
71
                ]
72
            ],
73
            $options
74
        );
75
76
        return new IronMqProvider(
77
            'test',
78
            $options,
79
            $this->client,
80
            $this->getMock(
81
                'Doctrine\Common\Cache\PhpFileCache',
82
                [],
83
                ['/tmp', 'qpush.ironmq.test.php']
84
            ),
85
            $this->getMock(
86
                'Symfony\Bridge\Monolog\Logger',
87
                [],
88
                ['qpush.test']
89
            )
90
        );
91
    }
92
93
    public function testGetProviderReturnsTheNameOfTheProvider()
94
    {
95
        $provider = $this->provider->getProvider();
96
97
        $this->assertEquals('IronMQ', $provider);
98
    }
99
100
    public function testCreateWillCreateAQueue()
101
    {
102
        $this->assertFalse($this->provider->queueExists());
103
        $this->assertTrue($this->provider->create());
104
        $this->assertTrue($this->provider->queueExists());
105
    }
106
107
    public function testCreateFailsWithEmailTypeSubscriber()
108
    {
109
        $provider = $this->getIronMqProvider([
110
            'subscribers' => [
111
                [ 'protocol' => 'email', 'endpoint' => '[email protected]' ]
112
            ]
113
        ]);
114
115
        $this->setExpectedException('InvalidArgumentException', 'IronMQ only supports `http` or `https` subscribers!');
116
        $provider->create();
117
        $this->assertTrue($this->provider->queueExists());
118
119
    }
120
121
    public function testDestroyWillDestroyAQueue()
122
    {
123
        $this->provider->create();
124
        $this->assertTrue($this->provider->queueExists(), 'fail1');
125
126
        $this->assertTrue($this->provider->destroy(), 'fail2');
127
128
        $this->assertFalse($this->provider->queueExists(), 'fail3');
129
130
    }
131
132
    public function testPublishWillPublishAMessage()
133
    {
134
        $message = $this->provider->publish(['foo' => 'bar']);
135
        $this->assertInternalType("int", $message);
136
    }
137
138
    public function testReceiveWillReserveAndReturnAMessageFromTheQueue()
139
    {
140
        $this->provider = $this->getIronMqProvider(['push_notifications' => false]);
141
        $this->provider->publish(['baz' => 'bar']);
142
143
        $messages = $this->provider->receive();
144
145
        $this->assertInternalType('array', $messages);
146
        $this->assertCount(1, $messages);
147
        $this->assertEquals(['baz' => 'bar'], $messages[0]->getBody());
148
    }
149
150
    public function testDeleteAnUnreservedMessage()
151
    {
152
        $messageId = $this->provider->publish(['bat' => 'ball']);
153
154
        $this->assertTrue($this->provider->delete($messageId));
155
    }
156
157 View Code Duplication
    public function testDeleteAReservedMessage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
    {
159
        $this->provider = $this->getIronMqProvider(['push_notifications' => false]);
160
        $this->provider->publish(['Hello' => 'World']);
161
        $messages = $this->provider->receive();
162
163
        $this->assertTrue($this->provider->delete($messages[0]->getId()));
164
    }
165
166
    public function testOnNotification()
167
    {
168
        $event = new NotificationEvent(
169
            'test',
170
            NotificationEvent::TYPE_MESSAGE,
171
            new Notification(123, "test", [])
172
        );
173
174
        $this->provider->onNotification(
175
            $event,
176
            NotificationEvent::TYPE_MESSAGE,
177
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
178
        );
179
    }
180
181 View Code Duplication
    public function testOnMessageReceived()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183
        $this->provider = $this->getIronMqProvider(['push_notifications' => false]);
184
        $this->provider->destroy();
185
        $this->provider->publish(['bob' => 'ball']);
186
        $messages = $this->provider->receive();
187
188
        $this->provider->onMessageReceived(new MessageEvent(
189
            'test',
190
            $messages[0]
191
        ));
192
    }
193
194
    public function testQueueInfo()
195
    {
196
        $this->provider->destroy();
197
        $this->assertNull($this->provider->queueInfo());
198
199
        $this->provider->create();
200
        $queue = $this->provider->queueInfo();
201
        $this->assertEquals('qpush_test', $queue->name);
202
        $this->assertEquals(IRONMQ_PROJECT_ID, $queue->project_id);
203
    }
204
}
205