Skip to content

Commit

Permalink
[gearman] add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Jun 27, 2017
1 parent 4eb82f5 commit cee3f7d
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 19 deletions.
38 changes: 19 additions & 19 deletions GearmanMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class GearmanMessage implements PsrMessage, \JsonSerializable
*/
private $redelivered;

// /**
// * @var \GearmanJob
// */
// private $job;
/**
* @var \GearmanJob
*/
private $job;

/**
* @param string $body
Expand Down Expand Up @@ -286,19 +286,19 @@ public static function jsonUnserialize($json)
return new self($data['body'], $data['properties'], $data['headers']);
}

// /**
// * @return \GearmanJob
// */
// public function getJob()
// {
// return $this->job;
// }
//
// /**
// * @param \GearmanJob $job
// */
// public function setJob(\GearmanJob $job)
// {
// $this->job = $job;
// }
/**
* @return \GearmanJob
*/
public function getJob()
{
return $this->job;
}

/**
* @param \GearmanJob $job
*/
public function setJob(\GearmanJob $job)
{
$this->job = $job;
}
}
103 changes: 103 additions & 0 deletions Tests/GearmanConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Enqueue\Gearman\Tests;

use Enqueue\Gearman\GearmanConnectionFactory;
use Enqueue\Test\ClassExtensionTrait;
use PHPUnit\Framework\TestCase;

/**
* The class contains the factory tests dedicated to configuration.
*/
class GearmanConnectionFactoryConfigTest extends TestCase
{
use ClassExtensionTrait;

public function testThrowNeitherArrayStringNorNullGivenAsConfig()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The config must be either an array of options, a DSN string or null');

new GearmanConnectionFactory(new \stdClass());
}

public function testThrowIfSchemeIsNotGearmanAmqp()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The given DSN scheme "http" is not supported. Could be "gearman" only.');

new GearmanConnectionFactory('http://example.com');
}

public function testThrowIfDsnCouldNotBeParsed()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Failed to parse DSN "gearman://:@/"');

new GearmanConnectionFactory('gearman://:@/');
}

/**
* @dataProvider provideConfigs
*
* @param mixed $config
* @param mixed $expectedConfig
*/
public function testShouldParseConfigurationAsExpected($config, $expectedConfig)
{
$factory = new GearmanConnectionFactory($config);

$this->assertAttributeEquals($expectedConfig, 'config', $factory);
}

public static function provideConfigs()
{
yield [
null,
[
'host' => \GEARMAN_DEFAULT_TCP_HOST,
'port' => \GEARMAN_DEFAULT_TCP_PORT,
],
];

yield [
'gearman://',
[
'host' => \GEARMAN_DEFAULT_TCP_HOST,
'port' => \GEARMAN_DEFAULT_TCP_PORT,
],
];

yield [
[],
[
'host' => \GEARMAN_DEFAULT_TCP_HOST,
'port' => \GEARMAN_DEFAULT_TCP_PORT,
],
];

yield [
'gearman://theHost:1234',
[
'host' => 'theHost',
'port' => 1234,
],
];

yield [
['host' => 'theHost', 'port' => 1234],
[
'host' => 'theHost',
'port' => 1234,
],
];

yield [
['host' => 'theHost'],
[
'host' => 'theHost',
'port' => \GEARMAN_DEFAULT_TCP_PORT,
],
];
}
}
42 changes: 42 additions & 0 deletions Tests/GearmanContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Enqueue\Gearman\Tests;

use Enqueue\Gearman\GearmanContext;
use Enqueue\Null\NullQueue;
use Enqueue\Psr\InvalidDestinationException;
use Enqueue\Psr\PsrContext;
use Enqueue\Test\ClassExtensionTrait;
use PHPUnit\Framework\TestCase;

class GearmanContextTest extends TestCase
{
use ClassExtensionTrait;

public function testShouldImplementPsrContextInterface()
{
$this->assertClassImplements(PsrContext::class, GearmanContext::class);
}

public function testCouldBeConstructedWithConnectionConfigAsFirstArgument()
{
new GearmanContext(['host' => 'aHost', 'port' => 'aPort']);
}

public function testThrowNotImplementedOnCreateTemporaryQueue()
{
$context = new GearmanContext(['host' => 'aHost', 'port' => 'aPort']);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Not implemented');
$context->createTemporaryQueue();
}

public function testThrowInvalidDestinationIfInvalidDestinationGivenOnCreateConsumer()
{
$context = new GearmanContext(['host' => 'aHost', 'port' => 'aPort']);

$this->expectException(InvalidDestinationException::class);
$context->createConsumer(new NullQueue('aQueue'));
}
}
31 changes: 31 additions & 0 deletions Tests/GearmanDestinationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Enqueue\Gearman\Tests;

use Enqueue\Gearman\GearmanDestination;
use Enqueue\Psr\PsrQueue;
use Enqueue\Psr\PsrTopic;
use Enqueue\Test\ClassExtensionTrait;
use PHPUnit\Framework\TestCase;

class GearmanDestinationTest extends TestCase
{
use ClassExtensionTrait;

public function testShouldImplementPsrQueueInterface()
{
$this->assertClassImplements(PsrQueue::class, GearmanDestination::class);
}

public function testShouldImplementPsrTopicInterface()
{
$this->assertClassImplements(PsrTopic::class, GearmanDestination::class);
}

public function testShouldAllowGetNameSetInConstructor()
{
$destination = new GearmanDestination('theDestinationName');

$this->assertSame('theDestinationName', $destination->getName());
}
}
22 changes: 22 additions & 0 deletions Tests/GearmanMessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Enqueue\Gearman\Tests;

use Enqueue\Gearman\GearmanMessage;
use Enqueue\Test\ClassExtensionTrait;
use PHPUnit\Framework\TestCase;

class GearmanMessageTest extends TestCase
{
use ClassExtensionTrait;

public function testShouldAllowGetJobPreviouslySet()
{
$job = new \GearmanJob();

$message = new GearmanMessage();
$message->setJob($job);

$this->assertSame($job, $message->getJob());
}
}
76 changes: 76 additions & 0 deletions Tests/GearmanProducerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Enqueue\Gearman\Tests;

use Enqueue\Gearman\GearmanDestination;
use Enqueue\Gearman\GearmanMessage;
use Enqueue\Gearman\GearmanProducer;
use Enqueue\Null\NullMessage;
use Enqueue\Null\NullQueue;
use Enqueue\Psr\InvalidDestinationException;
use Enqueue\Psr\InvalidMessageException;
use Enqueue\Test\ClassExtensionTrait;
use PHPUnit\Framework\TestCase;

class GearmanProducerTest extends TestCase
{
use ClassExtensionTrait;

public function testCouldBeConstructedWithGearmanClientAsFirstArgument()
{
new GearmanProducer($this->createGearmanClientMock());
}

public function testThrowIfDestinationInvalid()
{
$producer = new GearmanProducer($this->createGearmanClientMock());

$this->expectException(InvalidDestinationException::class);
$this->expectExceptionMessage('The destination must be an instance of Enqueue\Gearman\GearmanDestination but got Enqueue\Null\NullQueue.');
$producer->send(new NullQueue('aQueue'), new GearmanMessage());
}

public function testThrowIfMessageInvalid()
{
$producer = new GearmanProducer($this->createGearmanClientMock());

$this->expectException(InvalidMessageException::class);
$this->expectExceptionMessage('The message must be an instance of Enqueue\Gearman\GearmanMessage but it is Enqueue\Null\NullMessage.');
$producer->send(new GearmanDestination('aQueue'), new NullMessage());
}

public function testShouldJsonEncodeMessageAndPutToExpectedTube()
{
$message = new GearmanMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']);

$gearman = $this->createGearmanClientMock();
$gearman
->expects($this->once())
->method('doBackground')
->with(
'theQueueName',
'{"body":"theBody","properties":{"foo":"fooVal"},"headers":{"bar":"barVal"}}'
)
;
$gearman
->expects($this->once())
->method('returnCode')
->willReturn(\GEARMAN_SUCCESS)
;

$producer = new GearmanProducer($gearman);

$producer->send(
new GearmanDestination('theQueueName'),
$message
);
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|\GearmanClient
*/
private function createGearmanClientMock()
{
return $this->createMock(\GearmanClient::class);
}
}

0 comments on commit cee3f7d

Please sign in to comment.