Skip to content

Commit

Permalink
abstracting the tests with prep for future addition of real / mocked …
Browse files Browse the repository at this point in the history
…MVC objects
  • Loading branch information
dogmatic69 committed Sep 29, 2012
1 parent 8efb67a commit 8bab29f
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 47 deletions.
12 changes: 6 additions & 6 deletions Core/Assets/Test/Case/Lib/AssetsEventsTest.php
Expand Up @@ -5,17 +5,17 @@ class AssetsEventsTest extends InfinitasEventTestCase {
*@brief test required helpers load correctly
*/
public function testRequireHelpers() {
$this->Object->params = array();
$this->ViewObject->params = array();
$expected = array('requireHelpersToLoad' => array('Assets' => array('Assets.Compress')));
$result = $this->Event->trigger($this->Object, 'Assets.requireHelpersToLoad');
$result = $this->Event->trigger($this->ViewObject, 'Assets.requireHelpersToLoad');
$this->assertEquals($expected, $result);
}

/**
* @brief test required js loads correctly
*/
public function testRequireJavascript() {
$this->Object->params = array();
$this->ViewObject->params = array();
$expected = array('requireJavascriptToLoad' => array('Assets' => array(
'Assets.3rd/jquery',
'Assets.3rd/jquery_ui',
Expand All @@ -28,10 +28,10 @@ public function testRequireJavascript() {
'Assets.3rd/rater',
'Assets.3rd/moving_boxes'
)));
$result = $this->Event->trigger($this->Object, 'Assets.requireJavascriptToLoad');
$result = $this->Event->trigger($this->ViewObject, 'Assets.requireJavascriptToLoad');
$this->assertEquals($expected, $result);

$this->Object->params = array(
$this->ViewObject->params = array(
'admin' => true
);
$expected = array('requireJavascriptToLoad' => array('Assets' => array(
Expand All @@ -46,7 +46,7 @@ public function testRequireJavascript() {
'Assets.3rd/date',
'Assets.3rd/image_drop_down'
)));
$result = $this->Event->trigger($this->Object, 'Assets.requireJavascriptToLoad');
$result = $this->Event->trigger($this->ViewObject, 'Assets.requireJavascriptToLoad');
$this->assertEquals($expected, $result);
}
}
62 changes: 31 additions & 31 deletions Core/Events/Lib/AppEvents.php
@@ -1,23 +1,23 @@
<?php
/**
* Base class for events.
*
*
* This is the base class for events that can provide some documentation about
* what events are available and what they do. It extends Object so you can
* do things like $this->log(), redirect() and other basic methods
*
*
* Copyright (c) 2010 Carl Sutton ( dogmatic69 )
*
*
* @filesource
* @copyright Copyright (c) 2010 Carl Sutton ( dogmatic69 )
* @link http://www.infinitas-cms.org
* @package infinitas.events
* @subpackage infinitas.events.app_events
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @since 0.8
*
*
* @author dogmatic69
*
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*/
Expand All @@ -35,7 +35,7 @@ public function __construct() {
if(!empty($this->__events)) {
return true;
}

$this->__events = get_class_methods('AppEvents');
foreach($this->__events as $k => $event) {
if(substr($event, 0, 2) != 'on') {
Expand All @@ -54,7 +54,7 @@ public function __construct() {
final public function availableEvents() {
return $this->__events;
}

/**
* test if things are working
*/
Expand Down Expand Up @@ -82,7 +82,7 @@ public function onRequireLibs() {}
* They will be loaded when 'AppController' is included, before it runs.
*/
public function onLoadAppController() {}


/**
* Load up some of your own 'AppModels' so that you can create a base
Expand All @@ -99,12 +99,12 @@ public function onLoadAppModel() {}
* Add database connections from your plugins with this trigger.
* Its called in appModel before anything is created, even before default
* (which is a reserved value)
*
*
* Event should return in the format 'name' => array('configs')
*
* @param object $event
*/
public function onRequireDatabaseConfigs($event) {}
public function onRequireDatabaseConfigs(Event $Event) {}

/**
* Load the default cache settings.
Expand All @@ -116,22 +116,22 @@ public function onRequireDatabaseConfigs($event) {}
*
* @return true
*/
public function onSetupCache($event, $data = null) {}
public function onSetupCache(Event $Event, $data = null) {}

public function onSetupConfigStart($event, $data = null) {}
public function onSetupConfigEnd($event, $data = null) {}
public function onSetupConfigStart(Event $Event, $data = null) {}

public function onSetupConfigEnd(Event $Event, $data = null) {}

/**
* Adding routes
*
* Add routing to for the app from your plugin by calling Router::connect
* This should be used for routes that will not change if your plugin
* has routes that can be configured they should be in the database.
*
*
* @return nothing, it wont do anything
*/
public function onSetupRoutes($event, $data = null) {}
public function onSetupRoutes(Event $Event, $data = null) {}

/**
* parse extensions
Expand All @@ -140,7 +140,7 @@ public function onSetupRoutes($event, $data = null) {}
*
* @return array of extensions that should be registered.
*/
public function onSetupExtensions($event) {}
public function onSetupExtensions(Event $Event) {}

/**
* @brief called when editing a row is canceled
Expand All @@ -152,12 +152,12 @@ public function onSetupExtensions($event) {}
* @param Event $event the event being called
* @param string $id
*/
public function onEditCanceled($event, $id) {}
public function onEditCanceled(Event $Event, $id) {}

/**
*
*/
public function onAttachBehaviors($event = null) {}
public function onAttachBehaviors(Event $Event = null) {}



Expand Down Expand Up @@ -191,7 +191,7 @@ public function onAttachBehaviors($event = null) {}
* you can use {{templates.foo.phone}} in your views to show your phone
* number.
*/
public function onRequireGlobalTemplates($event) {}
public function onRequireGlobalTemplates(Event $Event) {}

/**
* @brief used to build the menus for the admin pages
Expand All @@ -208,7 +208,7 @@ public function onAdminMenu() {}
*
* @return mixed string | array like you would use in Controller->helpers
*/
public function onRequireHelpersToLoad($event = null) {
public function onRequireHelpersToLoad(Event $Event = null) {
return array();
}

Expand All @@ -217,13 +217,13 @@ public function onRequireHelpersToLoad($event = null) {
*
* Allows you to include javascript from your plugin that can be loaded
* on any page
*
*
* called in AppController::beforeRender()
*
* @param $event some data for the current event
* @return mixed string | array() of javascript like HtmlHelper::script() takes
*/
public function onRequireJavascriptToLoad($event, $data = null) {}
public function onRequireJavascriptToLoad(Event $Event, $data = null) {}

/**
* Get vcss files to include
Expand All @@ -236,7 +236,7 @@ public function onRequireJavascriptToLoad($event, $data = null) {}
* @param $event some data for the current event
* @return mixed string | array() of css like HtmlHelper::css() takes
*/
public function onRequireCssToLoad($event, $data = null) {}
public function onRequireCssToLoad(Event $Event, $data = null) {}

/**
* Load components
Expand All @@ -249,7 +249,7 @@ public function onRequireCssToLoad($event, $data = null) {}
* @param $event some data for the current event
* @return mixed string | array() of css like HtmlHelper::css() takes
*/
public function onRequireComponentsToLoad($event = null) {
public function onRequireComponentsToLoad(Event $Event = null) {
return array();
}

Expand All @@ -261,11 +261,11 @@ public function onRequireComponentsToLoad($event = null) {
* normally in the form of Model::find(first) that you can use.
*
*/
public function onSlugUrl($event, $data = null, $type = null) {
public function onSlugUrl(Event $Event, $data = null, $type = null) {
if(empty($data) || $type == null) {
return false;
}

$plugin = str_replace('Events', '', get_class($this));
$data = Set::flatten($data);

Expand Down Expand Up @@ -297,19 +297,19 @@ public function onSlugUrl($event, $data = null, $type = null) {
* dependancies or configs, new records like pending comments etc.
*
* @code
* // format should be
* // format should be
* $return[0]['name'] = 'something';
* $return[0]['type'] = 'warning|error';
* $return[0]['url'] = array();
* @endcode
*/
public function onRequireTodoList($event) {}
public function onRequireTodoList(Event $Event) {}

/**
* Last event to fire.
*
* Called after everything is done and finished. should not really be used
* for output unless in debug mode.
* for output unless in debug mode.
*/
public function onRequestDone() {}

Expand All @@ -324,7 +324,7 @@ public function onRequestDone() {}
*
* @access public
*/
public function onRunCrons($event) {
public function onRunCrons(Event $Event) {
return false;
}
}
10 changes: 5 additions & 5 deletions Core/Events/Lib/EventCore.php
Expand Up @@ -45,7 +45,7 @@ class EventCore {
private $__availablePlugins = array();

private $__installedPlugins = array();

private $__pluginsMap = array(
'testEvent' => 'all',
'setupRoutes' => 'all',
Expand Down Expand Up @@ -203,7 +203,7 @@ protected function _loadEventHandlers() {

Cache::write('event_handlers', $this->_eventHandlerCache, 'core');
}

public static function loadEventHandler($plugin) {
$_this = self::getInstance();
$filename = App::pluginPath($plugin) . 'Lib' . DS . $plugin . 'Events.php';
Expand Down Expand Up @@ -232,12 +232,12 @@ static protected function _dispatchEvent(&$HandlerObject, $scope, $eventName, $d
if(isset($_this->_eventHandlerCache[$eventName])) {
foreach($_this->_eventHandlerCache[$eventName] as $eventClass) {
$pluginName = EventCore::_extractPluginName($eventClass);

$pluginType = 'loaded';
if(isset($_this->__pluginsMap[$eventName])) {
$pluginType = $_this->__pluginsMap[$eventName];
}

if(!in_array(Inflector::camelize($pluginName), (array)InfinitasPlugin::listPlugins($pluginType))) {
continue;
}
Expand Down Expand Up @@ -399,7 +399,7 @@ class Event {
* @param string $eventName Name of the Event
* @param array $data optional array with k/v data
*/
public function __construct($eventName, &$HandlerObject, $pluginName) {
public function __construct($eventName, $HandlerObject, $pluginName) {
$this->name = $eventName;
$this->Handler = $HandlerObject;
$this->plugin = $pluginName;
Expand Down
64 changes: 59 additions & 5 deletions Core/Events/Test/Lib/InfinitasEventTestCase.php
Expand Up @@ -5,18 +5,52 @@
class InfinitasEventTestCase extends CakeTestCase {
/**
* @brief set up required objects for testing
*
* @todo setup MVC objects to pass in the correct places
*/
public function startTest() {
$this->Object = new Object();
public function setUp() {
parent::setUp();
$this->_setPlugin();

$this->ModelObject = $this->ViewObject = $this->ControllerObject = new Object();

$this->ObjectEvent = new Event('TestEvent', new Object(), $this->plugin);
$this->ModelEvent = new Event('ModelEvent', $this->ModelObject, $this->plugin);
$this->ViewtEvent = new Event('ViewtEvent', $this->ViewObject, $this->plugin);
$this->ControllerEvent = new Event('ControllerEvent', $this->ControllerObject, $this->plugin);

$this->Event = EventCore::getInstance();

$this->_loadEventClass();
}

/**
* @brief clear up the enviroment
*/
public function endTest() {
unset($this->Event, $this->Object);
ClassRegistry::flush();
public function tearDown() {
parent::tearDown();
unset($this->Event, $this->EventClass);
unset($this->ObjectEvent, $this->ControllerEvent, $this->ModelEvent, $this->ViewtEvent);
unset($this->ModelObject, $this->ViewObject, $this->ControllerObject);
}

/**
* @brief setup the current plugin name
*/
protected function _setPlugin() {
if(!isset($this->plugin)) {
$this->plugin = str_replace('EventsTest', '', get_class($this));
}
}

/**
* @brief load and get an instance of the event class being tested
*/
protected function _loadEventClass() {
//App::uses($this->plugin . 'Events', $this->plugin . '.Lib');

$this->EventClass = $this->plugin . 'Events';
$this->EventClass = new $this->EventClass();
}

/**
Expand All @@ -25,4 +59,24 @@ public function endTest() {
public function testInstance() {
$this->assertInstanceOf('EventCore', $this->Event);
}

/**
* @brief test getting the plugins details
*/
public function testPluginRollCall() {
$expected = array('pluginRollCall' => array($this->EventClass->onPluginRollCall()));
$expected['pluginRollCall'] = array_filter($expected['pluginRollCall']);
$result = $this->Event->trigger(new Object(), $this->plugin . '.pluginRollCall');
$this->assertEquals($expected, $result);
}

/**
* @brief test getting additional db configs
*/
public function testRequireDatabaseConfigs() {
$expected = array('requireDatabaseConfigs' => array($this->EventClass->onRequireDatabaseConfigs($this->ModelEvent)));
$expected['requireDatabaseConfigs'] = array_filter($expected['requireDatabaseConfigs']);
$result = $this->Event->trigger(new Object(), $this->plugin . '.requireDatabaseConfigs');
$this->assertEquals($expected, $result);
}
}

0 comments on commit 8bab29f

Please sign in to comment.