本文整理汇总了PHP中Slim\Http\Environment::mock方法的典型用法代码示例。如果您正苦于以下问题:PHP Environment::mock方法的具体用法?PHP Environment::mock怎么用?PHP Environment::mock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Slim\Http\Environment
的用法示例。
在下文中一共展示了Environment::mock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: requestFactory
/**
* @param $method
* @param $path
* @param $body
* @param $options
* @return Request
*/
protected function requestFactory($method, $path, $body = [], $options = [])
{
$uri = Uri::createFromString($path);
$headers = new Headers();
$cookies = [];
$_POST['_METHOD'] = $method;
if (strtolower($method) != 'get' && is_array($body)) {
foreach ($body as $key => $value) {
$_POST[$key] = $value;
}
}
$envMethod = 'POST';
if (strtolower($method) == 'get') {
$envMethod = 'GET';
}
$env = Environment::mock(['REQUEST_URI' => $path, 'REQUEST_METHOD' => $envMethod, 'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=---foo']);
$serverParams = $env->all();
$body = $this->buildBody($body);
//echo $body->getContents();
// @todo
// $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body, []);
$request = Request::createFromEnvironment($env);
unset($_POST);
return $request;
}
示例2: request
/**
* Perform request
*
* @param string $method
* @param string $uri
* @param array $params
* @param array $server
* @param string $content
*
* @throws \Slim\Exception\MethodNotAllowedException
* @throws \Slim\Exception\NotFoundException
*/
public function request($method, $uri, array $params = [], array $server = [], $content = null)
{
$method = strtoupper($method);
switch ($method) {
case 'POST':
case 'PUT':
case 'PATCH':
case 'DELETE':
$this->server['slim.input'] = http_build_query($params);
$query = '';
break;
case 'GET':
default:
$query = http_build_query($params);
break;
}
$server = array_merge($this->server, $server, ['CONTENT_TYPE' => 'application/json', 'REQUEST_URI' => $uri, 'REQUEST_METHOD' => $method, 'QUERY_STRING' => $query]);
$env = Http\Environment::mock($server);
$request = Http\Request::createFromEnvironment($env);
$response = new Http\Response();
// dirty hack to set body of request :(
if (!is_null($content)) {
\Closure::bind(function ($request) use($content) {
$request->bodyParsed = $content;
}, null, $request)->__invoke($request);
}
$response = $this->app->__invoke($request, $response);
$this->request = $request;
$this->response = $response;
}
示例3: setUp
public function setUp()
{
$this->request = Request::createFromEnvironment(Environment::mock());
$this->response = new Response();
$this->logger = new MemoryLogger();
$this->config = ['error' => 'critical', 'not-allowed' => 'info', 'not-found' => 'info'];
}
示例4: runApp
/**
* Process the application given a request method and URI
*
* @param string $requestMethod the request method (e.g. GET, POST, etc.)
* @param string $requestUri the request URI
* @param array|object|null $requestData the request data
* @return \Slim\Http\Response
*/
public function runApp($requestMethod, $requestUri, $requestData = null)
{
// Create a mock environment for testing with
$environment = Environment::mock(['REQUEST_METHOD' => $requestMethod, 'REQUEST_URI' => $requestUri]);
// Set up a request object based on the environment
$request = Request::createFromEnvironment($environment);
// Add request data, if it exists
if (isset($requestData)) {
$request = $request->withParsedBody($requestData);
}
// Set up a response object
$response = new Response();
// Use the application settings
$settings = (require __DIR__ . '/../../src/settings.php');
// Instantiate the application
$app = new App($settings);
// Set up dependencies
require __DIR__ . '/../../src/dependencies.php';
// Register middleware
if ($this->withMiddleware) {
require __DIR__ . '/../../src/middleware.php';
}
// Register routes
require __DIR__ . '/../../src/routes.php';
// Process the application
$response = $app->process($request, $response);
// Return the response
return $response;
}
示例5: dispatch
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));
}
示例6: request
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();
}
示例7: requestFactory
public function requestFactory($method, $path)
{
$environment = Environment::mock(['REQUEST_METHOD' => $method, 'REQUEST_URI' => $path, 'QUERY_STRING' => 'foo=bar']);
$request = Request::createFromEnvironment($environment);
$request->withMethod('GET');
return $request;
}
示例8: setUp
public function setUp()
{
$this->encryption = Mockery::mock(CookieEncryptionInterface::class);
$this->request = Request::createFromEnvironment(Environment::mock());
$this->response = new Response();
$this->capturedRequest = null;
}
示例9: setUp
public function setUp()
{
$container = new \Slim\Container(include "./Skeleton/Config/test.config.php");
$this->controller = $container[DefaultController::class];
$this->response = new Response();
$this->request = Request::createFromEnvironment(Environment::mock());
$this->args = [];
}
示例10: testCreateFromEnvironmentIgnoresHeaders
public function testCreateFromEnvironmentIgnoresHeaders()
{
$e = Environment::mock(['CONTENT_TYPE' => 'text/csv', 'HTTP_CONTENT_LENGTH' => 1230]);
$h = Headers::createFromEnvironment($e);
$prop = new ReflectionProperty($h, 'data');
$prop->setAccessible(true);
$this->assertNotContains('content-length', $prop->getValue($h));
}
示例11: testMock
/**
* Test environment from mock data
*/
public function testMock()
{
$env = Environment::mock(['SCRIPT_NAME' => '/foo/bar/index.php', 'REQUEST_URI' => '/foo/bar?abc=123']);
$this->assertTrue(is_array($env));
$this->assertEquals('/foo/bar/index.php', $env['SCRIPT_NAME']);
$this->assertEquals('/foo/bar?abc=123', $env['REQUEST_URI']);
$this->assertEquals('localhost', $env['HTTP_HOST']);
}
示例12: testMock
/**
* Test environment from mock data
*/
public function testMock()
{
$env = Environment::mock(['SCRIPT_NAME' => '/foo/bar/index.php', 'REQUEST_URI' => '/foo/bar?abc=123']);
$this->assertInstanceOf('\\Slim\\Interfaces\\CollectionInterface', $env);
$this->assertEquals('/foo/bar/index.php', $env->get('SCRIPT_NAME'));
$this->assertEquals('/foo/bar?abc=123', $env->get('REQUEST_URI'));
$this->assertEquals('localhost', $env->get('HTTP_HOST'));
}
示例13: testRunWithInvalidCredentials
/**
* Assert that
*/
public function testRunWithInvalidCredentials()
{
$this->createUser('foo', 'foobar');
$request = Request::createFromEnvironment(Environment::mock(['QUERY_STRING' => 'username=test&password=test123']));
$response = new Response();
$res = $this->obj->run($request, $response);
$this->assertFalse($this->obj->success());
$this->assertEquals(403, $res->getStatusCode());
}
示例14: testRunWithInvalidRecaptchaReturns404
public function testRunWithInvalidRecaptchaReturns404()
{
$request = Request::createFromEnvironment(Environment::mock(['QUERY_STRING' => 'token=foobar&username=foobar&password=foo&password_confirm=foo&g-recaptcha-response=foobar']));
$response = new Response();
$res = $this->obj->run($request, $response);
$this->assertEquals(404, $res->getStatusCode());
$res = $this->obj->results();
$this->assertFalse($res['success']);
}
示例15: testRunWithoutObjTypeIs404
/**
*
*/
public function testRunWithoutObjTypeIs404()
{
$request = Request::createFromEnvironment(Environment::mock());
$response = new Response();
$res = $this->obj->run($request, $response);
$this->assertEquals(404, $res->getStatusCode());
$res = $this->obj->results();
$this->assertFalse($res['success']);
}