Skip to content

Commit

Permalink
Minor improvements and fixes to the event decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed May 18, 2016
1 parent b607b02 commit c76c360
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Event/Decorator/ConditionDecorator.php
Expand Up @@ -25,6 +25,7 @@ class ConditionDecorator extends BaseDecorator

/**
* @inheritdoc
* @return mixed
*/
public function __invoke()
{
Expand Down
1 change: 1 addition & 0 deletions src/Event/Decorator/SubjectFilterDecorator.php
Expand Up @@ -25,6 +25,7 @@ class SubjectFilterDecorator extends BaseDecorator

/**
* @inheritdoc
* @return mixed
*/
public function __invoke()
{
Expand Down
6 changes: 3 additions & 3 deletions src/Event/EventManager.php
Expand Up @@ -14,7 +14,7 @@
*/
namespace Cake\Event;

use Cake\Event\Decorator\FilterDecorator;
use Cake\Event\Decorator\ConditionDecorator;
use InvalidArgumentException;

/**
Expand Down Expand Up @@ -158,14 +158,14 @@ public function on($eventKey = null, $options = [], $callable = null)
$argCount = func_num_args();
if ($argCount === 2) {
$this->_listeners[$eventKey][static::$defaultPriority][] = [
'callable' => new FilterDecorator($options),
'callable' => is_callable($options) ? new ConditionDecorator($options) : $options,
];
return;
}
if ($argCount === 3) {
$priority = isset($options['priority']) ? $options['priority'] : static::$defaultPriority;
$this->_listeners[$eventKey][$priority][] = [
'callable' => new FilterDecorator($callable, $options)
'callable' => is_callable($callable) ? new ConditionDecorator($callable, $options) : $callable
];
return;
}
Expand Down
12 changes: 5 additions & 7 deletions tests/TestCase/Event/Decorator/ConditionDecoratorTest.php
Expand Up @@ -35,16 +35,13 @@ class ConditionDecoratorTest extends TestCase
*/
public function testCanTriggerIf()
{
$callable = function(Event $event) {
$callable = function (Event $event) {
return 'success';
};

$decorator = new ConditionDecorator($callable, [
'if' => function(Event $event) {
if (isset($event->data['canTrigger'])) {
return true;
}
return false;
'if' => function (Event $event) {
return $event->data['canTrigger'];
}
]);

Expand All @@ -65,10 +62,11 @@ public function testCanTriggerIf()
* testCallableRuntimeException
*
* @expectedException \RuntimeException
* @expectedExceptionMessage Cake\Event\Decorator\ConditionDecorator the `if` condition is not a callable!
*/
public function testCallableRuntimeException()
{
$callable = function(Event $event) {
$callable = function (Event $event) {
return 'success';
};

Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Event/Decorator/SubjectFilterDecoratorTest.php
Expand Up @@ -18,8 +18,8 @@
*/
namespace Cake\Test\TestCase\Event;

use Cake\Event\Event;
use Cake\Event\Decorator\SubjectFilterDecorator;
use Cake\Event\Event;
use Cake\TestSuite\TestCase;

/**
Expand All @@ -36,7 +36,7 @@ class SubjectFilterDecoratorTest extends TestCase
public function testCanTrigger()
{
$event = new Event('decorator.test', $this);
$callable = function(Event $event) {
$callable = function (Event $event) {
return 'success';
};

Expand Down

0 comments on commit c76c360

Please sign in to comment.