作者:bigpoin
项目:slim-bootstra
public function testWritePlain()
{
$this->_jsonTestOutputWriter->expects($this->exactly(1))->method('_jsonEncode')->with($this->equalTo(array('mockKey' => 'mockValue')))->will($this->returnValue('{"mockKey": "mockValue"}'));
$this->_mockHeaders->expects($this->exactly(1))->method('set')->with($this->equalTo('Content-Type'), $this->equalTo('application/json; charset=UTF-8'));
$this->_mockResponse->expects($this->exactly(1))->method('setStatus')->with($this->equalTo(200));
$this->_mockResponse->expects($this->exactly(1))->method('setBody')->with($this->equalTo('{"mockKey": "mockValue"}'));
$this->_jsonTestOutputWriter->writePlain(array('mockKey' => 'mockValue'));
}
作者:bigpoin
项目:slim-bootstra
/**
* @param $data
*
* @dataProvider normalizeAllDataProvider
*/
public function testWriteArray($data)
{
$localCsvTestOutputWriter = $this->getMock('\\SlimBootstrap\\ResponseOutputWriter\\Csv', array('_csvEncode'), array($this->_mockRequest, $this->_mockResponse, $this->_mockHeaders, 'mockShortName'));
$this->_mockHeaders->expects($this->once())->method('set')->with($this->identicalTo("Content-Type"), $this->identicalTo("text/csv; charset=UTF-8"));
$this->_mockResponse->expects($this->once())->method('setStatus')->with($this->equalTo(200));
$localCsvTestOutputWriter->expects($this->once())->method('_csvEncode');
$this->_mockResponse->expects($this->once())->method('setBody');
$localCsvTestOutputWriter->write($data, 200);
}
作者:davidepastor
项目:slim-restrict-rout
/**
* Run before each test.
*/
public function setUp()
{
$uri = Uri::createFromString('https://example.com:443/foo/bar');
$headers = new Headers();
$headers->set('REMOTE_ADDR', '127.0.0.1');
$cookies = [];
$env = Environment::mock();
$serverParams = $env->all();
$body = new Body(fopen('php://temp', 'r+'));
$this->request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$this->response = new Response();
}
作者:DavidePastor
项目:Slim-Validatio
/**
* Setup for the XML POST requests.
*
* @param string $xml The XML to use to mock the body of the request.
*/
public function setUpXmlPost($xml)
{
$uri = Uri::createFromString('https://example.com:443/foo');
$headers = new Headers();
$headers->set('Content-Type', 'application/xml;charset=utf8');
$cookies = [];
$env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'POST']);
$serverParams = $env->all();
$body = new RequestBody();
$body->write($xml);
$this->request = new Request('POST', $uri, $headers, $cookies, $serverParams, $body);
$this->response = new Response();
}
作者:there
项目:slim-test-helper
private function request($method, $path, $data = array(), $optionalHeaders = array())
{
//Make method uppercase
$method = strtoupper($method);
$options = array('REQUEST_METHOD' => $method, 'REQUEST_URI' => $path);
if ($method === 'GET') {
$options['QUERY_STRING'] = http_build_query($data);
} else {
$params = json_encode($data);
}
// Prepare a mock environment
$env = Environment::mock(array_merge($options, $optionalHeaders));
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = $this->cookies;
$serverParams = $env->all();
$body = new RequestBody();
// Attach JSON request
if (isset($params)) {
$headers->set('Content-Type', 'application/json;charset=utf8');
$body->write($params);
}
$this->request = new Request($method, $uri, $headers, $cookies, $serverParams, $body);
$response = new Response();
// Invoke request
$app = $this->app;
$this->response = $app($this->request, $response);
// Return the application output.
return (string) $this->response->getBody();
}
作者:martynbi
项目:slim3-controlle
protected function dispatch($path, $method = 'GET', $data = array(), $cookies = array())
{
$container = $this->app->getContainer();
// seperate the path from the query string so we can set in the environment
@(list($path, $queryString) = explode('?', $path));
// Prepare a mock environment
$env = Environment::mock(array('REQUEST_URI' => $path, 'REQUEST_METHOD' => $method, 'QUERY_STRING' => is_null($queryString) ? '' : $queryString));
// Prepare request and response objects
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = $cookies;
$serverParams = $env->all();
$body = new RequestBody();
// create request, and set params
$req = new $container['request']($method, $uri, $headers, $cookies, $serverParams, $body);
if (!empty($data)) {
$req = $req->withParsedBody($data);
}
$res = new $container['response']();
// // Fix for body, but breaks POST params in tests - http://stackoverflow.com/questions/34823328/response-getbody-is-empty-when-testing-slim-3-routes-with-phpunit
// $body = new RequestBody();
// if (!empty($data))
// $body->write(json_encode($data));
//
// // create request, and set params
// $req = new $container['request']($method, $uri, $headers, $cookies, $serverParams, $body);
// $res = new $container['response']();
$this->headers = $headers;
$this->request = $req;
$this->response = call_user_func_array($this->app, array($req, $res));
}
作者:konstantin-p
项目:Se
/**
* Constructor (private access)
*
* @param array|null $settings If present, these are used instead of global server variables
*/
private function __construct($settings = null)
{
if ($settings) {
$this->properties = $settings;
} else {
$env = array();
//The HTTP request method
$env['REQUEST_METHOD'] = $_SERVER['REQUEST_METHOD'];
//The IP
$env['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'];
// Server params
$scriptName = $_SERVER['SCRIPT_NAME'];
// <-- "/foo/index.php"
$requestUri = $_SERVER['REQUEST_URI'];
// <-- "/foo/bar?test=abc" or "/foo/index.php/bar?test=abc"
$queryString = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
// <-- "test=abc" or ""
// Physical path
if (strpos($requestUri, $scriptName) !== false) {
$physicalPath = $scriptName;
// <-- Without rewriting
} else {
$physicalPath = str_replace('\\', '', dirname($scriptName));
// <-- With rewriting
}
$env['SCRIPT_NAME'] = rtrim($physicalPath, '/');
// <-- Remove trailing slashes
$env['PATH_INFO'] = !empty($requestUri) && !empty($physicalPath) && strpos($requestUri, $physicalPath) === 0 ? substr_replace($requestUri, '', 0, strlen($physicalPath)) : $requestUri;
// <-- Remove physical path
$env['PATH_INFO'] = str_replace('?' . $queryString, '', $env['PATH_INFO']);
// <-- Remove query string
$env['PATH_INFO'] = '/' . ltrim($env['PATH_INFO'], '/');
// <-- Ensure leading slash
// Query string (without leading "?")
$env['QUERY_STRING'] = $queryString;
//Name of server host that is running the script
$env['SERVER_NAME'] = $_SERVER['SERVER_NAME'];
//Number of server port that is running the script
$env['SERVER_PORT'] = $_SERVER['SERVER_PORT'];
//HTTP request headers (retains HTTP_ prefix to match $_SERVER)
$headers = \Slim\Http\Headers::extract($_SERVER);
foreach ($headers as $key => $value) {
$env[$key] = $value;
}
//Is the application running under HTTPS or HTTP protocol?
$env['slim.url_scheme'] = empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off' ? 'http' : 'https';
//Input stream (readable one time only; not available for multipart/form-data requests)
$rawInput = @file_get_contents('php://input');
if (!$rawInput) {
$rawInput = '';
}
$env['slim.input'] = $rawInput;
//Error stream
$env['slim.errors'] = @fopen('php://stderr', 'w');
// print_r($env); die;
$this->properties = $env;
}
}
作者:zithe
项目:mem
protected function createRequest($env)
{
$method = $env["REQUEST_METHOD"];
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = Cookies::parseHeader($headers->get("Cookie", []));
$serverParams = $env->all();
$body = new Body(fopen("php://input", "r"));
return new Request($method, $uri, $headers, $cookies, $serverParams, $body);
}
作者:mbarqui
项目:reactphp-sli
/**
* Creates a new request object from the data of a reactPHP request object
*
* @param \React\Http\Request $request ReactPHP native request object
*
* @return \Slim\Http\Request
*/
public static function createFromReactRequest(\React\Http\Request $request)
{
$slimHeads = new Headers();
foreach ($request->getHeaders() as $reactHeadKey => $reactHead) {
$slimHeads->add($reactHeadKey, $reactHead);
if ($reactHeadKey === 'Host') {
$host = explode(':', $reactHead);
if (count($host) === 1) {
$host[1] = '80';
}
}
}
$slimUri = new Uri('http', $host[0], (int) $host[1], $request->getPath(), $request->getQuery());
$cookies = [];
$serverParams = $_SERVER;
$serverParams['SERVER_PROTOCOL'] = 'HTTP/' . $request->getHttpVersion();
$slimBody = new RequestBody();
return new self($request->getMethod(), $slimUri, $slimHeads, $cookies, $serverParams, $slimBody);
}
作者:ChrisBrento
项目:recipe
/**
* @param $method
* @param string $url
*
* @return Request
*/
public function makeRequest($method, $url = '/')
{
$env = Environment::mock();
$uri = Uri::createFromString('http://example.com' . $url);
$headers = Headers::createFromEnvironment($env);
$serverParams = $env->all();
$body = new RequestBody();
$uploadedFiles = UploadedFile::createFromEnvironment($env);
$request = new Request($method, $uri, $headers, [], $serverParams, $body, $uploadedFiles);
return $request;
}
作者:slimphp-ap
项目:slim-jso
public function requestFactory($acceptType = 'application/json')
{
$env = Environment::mock(['HTTP_ACCEPT' => $acceptType]);
$uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123');
$headers = Headers::createFromEnvironment($env);
$serverParams = $env->all();
$body = new RequestBody();
$uploadedFiles = UploadedFile::createFromEnvironment($env);
$request = new Request('GET', $uri, $headers, [], $serverParams, $body, $uploadedFiles);
return $request;
}
作者:zithe
项目:mem
protected function createRequest()
{
$env = (new Environment())->mock(["PATH_INFO" => "/index/hello/", "SCRIPT_NAME" => "/index.php"]);
$method = $env["REQUEST_METHOD"];
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = Cookies::parseHeader($headers->get("Cookie", []));
$serverParams = $env->all();
$body = new Body(fopen("php://input", "r"));
return new Request($method, $uri, $headers, $cookies, $serverParams, $body);
}
作者:gegglet
项目:sessionmiddlewar
public function requestFactory($queryString = '')
{
$env = Environment::mock();
$uri = Uri::createFromString('https://example.com:443/foo/bar' . $queryString);
$headers = Headers::createFromEnvironment($env);
$cookies = ['user' => 'john', 'id' => '123'];
$serverParams = $env->all();
$body = new RequestBody();
$uploadedFiles = UploadedFile::createFromEnvironment($env);
$request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles);
return $request;
}
作者:usf-i
项目:slim-skeleto
public function setRequest($method = 'GET', $uri = '/', $other = [])
{
// Prepare request and response objects
$base = ['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => $uri, 'REQUEST_METHOD' => $method];
$env = \Slim\Http\Environment::mock(array_merge($base, $other));
$uri = \Slim\Http\Uri::createFromEnvironment($env);
$headers = \Slim\Http\Headers::createFromEnvironment($env);
$cookies = (array) new \Slim\Collection();
$serverParams = (array) new \Slim\Collection($env->all());
$body = new \Slim\Http\Body(fopen('php://temp', 'r+'));
return new \Slim\Http\Request('GET', $uri, $headers, $cookies, $serverParams, $body);
}
作者:gegglet
项目:Collectiv
public function requestFactory($uri = '/')
{
// Prepare request and response objects
$env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/', 'REQUEST_METHOD' => 'GET']);
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = [];
$serverParams = $env->all();
$body = new Body(fopen('php://temp', 'r+'));
$req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
return $req;
}
作者:gegglet
项目:slimgatewa
/**
* @param string $method
* @param string $uri
* @return Request
*/
public function makeRequest($method = 'GET', $uri = '')
{
$env = Environment::mock([]);
$uri = Uri::createFromString($uri);
$headers = Headers::createFromEnvironment($env);
$cookies = [];
$serverParams = $env->all();
$body = new RequestBody();
$uploadedFiles = UploadedFile::createFromEnvironment($env);
$request = new Request($method, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles);
$request = $request->withHeader("Content-Type", "application/x-www-form-urlencoded");
return $request;
}
作者:mbarqui
项目:reactphp-sli
/**
* Creates a new request object from the data of a reactPHP request object
*
* @param \React\Http\Request $request ReactPHP native request object
* @param string $body Content of received call
*
* @return \Slim\Http\Request
*/
public static function createFromReactRequest(\React\Http\Request $request, $body = '')
{
$slimHeads = new Headers();
$cookies = [];
$host = ['', 80];
foreach ($request->getHeaders() as $reactHeadKey => $reactHead) {
$slimHeads->add($reactHeadKey, $reactHead);
switch ($reactHeadKey) {
case 'Host':
$host = static::getHost($reactHead);
break;
case 'Cookie':
$cookies = Cookies::parseHeader($reactHead);
break;
}
}
$slimUri = new Uri('http', $host[0], (int) $host[1], $request->getPath(), $request->getQuery());
$serverParams = $_SERVER;
$serverParams['SERVER_PROTOCOL'] = 'HTTP/' . $request->getHttpVersion();
$slimBody = static::getBody($body);
return new Request($request->getMethod(), $slimUri, $slimHeads, $cookies, $serverParams, $slimBody);
}
作者:takkyuuplaye
项目:hackyo
public function request($method, $path, $options = array())
{
$env = Environment::mock(array_merge(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => $path, 'REQUEST_METHOD' => $method], $options));
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = [];
$serverParams = $env->all();
$body = new RequestBody();
$req = new Request($method, $uri, $headers, $cookies, $serverParams, $body);
$res = new Response();
$app = (require ROOT_DIR . '/bootstrap/bootstrap.php');
$app($req, $res);
return $app($req, $res);
}
作者:raistlfire
项目:slim-skeleto
public function setupRequest($url, $method, $request, $files)
{
$env = Environment::mock(['REQUEST_URI' => $url, 'REQUEST_METHOD' => $method, 'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=---foo']);
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$files = UploadedFile::createFromEnvironment(Environment::mock());
$cookies = [];
$serverParams = $env->all();
$body = new RequestBody();
$this->request = new Request($method, $uri, $headers, $cookies, $serverParams, $body, $files);
$this->response = new Response();
$app = $this->app;
return $app($this->request, $this->response);
}
作者:gegglet
项目:Slim-Htt
/**
* @param array $serverParams
* @return ServerRequest
*/
public function makeServerRequest(array $serverParams)
{
// I have no idea why I need to type hint here, but phpstorm is complaining
/** @var $serverRequest ServerRequest */
$headers = Headers::createFromArray($serverParams);
$cookies = Cookies::parseHeader($headers->get('Cookie', []));
$serverRequest = new ServerRequest($serverParams, $cookies);
$serverRequest = $serverRequest->withMethod($serverParams['REQUEST_METHOD']);
$serverRequest = $serverRequest->withUri();
foreach ($headers->all() as $header => $value) {
$serverRequest = $serverRequest->withHeader($header, $value);
}
$serverRequest = $serverRequest->withUploadedFiles(UploadedFile::parseUploadedFiles($_FILES));
return $serverRequest;
}