Skip to content
Permalink
Browse files

Adding tests for the FilterDecorator

  • Loading branch information...
burzum committed May 18, 2016
1 parent 86baa70 commit 757198a48bd7b1b139af371e785f3d98f6b548aa
Showing with 89 additions and 1 deletion.
  1. +8 −1 src/Event/Decorator/FilterDecorator.php
  2. +81 −0 tests/TestCase/Event/Decorator/FilterDecoratorTest.php
@@ -14,8 +14,12 @@
*/
namespace Cake\Event\Decorator;
use Cake\Event\Event;
use RuntimeException;
/**
* Event Filter Decorator
*/
class FilterDecorator extends EventDecorator {
/**
@@ -53,10 +57,13 @@ public function canTrigger(Event $event)
protected function _evaluateCondition($condition, Event $event)
{
if (!isset($this->_options[$condition])) {
if ($condition === 'unless') {
return false;
}
return true;
}
if (!is_callable($this->_options[$condition])) {
throw new RuntimeException('Is not a callable!');
throw new RuntimeException(self::class . ' the `' . $condition . '` condition is not a callable!');
}
return $this->_options[$condition]($event);
}
@@ -0,0 +1,81 @@
<?php
/**
* EventTest file
*
* Test Case for Event class
*
* CakePHP : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP Project
* @since 3.3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Event;
use Cake\Event\Event;
use Cake\Event\Decorator\FilterDecorator;
use Cake\TestSuite\TestCase;
/**
* Tests the Cake\Event\Event class functionality
*/
class EventTest extends TestCase
{
/**
* testCanTriggerIf
*
* @return void
*/
public function testCanTriggerIf()
{
$callable = function(Event $event) {
return 'success';
};
$decorator = new FilterDecorator($callable, [
'if' => function(Event $event) {
if (isset($event->data['canTrigger'])) {
return true;
}
return false;
}
]);
$event = new Event('decorator.test', $this);
$this->assertFalse($decorator->canTrigger($event));
$result = $decorator($event);
$this->assertFalse($result);
$event = new Event('decorator.test', $this, ['canTrigger' => true]);
$this->assertTrue($decorator->canTrigger($event));
$result = $decorator($event);
$this->assertEquals('success', $result);
}
/**
* testCallableRuntimeException
*
* @expectedException \RuntimeException
*/
public function testCallableRuntimeException() {
$callable = function(Event $event) {
return 'success';
};
$decorator = new FilterDecorator($callable, [
'if' => 'not a callable'
]);
$event = new Event('decorator.test', $this, []);
$decorator($event);
}
}

0 comments on commit 757198a

Please sign in to comment.
You can’t perform that action at this time.