Skip to content

Commit 0aa39b1

Browse files
committed
Make dispatchable trait more testable
1 parent eeceb86 commit 0aa39b1

File tree

4 files changed

+82
-8
lines changed

4 files changed

+82
-8
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php namespace spec\Laracasts\Commander\Events;
2+
3+
use PhpSpec\ObjectBehavior;
4+
use Prophecy\Argument;
5+
use Laracasts\Commander\Events\Contracts\Dispatcher;
6+
7+
class DispatchableSpec extends ObjectBehavior {
8+
9+
function let()
10+
{
11+
$this->beAnInstanceOf(HandlerStub::class);
12+
}
13+
14+
function it_dispatches_stuff(Dispatcher $dispatcher)
15+
{
16+
$this->setDispatcher($dispatcher);
17+
18+
$this->dispatchEventsFor(new EntityStub);
19+
20+
$dispatcher->dispatch([])->shouldBeCalled();
21+
}
22+
23+
}
24+
25+
26+
class HandlerStub {
27+
use \Laracasts\Commander\Events\DispatchableTrait;
28+
}
29+
30+
class EntityStub {
31+
32+
public function releaseEvents()
33+
{
34+
return [];
35+
}
36+
37+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php namespace Laracasts\Commander\Events\Contracts;
2+
3+
interface Dispatcher {
4+
5+
/**
6+
* Dispatch all raised events.
7+
*
8+
* @param array $events
9+
*/
10+
public function dispatch(array $events);
11+
12+
}
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
<?php namespace Laracasts\Commander\Events;
22

3-
use App;
3+
use Laracasts\Commander\Events\Contracts\Dispatcher;
4+
use App;
45

56
trait DispatchableTrait {
67

8+
/**
9+
* The Dispatcher instance.
10+
*
11+
* @var Dispatcher
12+
*/
13+
protected $dispatcher;
14+
715
/**
816
* Dispatch all events for an entity.
917
*
@@ -14,13 +22,24 @@ public function dispatchEventsFor($entity)
1422
return $this->getDispatcher()->dispatch($entity->releaseEvents());
1523
}
1624

25+
/**
26+
* Set the dispatcher instance.
27+
*
28+
* @param mixed $dispatcher
29+
*/
30+
public function setDispatcher(Dispatcher $dispatcher)
31+
{
32+
$this->dispatcher = $dispatcher;
33+
}
34+
1735
/**
1836
* Get the event dispatcher.
1937
*
20-
* @return \Laracasts\Commander\Events\EventDispatcher
38+
* @return Dispatcher
2139
*/
2240
public function getDispatcher()
2341
{
24-
return App::make('Laracasts\Commander\Events\EventDispatcher');
42+
return $this->dispatcher ?: App::make('Laracasts\Commander\Events\EventDispatcher');
2543
}
44+
2645
}

src/Laracasts/Commander/Events/EventDispatcher.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
<?php namespace Laracasts\Commander\Events;
22

3+
use Laracasts\Commander\Events\Contracts\Dispatcher as DispatcherInterface;
34
use Illuminate\Events\Dispatcher;
45
use Illuminate\Log\Writer;
56

6-
class EventDispatcher {
7+
class EventDispatcher implements DispatcherInterface {
78

89
/**
10+
* The Dispatcher instance.
11+
*
912
* @var Dispatcher
1013
*/
1114
protected $event;
1215

1316
/**
17+
* The writer instance.
18+
*
1419
* @var Writer
1520
*/
1621
protected $log;
1722

1823
/**
24+
* Create a new EventDispatcher instance.
25+
*
1926
* @param Dispatcher $event
2027
* @param Writer $log
2128
*/
@@ -26,7 +33,7 @@ function __construct(Dispatcher $event, Writer $log)
2633
}
2734

2835
/**
29-
* Dispatch all events
36+
* Dispatch all raised events.
3037
*
3138
* @param array $events
3239
*/
@@ -43,11 +50,10 @@ public function dispatch(array $events)
4350
}
4451

4552
/**
46-
* We'll make the fired event name look
47-
* just a bit more object-oriented.
53+
* Make the fired event name look more object-oriented.
4854
*
4955
* @param $event
50-
* @return mixed
56+
* @return string
5157
*/
5258
protected function getEventName($event)
5359
{

0 commit comments

Comments
 (0)