Skip to content

Commit cee3f7d

Browse files
committed
[gearman] add tests.
1 parent 4eb82f5 commit cee3f7d

6 files changed

+293
-19
lines changed

GearmanMessage.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ class GearmanMessage implements PsrMessage, \JsonSerializable
2727
*/
2828
private $redelivered;
2929

30-
// /**
31-
// * @var \GearmanJob
32-
// */
33-
// private $job;
30+
/**
31+
* @var \GearmanJob
32+
*/
33+
private $job;
3434

3535
/**
3636
* @param string $body
@@ -286,19 +286,19 @@ public static function jsonUnserialize($json)
286286
return new self($data['body'], $data['properties'], $data['headers']);
287287
}
288288

289-
// /**
290-
// * @return \GearmanJob
291-
// */
292-
// public function getJob()
293-
// {
294-
// return $this->job;
295-
// }
296-
//
297-
// /**
298-
// * @param \GearmanJob $job
299-
// */
300-
// public function setJob(\GearmanJob $job)
301-
// {
302-
// $this->job = $job;
303-
// }
289+
/**
290+
* @return \GearmanJob
291+
*/
292+
public function getJob()
293+
{
294+
return $this->job;
295+
}
296+
297+
/**
298+
* @param \GearmanJob $job
299+
*/
300+
public function setJob(\GearmanJob $job)
301+
{
302+
$this->job = $job;
303+
}
304304
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Enqueue\Gearman\Tests;
4+
5+
use Enqueue\Gearman\GearmanConnectionFactory;
6+
use Enqueue\Test\ClassExtensionTrait;
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* The class contains the factory tests dedicated to configuration.
11+
*/
12+
class GearmanConnectionFactoryConfigTest extends TestCase
13+
{
14+
use ClassExtensionTrait;
15+
16+
public function testThrowNeitherArrayStringNorNullGivenAsConfig()
17+
{
18+
$this->expectException(\LogicException::class);
19+
$this->expectExceptionMessage('The config must be either an array of options, a DSN string or null');
20+
21+
new GearmanConnectionFactory(new \stdClass());
22+
}
23+
24+
public function testThrowIfSchemeIsNotGearmanAmqp()
25+
{
26+
$this->expectException(\LogicException::class);
27+
$this->expectExceptionMessage('The given DSN scheme "http" is not supported. Could be "gearman" only.');
28+
29+
new GearmanConnectionFactory('http://example.com');
30+
}
31+
32+
public function testThrowIfDsnCouldNotBeParsed()
33+
{
34+
$this->expectException(\LogicException::class);
35+
$this->expectExceptionMessage('Failed to parse DSN "gearman://:@/"');
36+
37+
new GearmanConnectionFactory('gearman://:@/');
38+
}
39+
40+
/**
41+
* @dataProvider provideConfigs
42+
*
43+
* @param mixed $config
44+
* @param mixed $expectedConfig
45+
*/
46+
public function testShouldParseConfigurationAsExpected($config, $expectedConfig)
47+
{
48+
$factory = new GearmanConnectionFactory($config);
49+
50+
$this->assertAttributeEquals($expectedConfig, 'config', $factory);
51+
}
52+
53+
public static function provideConfigs()
54+
{
55+
yield [
56+
null,
57+
[
58+
'host' => \GEARMAN_DEFAULT_TCP_HOST,
59+
'port' => \GEARMAN_DEFAULT_TCP_PORT,
60+
],
61+
];
62+
63+
yield [
64+
'gearman://',
65+
[
66+
'host' => \GEARMAN_DEFAULT_TCP_HOST,
67+
'port' => \GEARMAN_DEFAULT_TCP_PORT,
68+
],
69+
];
70+
71+
yield [
72+
[],
73+
[
74+
'host' => \GEARMAN_DEFAULT_TCP_HOST,
75+
'port' => \GEARMAN_DEFAULT_TCP_PORT,
76+
],
77+
];
78+
79+
yield [
80+
'gearman://theHost:1234',
81+
[
82+
'host' => 'theHost',
83+
'port' => 1234,
84+
],
85+
];
86+
87+
yield [
88+
['host' => 'theHost', 'port' => 1234],
89+
[
90+
'host' => 'theHost',
91+
'port' => 1234,
92+
],
93+
];
94+
95+
yield [
96+
['host' => 'theHost'],
97+
[
98+
'host' => 'theHost',
99+
'port' => \GEARMAN_DEFAULT_TCP_PORT,
100+
],
101+
];
102+
}
103+
}

Tests/GearmanContextTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Enqueue\Gearman\Tests;
4+
5+
use Enqueue\Gearman\GearmanContext;
6+
use Enqueue\Null\NullQueue;
7+
use Enqueue\Psr\InvalidDestinationException;
8+
use Enqueue\Psr\PsrContext;
9+
use Enqueue\Test\ClassExtensionTrait;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class GearmanContextTest extends TestCase
13+
{
14+
use ClassExtensionTrait;
15+
16+
public function testShouldImplementPsrContextInterface()
17+
{
18+
$this->assertClassImplements(PsrContext::class, GearmanContext::class);
19+
}
20+
21+
public function testCouldBeConstructedWithConnectionConfigAsFirstArgument()
22+
{
23+
new GearmanContext(['host' => 'aHost', 'port' => 'aPort']);
24+
}
25+
26+
public function testThrowNotImplementedOnCreateTemporaryQueue()
27+
{
28+
$context = new GearmanContext(['host' => 'aHost', 'port' => 'aPort']);
29+
30+
$this->expectException(\LogicException::class);
31+
$this->expectExceptionMessage('Not implemented');
32+
$context->createTemporaryQueue();
33+
}
34+
35+
public function testThrowInvalidDestinationIfInvalidDestinationGivenOnCreateConsumer()
36+
{
37+
$context = new GearmanContext(['host' => 'aHost', 'port' => 'aPort']);
38+
39+
$this->expectException(InvalidDestinationException::class);
40+
$context->createConsumer(new NullQueue('aQueue'));
41+
}
42+
}

Tests/GearmanDestinationTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Enqueue\Gearman\Tests;
4+
5+
use Enqueue\Gearman\GearmanDestination;
6+
use Enqueue\Psr\PsrQueue;
7+
use Enqueue\Psr\PsrTopic;
8+
use Enqueue\Test\ClassExtensionTrait;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class GearmanDestinationTest extends TestCase
12+
{
13+
use ClassExtensionTrait;
14+
15+
public function testShouldImplementPsrQueueInterface()
16+
{
17+
$this->assertClassImplements(PsrQueue::class, GearmanDestination::class);
18+
}
19+
20+
public function testShouldImplementPsrTopicInterface()
21+
{
22+
$this->assertClassImplements(PsrTopic::class, GearmanDestination::class);
23+
}
24+
25+
public function testShouldAllowGetNameSetInConstructor()
26+
{
27+
$destination = new GearmanDestination('theDestinationName');
28+
29+
$this->assertSame('theDestinationName', $destination->getName());
30+
}
31+
}

Tests/GearmanMessageTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Enqueue\Gearman\Tests;
4+
5+
use Enqueue\Gearman\GearmanMessage;
6+
use Enqueue\Test\ClassExtensionTrait;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class GearmanMessageTest extends TestCase
10+
{
11+
use ClassExtensionTrait;
12+
13+
public function testShouldAllowGetJobPreviouslySet()
14+
{
15+
$job = new \GearmanJob();
16+
17+
$message = new GearmanMessage();
18+
$message->setJob($job);
19+
20+
$this->assertSame($job, $message->getJob());
21+
}
22+
}

Tests/GearmanProducerTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Enqueue\Gearman\Tests;
4+
5+
use Enqueue\Gearman\GearmanDestination;
6+
use Enqueue\Gearman\GearmanMessage;
7+
use Enqueue\Gearman\GearmanProducer;
8+
use Enqueue\Null\NullMessage;
9+
use Enqueue\Null\NullQueue;
10+
use Enqueue\Psr\InvalidDestinationException;
11+
use Enqueue\Psr\InvalidMessageException;
12+
use Enqueue\Test\ClassExtensionTrait;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class GearmanProducerTest extends TestCase
16+
{
17+
use ClassExtensionTrait;
18+
19+
public function testCouldBeConstructedWithGearmanClientAsFirstArgument()
20+
{
21+
new GearmanProducer($this->createGearmanClientMock());
22+
}
23+
24+
public function testThrowIfDestinationInvalid()
25+
{
26+
$producer = new GearmanProducer($this->createGearmanClientMock());
27+
28+
$this->expectException(InvalidDestinationException::class);
29+
$this->expectExceptionMessage('The destination must be an instance of Enqueue\Gearman\GearmanDestination but got Enqueue\Null\NullQueue.');
30+
$producer->send(new NullQueue('aQueue'), new GearmanMessage());
31+
}
32+
33+
public function testThrowIfMessageInvalid()
34+
{
35+
$producer = new GearmanProducer($this->createGearmanClientMock());
36+
37+
$this->expectException(InvalidMessageException::class);
38+
$this->expectExceptionMessage('The message must be an instance of Enqueue\Gearman\GearmanMessage but it is Enqueue\Null\NullMessage.');
39+
$producer->send(new GearmanDestination('aQueue'), new NullMessage());
40+
}
41+
42+
public function testShouldJsonEncodeMessageAndPutToExpectedTube()
43+
{
44+
$message = new GearmanMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']);
45+
46+
$gearman = $this->createGearmanClientMock();
47+
$gearman
48+
->expects($this->once())
49+
->method('doBackground')
50+
->with(
51+
'theQueueName',
52+
'{"body":"theBody","properties":{"foo":"fooVal"},"headers":{"bar":"barVal"}}'
53+
)
54+
;
55+
$gearman
56+
->expects($this->once())
57+
->method('returnCode')
58+
->willReturn(\GEARMAN_SUCCESS)
59+
;
60+
61+
$producer = new GearmanProducer($gearman);
62+
63+
$producer->send(
64+
new GearmanDestination('theQueueName'),
65+
$message
66+
);
67+
}
68+
69+
/**
70+
* @return \PHPUnit_Framework_MockObject_MockObject|\GearmanClient
71+
*/
72+
private function createGearmanClientMock()
73+
{
74+
return $this->createMock(\GearmanClient::class);
75+
}
76+
}

0 commit comments

Comments
 (0)