-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
293 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |