-
Notifications
You must be signed in to change notification settings - Fork 36
/
InfinitasEventTestCase.php
127 lines (107 loc) · 3.61 KB
/
InfinitasEventTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* @brief base test class for testing Events
*/
class InfinitasEventTestCase extends CakeTestCase {
/**
* @brief set up required objects for testing
*
* @todo setup MVC objects to pass in the correct places
*/
public function setUp() {
parent::setUp();
$this->_setPlugin();
$this->ObjectObject = $this->ModelObject = $this->ViewObject = $this->ControllerObject = new Object();
$this->ObjectEvent = new Event('TestEvent', $this->ObjectObject, $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);
EventCore::loadEventHandler($this->plugin);
$this->Event = EventCore::getInstance();
$this->_loadEventClass();
}
/**
* @brief clear up the enviroment
*/
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();
}
/**
* @brief manuall call an event
*
* This will manually call an event to get the return, for auto testing of triggers
* that do not use complex checks, just standard returns.
*
* @param string $event the name of the event
* @param Event $object the event object that would be passed
*
* @return array
*/
protected function _manualCall($event, $object = null) {
$method = 'on' . ucfirst($event);
$expected = array($event => array(call_user_func_array(array($this->EventClass, $method), array($object))));
$return = current(array_filter($expected[$event]));
$expected[$event] = array();
if($return) {
$expected[$event] = array($this->plugin => $return);
}
return $expected;
}
/**
* @brief test the instance is loaded correctly
*/
public function testInstance() {
$this->assertInstanceOf('EventCore', $this->Event);
}
/**
* @brief test getting the plugins details
*/
public function testPluginRollCall() {
$expected = $this->_manualCall('pluginRollCall');
$result = $this->Event->trigger($this->ObjectObject, $this->plugin . '.pluginRollCall');
$this->assertEquals($expected, $result);
}
/**
* @brief test getting additional db configs
*/
public function testRequireDatabaseConfigs() {
$expected = $this->_manualCall('requireDatabaseConfigs', $this->ObjectEvent);
$result = $this->Event->trigger($this->ModelObject, $this->plugin . '.requireDatabaseConfigs');
$this->assertEquals($expected, $result);
}
/**
* @brief test getting the admin menu
*/
public function testAdminMenu() {
$expected = $this->_manualCall('adminMenu', $this->ObjectEvent);
$result = $this->Event->trigger($this->ViewObject, $this->plugin . '.adminMenu');
$this->assertEquals($expected, $result);
}
/**
*@brief test required helpers load correctly
*/
public function testRequireHelpers() {
$expected = $this->_manualCall('requireHelpersToLoad', $this->ViewtEvent);
$result = $this->Event->trigger($this->ViewObject, $this->plugin . '.requireHelpersToLoad');
$this->assertEquals($expected, $result);
}
}