Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vyuldashev committed Aug 26, 2016
1 parent 518bfcb commit 11269d2
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/.idea
/tests/coverage
/vendor
composer.lock
.phpstorm.meta.php
.phpstorm.meta.php
phpunit.xml
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
"illuminate/queue": "5.3.*",
"php-amqplib/php-amqplib": "2.6.*"
},
"require-dev": {
"phpunit/phpunit": "^5.5",
"mockery/mockery": "^0.9.5"
},
"autoload": {
"psr-0": {
"VladimirYuldashev\\LaravelQueueRabbitMQ": "src/"
}
},
"minimum-stability": "dev"
"minimum-stability": "stable"
}
30 changes: 30 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<php>
<env name="HOST" value="127.0.0.1"/>
<env name="PORT" value="5672"/>
</php>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="tests/coverage" charset="UTF-8" yui="true" highlight="true"/>
</logging>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ class RabbitMQConnector implements ConnectorInterface
public function connect(array $config)
{
// create connection with AMQP
$connection = new AMQPStreamConnection($config['host'], $config['port'], $config['login'], $config['password'],
$config['vhost']);
$connection = new AMQPStreamConnection(
$config['host'],
$config['port'],
$config['login'],
$config['password'],
$config['vhost']
);

return new RabbitMQQueue(
$connection,
Expand Down
44 changes: 44 additions & 0 deletions tests/RabbitMQConnectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use PHPUnit\Framework\TestCase;
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Connectors\RabbitMQConnector;
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue;

class RabbitMQConnectorTest extends TestCase
{

public function test_connect()
{
$config = [
'host' => getenv('HOST'),
'port' => getenv('PORT'),
'login' => 'guest',
'password' => 'guest',
'vhost' => '/',

'queue' => 'queue_name',
'exchange_declare' => true,
'queue_declare_bind' => true,

'queue_params' => [
'passive' => false,
'durable' => true,
'exclusive' => false,
'auto_delete' => false,
],
'exchange_params' => [
'name' => null,
'type' => 'direct',
'passive' => false,
'durable' => true,
'auto_delete' => false,
],
];

$connector = new RabbitMQConnector();
$queue = $connector->connect($config);

$this->assertInstanceOf(RabbitMQQueue::class, $queue);
}

}
170 changes: 170 additions & 0 deletions tests/RabbitMQQueueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

use Illuminate\Container\Container;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use PHPUnit\Framework\TestCase;
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue;

/**
* @property \Mockery\MockInterface connection
* @property \Mockery\MockInterface channel
* @property array config
* @property RabbitMQQueue queue
*/
class RabbitMQQueueTest extends TestCase
{

public function setUp()
{
parent::setUp();

$this->connection = Mockery::mock(AMQPStreamConnection::class);
$this->channel = Mockery::mock(AMQPChannel::class);

$this->connection->shouldReceive('channel')->andReturn($this->channel);

$this->config = [
'queue' => str_random(),
'queue_params' => [
'passive' => false,
'durable' => true,
'exclusive' => false,
'auto_delete' => false,
],
'exchange_params' => [
'name' => 'exchange_name',
'type' => 'direct',
'passive' => false,
'durable' => true,
'auto_delete' => false,
],
'exchange_declare' => true,
'queue_declare_bind' => true,
];

$this->queue = new RabbitMQQueue($this->connection, $this->config);
}

public function test_size()
{
$this->queue->size();
}

public function test_push()
{
$job = new TestJob();
$data = [];

$this->channel->shouldReceive('exchange_declare')->with(
$this->config['exchange_params']['name'],
$this->config['exchange_params']['type'],
$this->config['exchange_params']['passive'],
$this->config['exchange_params']['durable'],
$this->config['exchange_params']['auto_delete']
)->once();

$this->channel->shouldReceive('queue_declare')->with(
$this->config['queue'],
$this->config['queue_params']['passive'],
$this->config['queue_params']['durable'],
$this->config['queue_params']['exclusive'],
$this->config['queue_params']['auto_delete']
)->once();

$this->channel->shouldReceive('queue_bind')->with(
$this->config['queue'],
$this->config['exchange_params']['name'],
$this->config['queue']
)->once();

$this->channel->shouldReceive('basic_publish')->once();

$correlationId = $this->queue->push($job, $data);

$this->assertEquals(13, strlen($correlationId));
}

public function test_later()
{
$job = new TestJob();
$data = [];
$delay = mt_rand(10, 60);

$this->channel->shouldReceive('exchange_declare')->with(
$this->config['exchange_params']['name'],
$this->config['exchange_params']['type'],
$this->config['exchange_params']['passive'],
$this->config['exchange_params']['durable'],
$this->config['exchange_params']['auto_delete']
)->once();

$this->channel->shouldReceive('queue_declare')->once();

// main queue
$this->channel->shouldReceive('queue_bind')->with(
$this->config['queue'],
$this->config['exchange_params']['name'],
$this->config['queue']
)->once();

// delayed queue
$this->channel->shouldReceive('queue_bind')->with(
$this->config['queue'] . '_deferred_' . $delay,
$this->config['exchange_params']['name'],
$this->config['queue'] . '_deferred_' . $delay
)->once();

$this->channel->shouldReceive('basic_publish')->once();

$correlationId = $this->queue->later($delay, $job, $data);

$this->assertEquals(13, strlen($correlationId));
}

public function test_pop()
{
$message = Mockery::mock(AMQPMessage::class);

$this->channel->shouldReceive('exchange_declare')->with(
$this->config['exchange_params']['name'],
$this->config['exchange_params']['type'],
$this->config['exchange_params']['passive'],
$this->config['exchange_params']['durable'],
$this->config['exchange_params']['auto_delete']
)->once();

$this->channel->shouldReceive('queue_declare')->with(
$this->config['queue'],
$this->config['queue_params']['passive'],
$this->config['queue_params']['durable'],
$this->config['queue_params']['exclusive'],
$this->config['queue_params']['auto_delete']
)->once();

$this->channel->shouldReceive('queue_bind')->with(
$this->config['queue'],
$this->config['exchange_params']['name'],
$this->config['queue']
)->once();

$this->channel->shouldReceive('basic_get')->with($this->config['queue'])->andReturn($message)->once();

$this->queue->setContainer(Mockery::mock(Container::class));
$this->queue->pop();
}

public function test_setAttempts() {
$count = mt_rand();

$this->queue->setAttempts($count);
}

public function test_setCorrelationId() {
$id = str_random();

$this->queue->setCorrelationId($id);
}

}
11 changes: 11 additions & 0 deletions tests/jobs/TestJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class TestJob
{

public function handle()
{

}

}

0 comments on commit 11269d2

Please sign in to comment.