Skip to content

Commit

Permalink
Make dispatchable trait more testable
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Sep 25, 2014
1 parent eeceb86 commit 0aa39b1
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 8 deletions.
37 changes: 37 additions & 0 deletions spec/Laracasts/Commander/Events/DispatchableSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php namespace spec\Laracasts\Commander\Events;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Laracasts\Commander\Events\Contracts\Dispatcher;

class DispatchableSpec extends ObjectBehavior {

function let()
{
$this->beAnInstanceOf(HandlerStub::class);
}

function it_dispatches_stuff(Dispatcher $dispatcher)
{
$this->setDispatcher($dispatcher);

$this->dispatchEventsFor(new EntityStub);

$dispatcher->dispatch([])->shouldBeCalled();
}

}


class HandlerStub {
use \Laracasts\Commander\Events\DispatchableTrait;
}

class EntityStub {

public function releaseEvents()
{
return [];
}

}
12 changes: 12 additions & 0 deletions src/Laracasts/Commander/Events/Contracts/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php namespace Laracasts\Commander\Events\Contracts;

interface Dispatcher {

/**
* Dispatch all raised events.
*
* @param array $events
*/
public function dispatch(array $events);

}
25 changes: 22 additions & 3 deletions src/Laracasts/Commander/Events/DispatchableTrait.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
<?php namespace Laracasts\Commander\Events;

use App;
use Laracasts\Commander\Events\Contracts\Dispatcher;
use App;

trait DispatchableTrait {

/**
* The Dispatcher instance.
*
* @var Dispatcher
*/
protected $dispatcher;

/**
* Dispatch all events for an entity.
*
Expand All @@ -14,13 +22,24 @@ public function dispatchEventsFor($entity)
return $this->getDispatcher()->dispatch($entity->releaseEvents());
}

/**
* Set the dispatcher instance.
*
* @param mixed $dispatcher
*/
public function setDispatcher(Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}

/**
* Get the event dispatcher.
*
* @return \Laracasts\Commander\Events\EventDispatcher
* @return Dispatcher
*/
public function getDispatcher()
{
return App::make('Laracasts\Commander\Events\EventDispatcher');
return $this->dispatcher ?: App::make('Laracasts\Commander\Events\EventDispatcher');
}

}
16 changes: 11 additions & 5 deletions src/Laracasts/Commander/Events/EventDispatcher.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
<?php namespace Laracasts\Commander\Events;

use Laracasts\Commander\Events\Contracts\Dispatcher as DispatcherInterface;
use Illuminate\Events\Dispatcher;
use Illuminate\Log\Writer;

class EventDispatcher {
class EventDispatcher implements DispatcherInterface {

/**
* The Dispatcher instance.
*
* @var Dispatcher
*/
protected $event;

/**
* The writer instance.
*
* @var Writer
*/
protected $log;

/**
* Create a new EventDispatcher instance.
*
* @param Dispatcher $event
* @param Writer $log
*/
Expand All @@ -26,7 +33,7 @@ function __construct(Dispatcher $event, Writer $log)
}

/**
* Dispatch all events
* Dispatch all raised events.
*
* @param array $events
*/
Expand All @@ -43,11 +50,10 @@ public function dispatch(array $events)
}

/**
* We'll make the fired event name look
* just a bit more object-oriented.
* Make the fired event name look more object-oriented.
*
* @param $event
* @return mixed
* @return string
*/
protected function getEventName($event)
{
Expand Down

0 comments on commit 0aa39b1

Please sign in to comment.