kenjis /
ci-phpunit-test
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 |
||
|
0 ignored issues
–
show
|
|||
| 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_FILES(array $files) |
||
| 30 | { |
||
| 31 | if (count($files) > 1) |
||
| 32 | { |
||
| 33 | throw new LogicException('Invalid $_FILES: '.var_export($files, true)); |
||
| 34 | } |
||
| 35 | |||
| 36 | $field = array_keys($files)[0]; |
||
| 37 | $is_multiple = is_array($files[$field]['tmp_name']); |
||
| 38 | |||
| 39 | if ($is_multiple) |
||
| 40 | { |
||
| 41 | throw new LogicException('Cannot handle multiple file upload. $_FILES: '.var_export($files, true)); |
||
| 42 | } |
||
| 43 | |||
| 44 | // Handle single file |
||
| 45 | $file =& $files[$field]; |
||
| 46 | |||
| 47 | if (! file_exists($file['tmp_name'])) |
||
| 48 | { |
||
| 49 | $file['error'] = UPLOAD_ERR_NO_FILE; |
||
| 50 | $file['type'] = ''; |
||
| 51 | $file['size'] = 0; |
||
| 52 | |||
| 53 | $_FILES = $files; |
||
| 54 | return; |
||
| 55 | } |
||
| 56 | |||
| 57 | $file['size'] = filesize($file['tmp_name']); |
||
| 58 | |||
| 59 | $upload_max_filesize = $this->convertToBytes(ini_get('upload_max_filesize')); |
||
| 60 | if ($upload_max_filesize < $file['size']) |
||
| 61 | { |
||
| 62 | $file['error'] = UPLOAD_ERR_INI_SIZE; |
||
| 63 | |||
| 64 | $_FILES = $files; |
||
| 65 | return; |
||
| 66 | } |
||
| 67 | |||
| 68 | $file['error'] = UPLOAD_ERR_OK; |
||
| 69 | |||
| 70 | $_FILES = $files; |
||
| 71 | return; |
||
| 72 | } |
||
| 73 | |||
| 74 | private function convertToBytes($value) |
||
| 75 | { |
||
| 76 | if (is_numeric($value)) |
||
| 77 | { |
||
| 78 | return $value; |
||
| 79 | } |
||
| 80 | else |
||
| 81 | { |
||
| 82 | $value_length = strlen($value); |
||
| 83 | $number = substr($value, 0, $value_length - 1); |
||
| 84 | $unit = strtolower(substr($value, $value_length - 1)); |
||
| 85 | |||
| 86 | switch ($unit) |
||
| 87 | { |
||
| 88 | case 'k': |
||
| 89 | $number *= 1024; |
||
| 90 | break; |
||
| 91 | case 'm': |
||
| 92 | $number *= 1024 * 1024; |
||
| 93 | break; |
||
| 94 | case 'g': |
||
| 95 | $number *= 1024 * 1024 * 1024; |
||
| 96 | break; |
||
| 97 | } |
||
| 98 | |||
| 99 | return $number; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | public function set_GET(&$argv, $params) |
||
| 104 | { |
||
| 105 | if (is_string($argv)) |
||
| 106 | { |
||
| 107 | $query_string = $this->getQueryString($argv); |
||
| 108 | if ($query_string !== null) |
||
| 109 | { |
||
| 110 | // Set $_GET if URI string has query string |
||
| 111 | parse_str($query_string, $_GET); |
||
| 112 | // Remove query string from URI string |
||
| 113 | $argv = substr($argv, 0, -strlen($query_string)-1); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | if (is_array($params)) |
||
| 118 | { |
||
| 119 | if ($_SERVER['REQUEST_METHOD'] === 'GET') |
||
| 120 | { |
||
| 121 | // if GET params are passed, overwrite $_GET |
||
| 122 | if ($params !== []) |
||
| 123 | { |
||
| 124 | $_GET = $params; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | public function set_SERVER_REQUEST_URI($argv) |
||
| 131 | { |
||
| 132 | $path = ''; |
||
| 133 | if (is_string($argv)) |
||
| 134 | { |
||
| 135 | $path = $argv; |
||
| 136 | } |
||
| 137 | elseif (is_array($argv)) |
||
| 138 | { |
||
| 139 | // Generate URI path from array of controller, method, arg, ... |
||
| 140 | $path = implode('/', $argv); |
||
| 141 | } |
||
| 142 | |||
| 143 | if ($_GET !== []) |
||
| 144 | { |
||
| 145 | $_SERVER['REQUEST_URI'] = |
||
| 146 | '/' . $path . '?' |
||
| 147 | . http_build_query($_GET); |
||
| 148 | } |
||
| 149 | else |
||
| 150 | { |
||
| 151 | $_SERVER['REQUEST_URI'] = '/' . $path; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Parse URI string and Get query string |
||
| 157 | * |
||
| 158 | * @param string $uri |
||
| 159 | * @return string|null |
||
| 160 | * @throws LogicException |
||
| 161 | */ |
||
| 162 | protected function getQueryString($uri) |
||
| 163 | { |
||
| 164 | $query_string = parse_url('http://localhost/'.$uri, PHP_URL_QUERY); |
||
| 165 | |||
| 166 | if ($query_string === false) |
||
| 167 | { |
||
| 168 | throw new LogicException('Bad URI string: ' . $uri); |
||
| 169 | } |
||
| 170 | |||
| 171 | return $query_string; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Set HTTP request header to $_SERVER |
||
| 176 | * |
||
| 177 | * @param string $name header name |
||
| 178 | * @param string $value value |
||
| 179 | */ |
||
| 180 | public function set_SERVER_HttpHeader($name, $value) |
||
| 181 | { |
||
| 182 | $normalized_name = str_replace('-', '_', strtoupper($name)); |
||
| 183 | |||
| 184 | if ( |
||
| 185 | $normalized_name === 'CONTENT_LENGTH' |
||
| 186 | || $normalized_name === 'CONTENT_TYPE' |
||
| 187 | ) |
||
| 188 | { |
||
| 189 | $key = $normalized_name; |
||
| 190 | } |
||
| 191 | else |
||
| 192 | { |
||
| 193 | $key = 'HTTP_' . $normalized_name; |
||
| 194 | } |
||
| 195 | |||
| 196 | $_SERVER[$key] = $value; |
||
| 197 | } |
||
| 198 | } |
||
| 199 |
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.