Ejemplo n.º 1
0
 /**
  * Create a normalized tree of UploadedFile instances from the Environment.
  *
  * @param Environment $env The environment
  *
  * @return array|null A normalized tree of UploadedFile instances or null if none are provided.
  */
 public static function createFromEnvironment(Environment $env)
 {
     if (is_array($env['slim.files']) && $env->has('slim.files')) {
         return $env['slim.files'];
     } elseif (isset($_FILES)) {
         return static::parseUploadedFiles($_FILES);
     }
     return [];
 }
Ejemplo n.º 2
0
 /**
  * If HTTP_AUTHORIZATION does not exist tries to get it from
  * getallheaders() when available.
  *
  * @param Environment $environment The Slim application Environment
  *
  * @return Environment
  */
 public static function determineAuthorization(Environment $environment)
 {
     $authorization = $environment->get('HTTP_AUTHORIZATION');
     if (null === $authorization && is_callable('getallheaders')) {
         $headers = getallheaders();
         $headers = array_change_key_case($headers, CASE_LOWER);
         if (isset($headers['authorization'])) {
             $environment->set('HTTP_AUTHORIZATION', $headers['authorization']);
         }
     }
     return $environment;
 }
 public function setUp()
 {
     $this->encryption = Mockery::mock(CookieEncryptionInterface::class);
     $this->request = Request::createFromEnvironment(Environment::mock());
     $this->response = new Response();
     $this->capturedRequest = null;
 }
Ejemplo n.º 4
0
 /**
  * @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;
 }
 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'];
 }
Ejemplo n.º 6
0
 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));
 }
Ejemplo n.º 7
0
 /**
  * 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;
 }
Ejemplo n.º 8
0
 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();
 }
Ejemplo n.º 9
0
 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;
 }
 /**
  * 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;
 }
Ejemplo n.º 11
0
 /**
  * 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']);
 }
 /**
  * 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'));
 }
Ejemplo n.º 13
0
 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 = [];
 }
 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));
 }
Ejemplo n.º 15
0
 /**
  *
  */
 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']);
 }
 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']);
 }
 /**
  * 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());
 }
Ejemplo n.º 18
0
 /**
  *
  */
 public function testRun()
 {
     $this->createUser('foo');
     $request = Request::createFromEnvironment(Environment::mock(['QUERY_STRING' => 'obj_type=charcoal/admin/user']));
     $response = new Response();
     $res = $this->obj->run($request, $response);
     $this->assertEquals(200, $res->getStatusCode());
     $res = $this->obj->results();
     $this->assertTrue($res['success']);
 }
Ejemplo n.º 19
0
 /**
  * @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;
 }
Ejemplo n.º 20
0
 public function requestFactory($requestUrl)
 {
     $uri = Uri::createFromString($requestUrl);
     $headers = new Headers();
     $cookies = [];
     $env = Slim\Http\Environment::mock();
     $serverParams = $env->all();
     $body = new Body(fopen('php://temp', 'r+'));
     $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
     return $request;
 }
Ejemplo n.º 21
0
 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;
 }
Ejemplo n.º 22
0
 /**
  * Run before each test.
  */
 public function setUp()
 {
     $uri = Uri::createFromString('https://example.com:443/foo/bar');
     $headers = new Headers();
     $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();
 }
Ejemplo n.º 23
0
 /**
  * Invoke middleware
  *
  * @param  ServerRequestInterface   $request  PSR7 request object
  * @param  ResponseInterface        $response PSR7 response object
  * @param  callable                 $next     Next middleware callable
  *
  * @return ResponseInterface PSR7 response object
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     global $argv;
     $this->request = $request;
     if (isset($argv)) {
         list($call, $path, $params) = $argv;
         $this->request = \Slim\Http\Request::createFromEnvironment(\Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/' . $path . '?' . $params, 'QUERY_STRING' => $params]));
         unset($argv);
     }
     return $next($this->request, $response);
 }
Ejemplo n.º 24
0
 /**
  * Run before each test
  */
 public function setUp()
 {
     $uri = \Slim\Http\Uri::createFromString('https://example.com:443/foo/bar?abc=123');
     $headers = new \Slim\Http\Headers();
     $cookies = new \Slim\Collection();
     $env = \Slim\Http\Environment::mock();
     $serverParams = new \Slim\Collection($env->all());
     $body = new \Slim\Http\Body(fopen('php://temp', 'r+'));
     $this->request = new \Slim\Http\Request('GET', $uri, $headers, $cookies, $serverParams, $body);
     $this->response = new \Slim\Http\Response();
 }
 /**
  *
  */
 public function testRun()
 {
     $container = $this->container();
     $userProto = $container['model/factory']->create(User::class);
     $userProto->source()->createTable();
     $request = Request::createFromEnvironment(Environment::mock(['QUERY_STRING' => 'obj_type=charcoal/admin/user&obj_orders[]=foo&obj_orders[]=bar']));
     $response = new Response();
     $res = $this->obj->run($request, $response);
     $this->assertEquals(200, $res->getStatusCode());
     $res = $this->obj->results();
     $this->assertTrue($res['success']);
 }
Ejemplo n.º 26
0
 public function getMockedController($token, $result)
 {
     $req = Request::createFromEnvironment(Env::mock());
     $res = new Response();
     $args = ['token' => $token];
     $db = $this->getMockBuilder('\\Abridger\\DB')->disableOriginalConstructor()->setMethods(['prepare'])->getMock();
     $query = $this->getMock('\\PDOStatement');
     $query->method('fetch')->willReturn($result);
     $db->method('prepare')->willReturn($query);
     $cnt = new Container(['DB' => $db]);
     return new \Abridger\Controller\Web($cnt, $req, $res, $args);
 }
Ejemplo n.º 27
0
 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;
 }
 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;
 }
Ejemplo n.º 29
0
 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);
 }
 /**
  * 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();
 }