-
Notifications
You must be signed in to change notification settings - Fork 436
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
20 changed files
with
556 additions
and
6 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
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
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,174 @@ | ||
<?php | ||
|
||
namespace Enqueue\Redis\Tests; | ||
|
||
use Enqueue\Redis\RedisConsumer; | ||
use Enqueue\Redis\RedisContext; | ||
use Enqueue\Redis\RedisSubscriptionConsumer; | ||
use Interop\Queue\PsrConsumer; | ||
use Interop\Queue\PsrQueue; | ||
use Interop\Queue\PsrSubscriptionConsumer; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RedisSubscriptionConsumerTest extends TestCase | ||
{ | ||
public function testShouldImplementPsrSubscriptionConsumerInterface() | ||
{ | ||
$rc = new \ReflectionClass(RedisSubscriptionConsumer::class); | ||
|
||
$this->assertTrue($rc->implementsInterface(PsrSubscriptionConsumer::class)); | ||
} | ||
|
||
public function testCouldBeConstructedWithAmqpContextAsFirstArgument() | ||
{ | ||
new RedisSubscriptionConsumer($this->createRedisContextMock()); | ||
} | ||
|
||
public function testShouldAddConsumerAndCallbackToSubscribersPropertyOnSubscribe() | ||
{ | ||
$subscriptionConsumer = new RedisSubscriptionConsumer($this->createRedisContextMock()); | ||
|
||
$fooCallback = function () {}; | ||
$fooConsumer = $this->createConsumerStub('foo_queue'); | ||
|
||
$barCallback = function () {}; | ||
$barConsumer = $this->createConsumerStub('bar_queue'); | ||
|
||
$subscriptionConsumer->subscribe($fooConsumer, $fooCallback); | ||
$subscriptionConsumer->subscribe($barConsumer, $barCallback); | ||
|
||
$this->assertAttributeSame([ | ||
'foo_queue' => [$fooConsumer, $fooCallback], | ||
'bar_queue' => [$barConsumer, $barCallback], | ||
], 'subscribers', $subscriptionConsumer); | ||
} | ||
|
||
public function testThrowsIfTrySubscribeAnotherConsumerToAlreadySubscribedQueue() | ||
{ | ||
$subscriptionConsumer = new RedisSubscriptionConsumer($this->createRedisContextMock()); | ||
|
||
$fooCallback = function () {}; | ||
$fooConsumer = $this->createConsumerStub('foo_queue'); | ||
|
||
$barCallback = function () {}; | ||
$barConsumer = $this->createConsumerStub('foo_queue'); | ||
|
||
$subscriptionConsumer->subscribe($fooConsumer, $fooCallback); | ||
|
||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('There is a consumer subscribed to queue: "foo_queue"'); | ||
$subscriptionConsumer->subscribe($barConsumer, $barCallback); | ||
} | ||
|
||
public function testShouldAllowSubscribeSameConsumerAndCallbackSecondTime() | ||
{ | ||
$subscriptionConsumer = new RedisSubscriptionConsumer($this->createRedisContextMock()); | ||
|
||
$fooCallback = function () {}; | ||
$fooConsumer = $this->createConsumerStub('foo_queue'); | ||
|
||
$subscriptionConsumer->subscribe($fooConsumer, $fooCallback); | ||
$subscriptionConsumer->subscribe($fooConsumer, $fooCallback); | ||
} | ||
|
||
public function testShouldRemoveSubscribedConsumerOnUnsubscribeCall() | ||
{ | ||
$subscriptionConsumer = new RedisSubscriptionConsumer($this->createRedisContextMock()); | ||
|
||
$fooConsumer = $this->createConsumerStub('foo_queue'); | ||
$barConsumer = $this->createConsumerStub('bar_queue'); | ||
|
||
$subscriptionConsumer->subscribe($fooConsumer, function () {}); | ||
$subscriptionConsumer->subscribe($barConsumer, function () {}); | ||
|
||
// guard | ||
$this->assertAttributeCount(2, 'subscribers', $subscriptionConsumer); | ||
|
||
$subscriptionConsumer->unsubscribe($fooConsumer); | ||
|
||
$this->assertAttributeCount(1, 'subscribers', $subscriptionConsumer); | ||
} | ||
|
||
public function testShouldDoNothingIfTryUnsubscribeNotSubscribedQueueName() | ||
{ | ||
$subscriptionConsumer = new RedisSubscriptionConsumer($this->createRedisContextMock()); | ||
|
||
$subscriptionConsumer->subscribe($this->createConsumerStub('foo_queue'), function () {}); | ||
|
||
// guard | ||
$this->assertAttributeCount(1, 'subscribers', $subscriptionConsumer); | ||
|
||
$subscriptionConsumer->unsubscribe($this->createConsumerStub('bar_queue')); | ||
|
||
$this->assertAttributeCount(1, 'subscribers', $subscriptionConsumer); | ||
} | ||
|
||
public function testShouldDoNothingIfTryUnsubscribeNotSubscribedConsumer() | ||
{ | ||
$subscriptionConsumer = new RedisSubscriptionConsumer($this->createRedisContextMock()); | ||
|
||
$subscriptionConsumer->subscribe($this->createConsumerStub('foo_queue'), function () {}); | ||
|
||
// guard | ||
$this->assertAttributeCount(1, 'subscribers', $subscriptionConsumer); | ||
|
||
$subscriptionConsumer->unsubscribe($this->createConsumerStub('foo_queue')); | ||
|
||
$this->assertAttributeCount(1, 'subscribers', $subscriptionConsumer); | ||
} | ||
|
||
public function testShouldRemoveAllSubscriberOnUnsubscribeAllCall() | ||
{ | ||
$subscriptionConsumer = new RedisSubscriptionConsumer($this->createRedisContextMock()); | ||
|
||
$subscriptionConsumer->subscribe($this->createConsumerStub('foo_queue'), function () {}); | ||
$subscriptionConsumer->subscribe($this->createConsumerStub('bar_queue'), function () {}); | ||
|
||
// guard | ||
$this->assertAttributeCount(2, 'subscribers', $subscriptionConsumer); | ||
|
||
$subscriptionConsumer->unsubscribeAll(); | ||
|
||
$this->assertAttributeCount(0, 'subscribers', $subscriptionConsumer); | ||
} | ||
|
||
public function testThrowsIfTryConsumeWithoutSubscribers() | ||
{ | ||
$subscriptionConsumer = new RedisSubscriptionConsumer($this->createRedisContextMock()); | ||
|
||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage('No subscribers'); | ||
$subscriptionConsumer->consume(); | ||
} | ||
|
||
/** | ||
* @return RedisContext|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private function createRedisContextMock() | ||
{ | ||
return $this->createMock(RedisContext::class); | ||
} | ||
|
||
/** | ||
* @param null|mixed $queueName | ||
* | ||
* @return PsrConsumer|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private function createConsumerStub($queueName = null) | ||
{ | ||
$queueMock = $this->createMock(PsrQueue::class); | ||
$queueMock | ||
->expects($this->any()) | ||
->method('getQueueName') | ||
->willReturn($queueName); | ||
|
||
$consumerMock = $this->createMock(RedisConsumer::class); | ||
$consumerMock | ||
->expects($this->any()) | ||
->method('getQueue') | ||
->willReturn($queueMock) | ||
; | ||
|
||
return $consumerMock; | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace Enqueue\Redis\Tests\Spec; | ||
|
||
use Enqueue\Redis\RedisConnectionFactory; | ||
use Interop\Queue\Spec\PsrConnectionFactorySpec; | ||
|
||
/** | ||
* @group Redis | ||
*/ | ||
class RedisConnectionFactoryTest extends PsrConnectionFactorySpec | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function createConnectionFactory() | ||
{ | ||
return new RedisConnectionFactory(); | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
namespace Enqueue\Redis\Tests\Spec; | ||
|
||
use Enqueue\Test\RedisExtension; | ||
use Interop\Queue\Spec\PsrContextSpec; | ||
|
||
/** | ||
* @group functional | ||
* @group Redis | ||
*/ | ||
class RedisContextTest extends PsrContextSpec | ||
{ | ||
use RedisExtension; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function createContext() | ||
{ | ||
return $this->buildPhpRedisContext(); | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
namespace Enqueue\Redis\Tests\Spec; | ||
|
||
use Enqueue\Test\RedisExtension; | ||
use Interop\Queue\Spec\PsrProducerSpec; | ||
|
||
/** | ||
* @group functional | ||
* @group Redis | ||
*/ | ||
class RedisProducerTest extends PsrProducerSpec | ||
{ | ||
use RedisExtension; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function createProducer() | ||
{ | ||
return $this->buildPhpRedisContext()->createProducer(); | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace Enqueue\Redis\Tests\Spec; | ||
|
||
use Enqueue\Redis\RedisDestination; | ||
use Interop\Queue\Spec\PsrQueueSpec; | ||
|
||
/** | ||
* @group Redis | ||
*/ | ||
class RedisQueueTest extends PsrQueueSpec | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function createQueue() | ||
{ | ||
return new RedisDestination(self::EXPECTED_QUEUE_NAME); | ||
} | ||
} |
Oops, something went wrong.