|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Part of CI PHPUnit Test |
|
4
|
|
|
* |
|
5
|
|
|
* @author Kenji Suzuki <https://github.com/kenjis> |
|
6
|
|
|
* @license MIT License |
|
7
|
|
|
* @copyright 2015 Kenji Suzuki |
|
8
|
|
|
* @link https://github.com/kenjis/ci-phpunit-test |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
class CIPHPUnitTestSuperGlobal |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
public static function set_Global($name, $value) |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
$GLOBALS[$name] = $value; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function set_POST($params) |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
if (is_array($params)) |
|
21
|
|
|
{ |
|
22
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') |
|
23
|
|
|
{ |
|
24
|
|
|
$_POST = $params; |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function set_GET(&$argv, $params) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
if (is_string($argv)) |
|
32
|
|
|
{ |
|
33
|
|
|
$query_string = $this->getQueryString($argv); |
|
34
|
|
|
if ($query_string !== null) |
|
35
|
|
|
{ |
|
36
|
|
|
// Set $_GET if URI string has query string |
|
37
|
|
|
parse_str($query_string, $_GET); |
|
38
|
|
|
// Remove query string from URI string |
|
39
|
|
|
$argv = substr($argv, 0, -strlen($query_string)-1); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (is_array($params)) |
|
44
|
|
|
{ |
|
45
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') |
|
46
|
|
|
{ |
|
47
|
|
|
// if GET params are passed, overwrite $_GET |
|
48
|
|
|
if ($params !== []) |
|
49
|
|
|
{ |
|
50
|
|
|
$_GET = $params; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function set_SERVER_REQUEST_URI($argv) |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
$path = ''; |
|
59
|
|
|
if (is_string($argv)) |
|
60
|
|
|
{ |
|
61
|
|
|
$path = $argv; |
|
62
|
|
|
} |
|
63
|
|
|
elseif (is_array($argv)) |
|
64
|
|
|
{ |
|
65
|
|
|
// Generate URI path from array of controller, method, arg, ... |
|
66
|
|
|
$path = implode('/', $argv); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($_GET !== []) |
|
70
|
|
|
{ |
|
71
|
|
|
$_SERVER['REQUEST_URI'] = |
|
72
|
|
|
'/' . $path . '?' |
|
73
|
|
|
. http_build_query($_GET); |
|
74
|
|
|
} |
|
75
|
|
|
else |
|
76
|
|
|
{ |
|
77
|
|
|
$_SERVER['REQUEST_URI'] = '/' . $path; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Parse URI string and Get query string |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $uri |
|
85
|
|
|
* @return string|null |
|
86
|
|
|
* @throws LogicException |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getQueryString($uri) |
|
89
|
|
|
{ |
|
90
|
|
|
$query_string = parse_url('http://localhost/'.$uri, PHP_URL_QUERY); |
|
91
|
|
|
|
|
92
|
|
|
if ($query_string === false) |
|
93
|
|
|
{ |
|
94
|
|
|
throw new LogicException('Bad URI string: ' . $uri); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $query_string; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set HTTP request header to $_SERVER |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $name header name |
|
104
|
|
|
* @param string $value value |
|
105
|
|
|
*/ |
|
106
|
|
|
public function set_SERVER_HttpHeader($name, $value) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
$normalized_name = str_replace('-', '_', strtoupper($name)); |
|
109
|
|
|
|
|
110
|
|
|
if ( |
|
111
|
|
|
$normalized_name === 'CONTENT_LENGTH' |
|
112
|
|
|
|| $normalized_name === 'CONTENT_TYPE' |
|
113
|
|
|
) |
|
114
|
|
|
{ |
|
115
|
|
|
$key = $normalized_name; |
|
116
|
|
|
} |
|
117
|
|
|
else |
|
118
|
|
|
{ |
|
119
|
|
|
$key = 'HTTP_' . $normalized_name; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$_SERVER[$key] = $value; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.