Base::mock PHP Method

mock() public method

Mock HTTP request
public mock ( $pattern, array $args = NULL, array $headers = NULL, $body = NULL ) : mixed
$pattern string
$args array array
$headers array array
$body string
return mixed
    function mock($pattern, array $args = NULL, array $headers = NULL, $body = NULL)
    {
        if (!$args) {
            $args = [];
        }
        $types = ['sync', 'ajax', 'cli'];
        preg_match('/([\\|\\w]+)\\h+(?:@(\\w+)(?:(\\(.+?)\\))*|([^\\h]+))' . '(?:\\h+\\[(' . implode('|', $types) . ')\\])?/', $pattern, $parts);
        $verb = strtoupper($parts[1]);
        if ($parts[2]) {
            if (empty($this->hive['ALIASES'][$parts[2]])) {
                user_error(sprintf(self::E_Named, $parts[2]), E_USER_ERROR);
            }
            $parts[4] = $this->hive['ALIASES'][$parts[2]];
            $parts[4] = $this->build($parts[4], isset($parts[3]) ? $this->parse($parts[3]) : []);
        }
        if (empty($parts[4])) {
            user_error(sprintf(self::E_Pattern, $pattern), E_USER_ERROR);
        }
        $url = parse_url($parts[4]);
        parse_str(@$url['query'], $GLOBALS['_GET']);
        if (preg_match('/GET|HEAD/', $verb)) {
            $GLOBALS['_GET'] = array_merge($GLOBALS['_GET'], $args);
        }
        $GLOBALS['_POST'] = $verb == 'POST' ? $args : [];
        $GLOBALS['_REQUEST'] = array_merge($GLOBALS['_GET'], $GLOBALS['_POST']);
        foreach ($headers ?: [] as $key => $val) {
            $_SERVER['HTTP_' . strtr(strtoupper($key), '-', '_')] = $val;
        }
        $this->hive['VERB'] = $verb;
        $this->hive['PATH'] = $url['path'];
        $this->hive['URI'] = $this->hive['BASE'] . $url['path'];
        if ($GLOBALS['_GET']) {
            $this->hive['URI'] .= '?' . http_build_query($GLOBALS['_GET']);
        }
        $this->hive['BODY'] = '';
        if (!preg_match('/GET|HEAD/', $verb)) {
            $this->hive['BODY'] = $body ?: http_build_query($args);
        }
        $this->hive['AJAX'] = isset($parts[5]) && preg_match('/ajax/i', $parts[5]);
        $this->hive['CLI'] = isset($parts[5]) && preg_match('/cli/i', $parts[5]);
        return $this->run();
    }