Skip to content
Permalink
Browse files

Added assertEventFired and assertEventFiredWith to TestCase

  • Loading branch information...
jeremyharris committed Jun 14, 2016
1 parent 61f83c0 commit 8de073aff6399e4f86e709866159013c122ecaa3
@@ -0,0 +1,50 @@
<?php
namespace Cake\TestSuite\Constraint;
use Cake\Event\EventManager;
use PHPUnit_Framework_Constraint;
/**
* EventFired constraint
*/
class EventFired extends PHPUnit_Framework_Constraint
{
/**
* Array of fired events
*
* @var EventManager
*/
protected $_eventManager;
/**
* Constructor
*
* @param EventManager $eventManager Event manager to check
*/
public function __construct($eventManager)
{
parent::__construct();
$this->_eventManager = $eventManager;
}
/**
* Checks if event is in fired array
*
* @param mixed $other Constraint check
* @return bool
*/
public function matches($other)
{
return $this->_eventManager->getEventList()->hasEvent($other);
}
/**
* Assertion message string
*
* @return string
*/
public function toString()
{
return 'was fired';
}
}
@@ -0,0 +1,101 @@
<?php
namespace Cake\TestSuite\Constraint;
use Cake\Event\Event;
use Cake\Event\EventManager;
use PHPUnit_Framework_AssertionFailedError;
use PHPUnit_Framework_Constraint;
/**
* EventFiredWith constraint
*
* Another glorified in_array check
*/
class EventFiredWith extends PHPUnit_Framework_Constraint
{
/**
* Array of fired events
*
* @var EventManager
*/
protected $_eventManager;
/**
* Event data key
*
* @var string
*/
protected $_dataKey;
/**
* Event data value
*
* @var string
*/
protected $_dataValue;
/**
* Constructor
*
* @param EventManager $eventManager Event manager to check
* @param string $dataKey Data key
* @param string $dataValue Data value
*/
public function __construct($eventManager, $dataKey, $dataValue)
{
parent::__construct();
$this->_eventManager = $eventManager;
$this->_dataKey = $dataKey;
$this->_dataValue = $dataValue;
}
/**
* Checks if event is in fired array
*
* @param mixed $other Constraint check
* @return bool
*/
public function matches($other)
{
$firedEvents = [];
$list = $this->_eventManager->getEventList();
$totalEvents = count($list);
for ($e = 0; $e < $totalEvents; $e++) {
$firedEvents[] = $list[$e];
}
$eventGroup = collection($firedEvents)
->groupBy(function (Event $event) {
return $event->name();
})
->toArray();
if (!array_key_exists($other, $eventGroup)) {
return false;
}
$events = $eventGroup[$other];
if (count($events) > 1) {
throw new PHPUnit_Framework_AssertionFailedError(sprintf('Event "%s" was fired %d times, cannot make data assertion', $other, count($events)));
}
$event = $events[0];
if (array_key_exists($this->_dataKey, $event->data) === false) {
return false;
}
return $event->data[$this->_dataKey] === $this->_dataValue;
}
/**
* Assertion message string
*
* @return string
*/
public function toString()
{
return 'was fired with ' . $this->_dataKey . ' matching ' . (string)$this->_dataValue;
}
}
@@ -140,6 +140,42 @@ public function loadFixtures()
}
}
/**
* Asserts that a global event was fired. You must track events in your event manager for this assertion to work
*
* @param string $name Event name
* @param EventManager $eventManager Event manager to check, defaults to global event manager
* @param string $message Assertion failure message
* @return void
*/
public function assertEventFired($name, $eventManager = null, $message = '')
{
if (!$eventManager) {
$eventManager = EventManager::instance();
}
$this->assertThat($name, new Constraint\EventFired($eventManager), $message);
}
/**
* Asserts an event was fired with data
*
* If a third argument is passed, that value is used to compare with the value in $dataKey
*
* @param string $name Event name
* @param string $dataKey Data key
* @param string $dataValue Data value
* @param EventManager $eventManager Event manager to check, defaults to global event manager
* @param string $message Assertion failure message
* @return void
*/
public function assertEventFiredWith($name, $dataKey, $dataValue, $eventManager = null, $message = '')
{
if (!$eventManager) {
$eventManager = EventManager::instance();
}
$this->assertThat($name, new Constraint\EventFiredWith($eventManager, $dataKey, $dataValue), $message);
}
/**
* Assert text equality, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
@@ -0,0 +1,39 @@
<?php
namespace Cake\Test\TestCase\TestSuite\Constraint;
use Cake\Event\Event;
use Cake\Event\EventList;
use Cake\Event\EventManager;
use Cake\TestSuite\Constraint\EventFired;
use Cake\TestSuite\TestCase;
/**
* EventFired Test
*/
class EventFiredTest extends TestCase
{
/**
* tests EventFired constraint
*
* @return void
*/
public function testMatches()
{
$manager = EventManager::instance();
$manager->setEventList(new EventList());
$manager->trackEvents(true);
$myEvent = new Event('my.event', $this, []);
$myOtherEvent = new Event('my.other.event', $this, []);
$manager->getEventList()->add($myEvent);
$manager->getEventList()->add($myOtherEvent);
$constraint = new EventFired($manager);
$this->assertTrue($constraint->matches('my.event'));
$this->assertTrue($constraint->matches('my.other.event'));
$this->assertFalse($constraint->matches('event.not.fired'));
}
}
@@ -0,0 +1,72 @@
<?php
namespace Cake\Test\TestCase\TestSuite\Constraint;
use Cake\Event\Event;
use Cake\Event\EventList;
use Cake\Event\EventManager;
use Cake\TestSuite\Constraint\EventFiredWith;
use Cake\TestSuite\TestCase;
/**
* EventFiredWith Test
*/
class EventFiredWithTest extends TestCase
{
/**
* tests EventFiredWith constraint
*
* @return void
*/
public function testMatches()
{
$manager = EventManager::instance();
$manager->setEventList(new EventList());
$manager->trackEvents(true);
$myEvent = new Event('my.event', $this, [
'key' => 'value'
]);
$myOtherEvent = new Event('my.other.event', $this, [
'key' => null
]);
$manager->getEventList()->add($myEvent);
$manager->getEventList()->add($myOtherEvent);
$constraint = new EventFiredWith($manager, 'key', 'value');
$this->assertTrue($constraint->matches('my.event'));
$this->assertFalse($constraint->matches('my.other.event'));
$this->assertFalse($constraint->matches('event.not.fired'));
$constraint = new EventFiredWith($manager, 'key', null);
$this->assertTrue($constraint->matches('my.other.event'));
$this->assertFalse($constraint->matches('my.event'));
}
/**
* tests trying to assert data key=>value when an event is fired multiple times
*
* @return void
* @expectedException \PHPUnit_Framework_AssertionFailedError
*/
public function testMatchesInvalid()
{
$manager = EventManager::instance();
$manager->setEventList(new EventList());
$manager->trackEvents(true);
$myEvent = new Event('my.event', $this, [
'key' => 'value'
]);
$manager->getEventList()->add($myEvent);
$manager->getEventList()->add($myEvent);
$constraint = new EventFiredWith($manager, 'key', 'value');
$constraint->matches('my.event');
}
}
@@ -17,6 +17,9 @@
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
use Cake\Event\Event;
use Cake\Event\EventList;
use Cake\Event\EventManager;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
@@ -45,6 +48,58 @@ public static function defaultConnectionName()
class TestCaseTest extends TestCase
{
/**
* tests assertEventFiredWith
*
* @return void
*/
public function testEventFiredWith()
{
$manager = EventManager::instance();
$manager->setEventList(new EventList());
$manager->trackEvents(true);
$event = new Event('my.event', $this, [
'some' => 'data'
]);
$manager->dispatch($event);
$this->assertEventFiredWith('my.event', 'some', 'data');
$manager = new EventManager();
$manager->setEventList(new EventList());
$manager->trackEvents(true);
$event = new Event('my.event', $this, [
'other' => 'data'
]);
$manager->dispatch($event);
$this->assertEventFiredWith('my.event', 'other', 'data', $manager);
}
/**
* tests assertEventFired
*
* @return void
*/
public function testEventFired()
{
$manager = EventManager::instance();
$manager->setEventList(new EventList());
$manager->trackEvents(true);
$event = new Event('my.event');
$manager->dispatch($event);
$this->assertEventFired('my.event');
$manager = new EventManager();
$manager->setEventList(new EventList());
$manager->trackEvents(true);
$event = new Event('my.event');
$manager->dispatch($event);
$this->assertEventFired('my.event', $manager);
}
/**
* testAssertHtml
*

0 comments on commit 8de073a

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