-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
dispatcher.php
133 lines (118 loc) · 2.95 KB
/
dispatcher.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
128
129
130
131
132
133
<?php
/**
* @package Joomla.Test
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
use Joomla\Event\DispatcherInterface;
/**
* Class to mock DispatcherInterface.
*
* @package Joomla.Test
* @since 12.1
*/
class TestMockDispatcher
{
/**
* Keeps track of mock handlers.
*
* @var array
* @since 11.3
*/
public static $handlers = array();
/**
* Keeps track of triggers.
*
* @var array
* @since 11.3
*/
public static $triggered = array();
/**
* Creates and instance of the mock DispatcherInterface object.
*
* @param PHPUnit_Framework_TestCase $test A test object.
* @param boolean $defaults True to create the default mock handlers and triggers.
*
* @return PHPUnit_Framework_MockObject_MockObject
*
* @since 11.3
*/
public static function create($test, $defaults = true)
{
// Clear the static tracker properties.
self::$handlers = array();
self::$triggered = array();
// Collect all the relevant methods in DispatcherInterface.
$methods = array(
'addListener',
'dispatch',
'register',
'removeListener',
'trigger',
'test',
);
// Create the mock.
$mockObject = $test->getMockBuilder(DispatcherInterface::class)
->setMethods($methods)
->getMock();
// Mock selected methods.
$test->assignMockReturns(
$mockObject, array(
// An additional 'test' method for confirming this object is successfully mocked.
'test' => 'ok',
)
);
if ($defaults)
{
$test->assignMockCallbacks(
$mockObject,
array(
'dispatch' => array(get_called_class(), 'mockDispatch'),
'addListener' => array(get_called_class(), 'mockRegister'),
)
);
}
return $mockObject;
}
/**
* Callback for the DispatcherInterface register method.
*
* @param string $event Name of the event to register handler for.
* @param array $args An array of arguments.
*
* @return array An array of results from each function call.
*
* @since 11.3
*/
public static function mockDispatch($event, $args = [])
{
// Track the events that were triggered, in order.
self::$triggered[] = $event;
if (!empty(self::$handlers[$event]))
{
return self::$handlers[$event];
}
return array();
}
/**
* Callback for the DispatcherInterface register method.
*
* @param string $event Name of the event to register handler for.
* @param callable $handler Callback
* @param mixed $return The mock value to return for the given event handler.
*
* @return void
*
* @since 11.3
*/
public static function mockRegister($event, $handler, $return = null)
{
if (empty(self::$handlers[$event]))
{
self::$handlers[$event] = [];
}
$identifier = is_array($handler) ? md5(serialize($handler)) : spl_object_hash($handler);
self::$handlers[$event][$identifier] = $return;
}
}