This repository has been archived by the owner on Nov 26, 2017. It is now read-only.
/
JSessionMock.php
140 lines (127 loc) · 2.89 KB
/
JSessionMock.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
134
135
136
137
138
139
140
<?php
/**
* @package Joomla.UnitTest
* @copyright Copyright (C) 2005 - 2012 Open Source Matters. All rights reserved.
* @license GNU General Public License
*/
/**
* Mock class for JSession.
*
* @package Joomla.UnitTest
* @since 11.3
*/
class JSessionGlobalMock
{
/**
* An array of options.
*
* @var array
* @since 11.3
*/
protected static $options = array();
/**
* Gets an option.
*
* @param string $name The name of the option.
* @param string $default The default value to use if the option is not found.
*
* @return mixed The value of the option, or the default if not found.
*
* @since 11.3
*/
public function getOption($name, $default = null)
{
return isset(self::$options[$name]) ? self::$options[$name] : $default;
}
/**
* Creates an instance of the mock JSession object.
*
* @param object $test A test object.
* @param array $options An array of optional configuration values.
* getId : the value to be returned by the mock getId method
* get.user.id : the value to assign to the user object id returned by get('user')
* get.user.name : the value to assign to the user object name returned by get('user')
* get.user.username : the value to assign to the user object username returned by get('user')
*
* @return object
*
* @since 11.3
*/
public static function create($test, $options = array())
{
if (is_array($options))
{
self::$options = $options;
}
// Mock all the public methods.
$methods = array(
'clear',
'close',
'destroy',
'fork',
'get',
'getExpire',
'getFormToken',
'getId',
'getInstance',
'getName',
'getState',
'getStores',
'getToken',
'has',
'hasToken',
'getPrefix',
'isNew',
'restart',
'set',
);
// Create the mock.
$mockObject = $test->getMock(
'JSession',
$methods,
// Constructor arguments.
array(),
// Mock class name.
'',
// Call original constructor.
false
);
// Mock selected methods.
$test->assignMockReturns(
$mockObject, array(
'getId' => self::getOption('getId')
)
);
$test->assignMockCallbacks(
$mockObject,
array(
'get' => array(get_called_class(), 'mockGet'),
)
);
return $mockObject;
}
/**
* Mocking the get method.
*
* @param string $key The key to get.
*
* @return mixed
*
* @since 11.3
*/
public function mockGet($key)
{
switch ($key)
{
case 'user':
include_once JPATH_PLATFORM . '/joomla/user/user.php';
$user = new JUser;
$user->id = (int) self::getOption('get.user.id', 0);
$user->name = self::getOption('get.user.name');
$user->username = self::getOption('get.user.username');
$user->guest = (int) self::getOption('get.user.guest', 1);
return $user;
}
return null;
}
}