Skip to content

Commit 11269d2

Browse files
committed
Adding tests
1 parent 518bfcb commit 11269d2

File tree

7 files changed

+270
-4
lines changed

7 files changed

+270
-4
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/.idea
2+
/tests/coverage
23
/vendor
34
composer.lock
4-
.phpstorm.meta.php
5+
.phpstorm.meta.php
6+
phpunit.xml

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
"illuminate/queue": "5.3.*",
1616
"php-amqplib/php-amqplib": "2.6.*"
1717
},
18+
"require-dev": {
19+
"phpunit/phpunit": "^5.5",
20+
"mockery/mockery": "^0.9.5"
21+
},
1822
"autoload": {
1923
"psr-0": {
2024
"VladimirYuldashev\\LaravelQueueRabbitMQ": "src/"
2125
}
2226
},
23-
"minimum-stability": "dev"
27+
"minimum-stability": "stable"
2428
}

phpunit.xml.dist

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Package Test Suite">
15+
<directory suffix=".php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
<php>
19+
<env name="HOST" value="127.0.0.1"/>
20+
<env name="PORT" value="5672"/>
21+
</php>
22+
<filter>
23+
<whitelist>
24+
<directory suffix=".php">src/</directory>
25+
</whitelist>
26+
</filter>
27+
<logging>
28+
<log type="coverage-html" target="tests/coverage" charset="UTF-8" yui="true" highlight="true"/>
29+
</logging>
30+
</phpunit>

src/VladimirYuldashev/LaravelQueueRabbitMQ/Queue/Connectors/RabbitMQConnector.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ class RabbitMQConnector implements ConnectorInterface
1818
public function connect(array $config)
1919
{
2020
// create connection with AMQP
21-
$connection = new AMQPStreamConnection($config['host'], $config['port'], $config['login'], $config['password'],
22-
$config['vhost']);
21+
$connection = new AMQPStreamConnection(
22+
$config['host'],
23+
$config['port'],
24+
$config['login'],
25+
$config['password'],
26+
$config['vhost']
27+
);
2328

2429
return new RabbitMQQueue(
2530
$connection,

tests/RabbitMQConnectorTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Connectors\RabbitMQConnector;
5+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue;
6+
7+
class RabbitMQConnectorTest extends TestCase
8+
{
9+
10+
public function test_connect()
11+
{
12+
$config = [
13+
'host' => getenv('HOST'),
14+
'port' => getenv('PORT'),
15+
'login' => 'guest',
16+
'password' => 'guest',
17+
'vhost' => '/',
18+
19+
'queue' => 'queue_name',
20+
'exchange_declare' => true,
21+
'queue_declare_bind' => true,
22+
23+
'queue_params' => [
24+
'passive' => false,
25+
'durable' => true,
26+
'exclusive' => false,
27+
'auto_delete' => false,
28+
],
29+
'exchange_params' => [
30+
'name' => null,
31+
'type' => 'direct',
32+
'passive' => false,
33+
'durable' => true,
34+
'auto_delete' => false,
35+
],
36+
];
37+
38+
$connector = new RabbitMQConnector();
39+
$queue = $connector->connect($config);
40+
41+
$this->assertInstanceOf(RabbitMQQueue::class, $queue);
42+
}
43+
44+
}

tests/RabbitMQQueueTest.php

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
3+
use Illuminate\Container\Container;
4+
use PhpAmqpLib\Channel\AMQPChannel;
5+
use PhpAmqpLib\Connection\AMQPStreamConnection;
6+
use PhpAmqpLib\Message\AMQPMessage;
7+
use PHPUnit\Framework\TestCase;
8+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue;
9+
10+
/**
11+
* @property \Mockery\MockInterface connection
12+
* @property \Mockery\MockInterface channel
13+
* @property array config
14+
* @property RabbitMQQueue queue
15+
*/
16+
class RabbitMQQueueTest extends TestCase
17+
{
18+
19+
public function setUp()
20+
{
21+
parent::setUp();
22+
23+
$this->connection = Mockery::mock(AMQPStreamConnection::class);
24+
$this->channel = Mockery::mock(AMQPChannel::class);
25+
26+
$this->connection->shouldReceive('channel')->andReturn($this->channel);
27+
28+
$this->config = [
29+
'queue' => str_random(),
30+
'queue_params' => [
31+
'passive' => false,
32+
'durable' => true,
33+
'exclusive' => false,
34+
'auto_delete' => false,
35+
],
36+
'exchange_params' => [
37+
'name' => 'exchange_name',
38+
'type' => 'direct',
39+
'passive' => false,
40+
'durable' => true,
41+
'auto_delete' => false,
42+
],
43+
'exchange_declare' => true,
44+
'queue_declare_bind' => true,
45+
];
46+
47+
$this->queue = new RabbitMQQueue($this->connection, $this->config);
48+
}
49+
50+
public function test_size()
51+
{
52+
$this->queue->size();
53+
}
54+
55+
public function test_push()
56+
{
57+
$job = new TestJob();
58+
$data = [];
59+
60+
$this->channel->shouldReceive('exchange_declare')->with(
61+
$this->config['exchange_params']['name'],
62+
$this->config['exchange_params']['type'],
63+
$this->config['exchange_params']['passive'],
64+
$this->config['exchange_params']['durable'],
65+
$this->config['exchange_params']['auto_delete']
66+
)->once();
67+
68+
$this->channel->shouldReceive('queue_declare')->with(
69+
$this->config['queue'],
70+
$this->config['queue_params']['passive'],
71+
$this->config['queue_params']['durable'],
72+
$this->config['queue_params']['exclusive'],
73+
$this->config['queue_params']['auto_delete']
74+
)->once();
75+
76+
$this->channel->shouldReceive('queue_bind')->with(
77+
$this->config['queue'],
78+
$this->config['exchange_params']['name'],
79+
$this->config['queue']
80+
)->once();
81+
82+
$this->channel->shouldReceive('basic_publish')->once();
83+
84+
$correlationId = $this->queue->push($job, $data);
85+
86+
$this->assertEquals(13, strlen($correlationId));
87+
}
88+
89+
public function test_later()
90+
{
91+
$job = new TestJob();
92+
$data = [];
93+
$delay = mt_rand(10, 60);
94+
95+
$this->channel->shouldReceive('exchange_declare')->with(
96+
$this->config['exchange_params']['name'],
97+
$this->config['exchange_params']['type'],
98+
$this->config['exchange_params']['passive'],
99+
$this->config['exchange_params']['durable'],
100+
$this->config['exchange_params']['auto_delete']
101+
)->once();
102+
103+
$this->channel->shouldReceive('queue_declare')->once();
104+
105+
// main queue
106+
$this->channel->shouldReceive('queue_bind')->with(
107+
$this->config['queue'],
108+
$this->config['exchange_params']['name'],
109+
$this->config['queue']
110+
)->once();
111+
112+
// delayed queue
113+
$this->channel->shouldReceive('queue_bind')->with(
114+
$this->config['queue'] . '_deferred_' . $delay,
115+
$this->config['exchange_params']['name'],
116+
$this->config['queue'] . '_deferred_' . $delay
117+
)->once();
118+
119+
$this->channel->shouldReceive('basic_publish')->once();
120+
121+
$correlationId = $this->queue->later($delay, $job, $data);
122+
123+
$this->assertEquals(13, strlen($correlationId));
124+
}
125+
126+
public function test_pop()
127+
{
128+
$message = Mockery::mock(AMQPMessage::class);
129+
130+
$this->channel->shouldReceive('exchange_declare')->with(
131+
$this->config['exchange_params']['name'],
132+
$this->config['exchange_params']['type'],
133+
$this->config['exchange_params']['passive'],
134+
$this->config['exchange_params']['durable'],
135+
$this->config['exchange_params']['auto_delete']
136+
)->once();
137+
138+
$this->channel->shouldReceive('queue_declare')->with(
139+
$this->config['queue'],
140+
$this->config['queue_params']['passive'],
141+
$this->config['queue_params']['durable'],
142+
$this->config['queue_params']['exclusive'],
143+
$this->config['queue_params']['auto_delete']
144+
)->once();
145+
146+
$this->channel->shouldReceive('queue_bind')->with(
147+
$this->config['queue'],
148+
$this->config['exchange_params']['name'],
149+
$this->config['queue']
150+
)->once();
151+
152+
$this->channel->shouldReceive('basic_get')->with($this->config['queue'])->andReturn($message)->once();
153+
154+
$this->queue->setContainer(Mockery::mock(Container::class));
155+
$this->queue->pop();
156+
}
157+
158+
public function test_setAttempts() {
159+
$count = mt_rand();
160+
161+
$this->queue->setAttempts($count);
162+
}
163+
164+
public function test_setCorrelationId() {
165+
$id = str_random();
166+
167+
$this->queue->setCorrelationId($id);
168+
}
169+
170+
}

tests/jobs/TestJob.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
class TestJob
4+
{
5+
6+
public function handle()
7+
{
8+
9+
}
10+
11+
}

0 commit comments

Comments
 (0)