Skip to content

Commit

Permalink
Make more tests (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
masterjus authored Oct 30, 2017
1 parent 6034a9d commit 70886b2
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 37 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"Nuwber\\Events\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Nuwber's broadcasting events provides a simple observer implementation, allowing

Do not confuse this package with Laravel's broadcast. This package was made to communicate in backend-backend way.

Generally, this is compilation of LAravel's [events](https://laravel.com/docs/events) and [queues](https://laravel.com/docs/queues).
Generally, this is compilation of Laravel's [events](https://laravel.com/docs/events) and [queues](https://laravel.com/docs/queues).

Listener classes are typically stored in the `app/Listeners` folder. You may use Laravel's artisan command to generate them as it described in the [official documentation](https://laravel.com/docs/events).

Expand Down Expand Up @@ -136,7 +136,7 @@ After this command start all registered in project events will be registered in
Currently it doesn't detaches command from console, so you can just add `&` at the end of command:

```
php artian events:listen > /dev/null &
php artisan events:listen > /dev/null &
```

In this case you need to remember that you have organize some system such as [Supervisor](http://supervisord.org/) or [pm2](http://pm2.keymetrics.io/) which will controll your processes.
Expand Down
4 changes: 2 additions & 2 deletions src/Console/ListenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ protected function listenForSignals()
* @param int $memoryLimit
* @return bool
*/
public function memoryExceeded($memoryLimit)
protected function memoryExceeded($memoryLimit)
{
return (memory_get_usage(true) / 1024 / 1024) >= $memoryLimit;
}
Expand All @@ -236,7 +236,7 @@ public function memoryExceeded($memoryLimit)
* @param int $status
* @return void
*/
public function stop($status = 0)
protected function stop($status = 0)
{
exit($status);
}
Expand Down
13 changes: 11 additions & 2 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function listen($events, $listener)
if (Str::contains($event, '*')) {
$this->setupWildcardListen($event, $listener);
} else {
$this->listeners[$event][$listener] = $this->makeListener($listener);
$this->listeners[$event][$this->getListenerClass($listener)][] = $this->makeListener($listener);
}
}
}
Expand All @@ -39,6 +39,15 @@ public function listen($events, $listener)
*/
protected function setupWildcardListen($event, $listener)
{
$this->wildcards[$event][$listener] = $this->makeListener($listener, true);
$this->wildcards[$event][$this->getListenerClass($listener)][] = $this->makeListener($listener, true);
}

protected function getListenerClass($listener)
{
if ($listener instanceof \Closure) {
return \Closure::class;
}

return $listener;
}
}
24 changes: 13 additions & 11 deletions src/MessageProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,19 @@ protected function makeJobs(PsrConsumer $consumer, PsrMessage $payload)
{
$event = $payload->getRoutingKey();

foreach ($this->broadcastEvents->getListeners($event) as $name => $listener) {
yield new Job(
$this->container,
$this->context,
$consumer,
$payload,
$this->connectionName,
$event,
$name,
$listener
);
foreach ($this->broadcastEvents->getListeners($event) as $name => $listeners) {
foreach ($listeners as $listener) {
yield new Job(
$this->container,
$this->context,
$consumer,
$payload,
$this->connectionName,
$event,
$name,
$listener
);
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions tests/BroadcastFactoryTest.php
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));
}
}
36 changes: 36 additions & 0 deletions tests/ConsumerFactoryTest.php
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));
}
}
89 changes: 89 additions & 0 deletions tests/DispatcherTest.php
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;
}
}
17 changes: 6 additions & 11 deletions tests/JobTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php

namespace Nuwber\Events;
namespace Nuwber\Events\Tests;

use Illuminate\Container\Container;
use Interop\Queue\PsrConsumer;
use Interop\Queue\PsrContext;
use Interop\Queue\PsrMessage;
use Mockery as m;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Nuwber\Events\Job;

class JobTest extends TestCase
{
Expand All @@ -24,7 +23,8 @@ public function setUp()
};

$message = m::mock(PsrMessage::class);
$message->shouldReceive('getBody')->andReturn('{"id": 1}');
$message->shouldReceive('getBody')
->andReturn('{"id": 1}');

$this->job = new Job(
m::mock(Container::class),
Expand All @@ -38,21 +38,16 @@ public function setUp()
);
}

public function tearDown()
{
m::close();
}

public function testFire()
{
Assert::assertEquals("Event: $this->event. Item id: 1", $this->job->fire());
self::assertEquals("Event: $this->event. Item id: 1", $this->job->fire());
}

public function testGetName()
{
$expectedMessage = "$this->connectionName: $this->event:$this->listenerClass";

Assert::assertEquals($expectedMessage, $this->job->getName());
self::assertEquals($expectedMessage, $this->job->getName());
}

}
29 changes: 29 additions & 0 deletions tests/MessageFactoryTest.php
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);
}
}
15 changes: 6 additions & 9 deletions tests/MessageProcessorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Nuwber\Events;
namespace Nuwber\Events\Tests;

use Enqueue\AmqpLib\AmqpConsumer;
use Enqueue\AmqpLib\AmqpProducer;
Expand All @@ -11,10 +11,12 @@
use Interop\Queue\PsrConsumer;
use Interop\Queue\PsrContext;
use Mockery as m;
use Nuwber\Events\Dispatcher;
use Nuwber\Events\Exceptions\FailedException;
use Nuwber\Events\MessageProcessor;
use Nuwber\Events\ProcessingOptions;
use PhpAmqpLib\Channel\AMQPChannel;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;

class MessageProcessorTest extends TestCase
{
Expand Down Expand Up @@ -66,9 +68,9 @@ public function testProcessJob()
public function testProcessJobFailException()
{
$this->listeners = [
'ListenerClass' => function () {
'ListenerClass' => [function () {
throw new FailedException();
}
}]
];

$broadcastEvents = m::spy(Dispatcher::class)->makePartial();
Expand Down Expand Up @@ -120,9 +122,4 @@ private function createConsumer()

return new AmqpConsumer($channel, $queue, new Buffer(), 'basic_get');
}

public function tearDown()
{
m::close();
}
}
Loading

0 comments on commit 70886b2

Please sign in to comment.