/** * Creates and instance of the mock JApplicationWeb object. * * The test can implement the following overrides: * - mockAppendBody * - mockGetBody * - mockPrepentBody * - mockSetBody * * If any *Body methods are implemented in the test class, all should be implemented otherwise behaviour will be unreliable. * * @param TestCase $test A test object. * @param array $options A set of options to configure the mock. * * @return PHPUnit_Framework_MockObject_MockObject * * @since 3.2 */ public static function create($test, $options = array()) { // Set expected server variables. if (!isset($_SERVER['HTTP_HOST'])) { $_SERVER['HTTP_HOST'] = 'localhost'; } // Collect all the relevant methods in JApplicationCms (work in progress). $methods = array('allowCache', 'appendBody', 'clearHeaders', 'close', 'execute', 'get', 'getBody', 'getDocument', 'getHeaders', 'getIdentity', 'getLanguage', 'getMenu', 'getPathway', 'getSession', 'getTemplate', 'initialiseApp', 'isAdmin', 'loadConfiguration', 'loadDispatcher', 'loadDocument', 'loadIdentity', 'loadLanguage', 'loadSession', 'prependBody', 'redirect', 'registerEvent', 'sendHeaders', 'set', 'setBody', 'setHeader', 'triggerEvent'); // Create the mock. $mockObject = $test->getMock('JApplicationCms', $methods, array(), '', true); // Mock calls to JApplicationWeb::getDocument(). $mockObject->expects($test->any())->method('getDocument')->will($test->returnValue(TestMockDocument::create($test))); // Mock calls to JApplicationWeb::getLanguage(). $mockObject->expects($test->any())->method('getLanguage')->will($test->returnValue(TestMockLanguage::create($test))); // Mock a call to JApplicationWeb::getSession(). if (isset($options['session'])) { $mockObject->expects($test->any())->method('getSession')->will($test->returnValue($options['session'])); } else { $mockObject->expects($test->any())->method('getSession')->will($test->returnValue(TestMockSession::create($test))); } $test->assignMockCallbacks($mockObject, array('appendBody' => array(is_callable(array($test, 'mockAppendBody')) ? $test : get_called_class(), 'mockAppendBody'), 'getBody' => array(is_callable(array($test, 'mockGetBody')) ? $test : get_called_class(), 'mockGetBody'), 'prependBody' => array(is_callable(array($test, 'mockPrependBody')) ? $test : get_called_class(), 'mockPrependBody'), 'setBody' => array(is_callable(array($test, 'mockSetBody')) ? $test : get_called_class(), 'mockSetBody'), 'getHeaders' => array(is_callable(array($test, 'mockGetHeaders')) ? $test : get_called_class(), 'mockGetHeaders'), 'setHeader' => array(is_callable(array($test, 'mockSetHeader')) ? $test : get_called_class(), 'mockSetHeader'), 'clearHeaders' => array(is_callable(array($test, 'mockClearHeaders')) ? $test : get_called_class(), 'mockClearHeaders'), 'allowCache' => array(is_callable(array($test, 'mockAllowCache')) ? $test : get_called_class(), 'mockAllowCache'))); // Reset the body storage. static::$body = array(); // Reset the headers storage. static::$headers = array(); // Reset the cache storage. static::$cachable = array(); return $mockObject; }
/** * Returns request body data, convert to object if content type is JSON * Gives you all request data if you pass `null` as $key * * @param string $key The name of the key requested * * @return mixed The request body data */ public static function data($key = null, $default = null) { if (null === static::$body) { $json = false !== stripos(empty($_SERVER['HTTP_CONTENT_TYPE']) ? '' : $_SERVER['HTTP_CONTENT_TYPE'], 'json') || false !== stripos(empty($_SERVER['CONTENT_TYPE']) ? '' : $_SERVER['CONTENT_TYPE'], 'json'); if ($json) { static::$body = json_decode(file_get_contents("php://input")); } else { if (empty($_POST)) { static::$body = file_get_contents("php://input"); } else { static::$body = (object) $_POST; } } } return $key ? isset(static::$body->{$key}) ? static::$body->{$key} : (is_callable($default) ? call_user_func($default) : $default) : static::$body; }
/** * Returns request body data, convert to object if content type is JSON * Gives you all request data if you pass `null` as $key * * @param string $key The name of the key requested * * @return mixed The request body data */ public static function data($key = null, $default = null) { if (null === static::$body) { $json = filter_input(INPUT_SERVER, 'HTTP_CONTENT_TYPE') == 'application/json' || filter_input(INPUT_SERVER, 'CONTENT_TYPE') == 'application/json'; static::$body = $json ? json_decode(file_get_contents("php://input")) : file_get_contents("php://input"); } return $key ? isset(static::$body->{$key}) ? static::$body->{$key} : (is_callable($default) ? call_user_func($default) : $default) : static::$body; }
/** * Returns the request body from POST requests for example * * @return array */ public static function body() { if (!is_null(static::$body)) { return static::$body; } return static::$body = file_get_contents('php://input'); }
private static function _reset() { static::$body = ''; static::$position = 0; }
/** * Mock JApplicationWeb->setBody method. * * @param string $content The body of the response. * * @return void * * @since 12.2 */ public static function mockSetBody($content) { static::$body = array($content); }
/** * Clears the body. */ public static function clear() { static::$body = ''; }
/** * Creates a response instance from an entire HTTP message including HTTP status headers and body. * * @param string $message The full HTTP message. * @param string $options Additionnal options. * @return object Returns a request instance. */ public static function parse($message, $options = []) { $parts = explode("\r\n\r\n", $message, 2); if (count($parts) < 2) { throw new NetException("The CRLFCRLF separator between headers and body is missing."); } $response = new static($options + ['format' => null]); list($header, $body) = $parts; $headers = str_replace("\r", '', explode("\n", $header)); $headers = array_filter($headers); preg_match('/HTTP\\/(\\d+\\.\\d+)\\s+(\\d+)(?:\\s+(.*))?/i', array_shift($headers), $matches); $response->headers->push($headers); if ($matches) { $response->version($matches[1]); $response->status([$matches[2], isset($matches[3]) ? $matches[3] : '']); } if ($response->headers['Transfer-Encoding']->value() === 'chunked') { $decoded = ''; while (!empty($body)) { $pos = strpos($body, "\r\n"); $len = hexdec(substr($body, 0, $pos)); $decoded .= substr($body, $pos + 2, $len); $body = substr($body, $pos + 2 + $len); } $body = $decoded; } $response->body($body); return $response; }