-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
12 changed files
with
242 additions
and
37 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
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,32 @@ | ||
<?php | ||
|
||
namespace Nuwber\Events\Tests; | ||
|
||
use Enqueue\AmqpLib\AmqpContext; | ||
use Enqueue\AmqpLib\AmqpProducer; | ||
use Interop\Amqp\Impl\AmqpMessage; | ||
use Interop\Amqp\Impl\AmqpTopic; | ||
use Nuwber\Events\BroadcastFactory; | ||
|
||
class BroadcastFactoryTest extends TestCase | ||
{ | ||
|
||
public function testSend() | ||
{ | ||
$message = new AmqpMessage('Hello!'); | ||
$topic = new AmqpTopic('events'); | ||
|
||
$producer = \Mockery::mock(AmqpProducer::class); | ||
$producer->shouldReceive('send') | ||
->with($topic, $message) | ||
->once(); | ||
|
||
$context = \Mockery::mock(AmqpContext::class)->makePartial(); | ||
$context->shouldReceive('createProducer') | ||
->andReturn($producer); | ||
|
||
$factory = new BroadcastFactory($context, $topic); | ||
|
||
self::assertNull($factory->send($message)); | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace Nuwber\Events\Tests; | ||
|
||
use Enqueue\AmqpLib\AmqpConsumer; | ||
use Enqueue\AmqpLib\AmqpContext; | ||
use Interop\Amqp\Impl\AmqpQueue; | ||
use Interop\Amqp\Impl\AmqpTopic; | ||
use Nuwber\Events\ConsumerFactory; | ||
|
||
class ConsumerFactoryTest extends TestCase | ||
{ | ||
|
||
public function testMake() | ||
{ | ||
$queue = new AmqpQueue(''); | ||
|
||
$consumer = \Mockery::mock(AmqpConsumer::class)->makePartial(); | ||
|
||
$context = \Mockery::mock(AmqpContext::class)->makePartial(); | ||
$context->shouldReceive('createConsumer') | ||
->once() | ||
->andReturn($consumer); | ||
|
||
$context->shouldReceive('createTemporaryQueue') | ||
->once() | ||
->andReturn($queue); | ||
|
||
$events = ['item.created', 'item.updated']; | ||
$context->shouldReceive('bind')->twice(); | ||
|
||
$factory = new ConsumerFactory($context, new AmqpTopic('events')); | ||
|
||
self::assertEquals($consumer, $factory->make($events)); | ||
} | ||
} |
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,89 @@ | ||
<?php | ||
|
||
namespace Nuwber\Events\Tests; | ||
|
||
use Nuwber\Events\Dispatcher; | ||
use PHPUnit\Framework\Assert; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DispatcherTest extends TestCase | ||
{ | ||
private $listen = [ | ||
'item.created' => [ | ||
'Listeners/Class1', | ||
'Listeners/Class2', | ||
], | ||
'item.updated' => [ | ||
'Listeners/Class3' | ||
], | ||
'item.*' => [ | ||
'Listeners/Class4' | ||
] | ||
]; | ||
|
||
public function testGetEvents() | ||
{ | ||
$events = array_keys($this->listen); | ||
|
||
self::assertEquals($events, $this->setupDispatcher()->getEvents()); | ||
} | ||
|
||
public function testListen() | ||
{ | ||
$dispatcher = new Dispatcher(); | ||
$dispatcher->listen('item.event', function() {}); | ||
|
||
self::assertTrue($dispatcher->hasListeners('item.event')); | ||
} | ||
|
||
public function testAddedClosureListeners() | ||
{ | ||
$dispatcher = new Dispatcher(); | ||
|
||
$dispatcher->listen('item.event', function() {}); | ||
$dispatcher->listen('item.event', function() {}); | ||
|
||
$listeners = $dispatcher->getListeners('item.event'); | ||
|
||
self::assertCount(1, $listeners); | ||
|
||
self::assertEquals(['Closure'], array_keys($listeners)); | ||
|
||
self::assertCount(2, $listeners['Closure']); | ||
} | ||
|
||
public function testCorrectWildcardHandling() | ||
{ | ||
$listeners = $this->setupDispatcher() | ||
->getListeners('item.event'); | ||
|
||
self::assertCount(1, $listeners); | ||
|
||
|
||
self::assertEquals(['Listeners/Class4'], array_keys($listeners)); | ||
} | ||
|
||
public function testListenersAddedWithNameAsKey() | ||
{ | ||
$listeners = $this->setupDispatcher() | ||
->getListeners('item.created'); | ||
|
||
// Expected 3 because 'item.created' + 'item.*' | ||
self::assertCount(3, $listeners); | ||
|
||
self::assertEquals(['Listeners/Class1', 'Listeners/Class2', 'Listeners/Class4'], array_keys($listeners)); | ||
} | ||
|
||
private function setupDispatcher() | ||
{ | ||
$dispatcher = new Dispatcher(); | ||
|
||
foreach ($this->listen as $event => $listeners) { | ||
foreach ($listeners as $listener) { | ||
$dispatcher->listen($event, $listener); | ||
} | ||
} | ||
|
||
return $dispatcher; | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
namespace Nuwber\Events\Tests; | ||
|
||
use Enqueue\AmqpLib\AmqpContext; | ||
use Interop\Amqp\Impl\AmqpMessage; | ||
use Interop\Amqp\Impl\AmqpTopic; | ||
use Nuwber\Events\MessageFactory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class MessageFactoryTest extends TestCase | ||
{ | ||
public function testMake() | ||
{ | ||
$data = ['id' => 1]; | ||
$event = 'item.created'; | ||
|
||
$expectedMessage = new AmqpMessage(json_encode($data)); | ||
$expectedMessage->setRoutingKey($event); | ||
|
||
$context = \Mockery::mock(AmqpContext::class)->makePartial(); | ||
|
||
$factory = new MessageFactory($context, new AmqpTopic('events')); | ||
|
||
$message = $factory->make($event, $data); | ||
|
||
self::assertEquals($expectedMessage, $message); | ||
} | ||
} |
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
Oops, something went wrong.