/** * Create a new mock request * * @param string $uri * @param string $req_method * @param array $parameters * @param array $cookies * @param array $server * @param array $files * @param string $body * @return void */ public static function create($uri = '/', $req_method = 'GET', $parameters = array(), $cookies = array(), $server = array(), $files = array(), $body = null) { // Create a new Request object $request = new Request(array(), array(), $cookies, $server, $files, $body); // Reformat $req_method = strtoupper(trim($req_method)); // Set its URI and Method $request->server()->set('REQUEST_URI', $uri); $request->server()->set('REQUEST_METHOD', $req_method); // Set our parameters switch ($req_method) { case 'POST': case 'PUT': case 'PATCH': case 'DELETE': $request->paramsPost()->replace($parameters); break; default: $request->paramsGet()->replace($parameters); break; } return $request; }
public function testQueryModify() { $test_uri = '/test?query'; $query_string = 'search=string&page=2&per_page=3'; $test_one = ''; $test_two = ''; $test_three = ''; $request = new Request(); $request->server()->set('REQUEST_URI', $test_uri); $request->server()->set('QUERY_STRING', $query_string); $this->klein_app->respond(function ($request, $response, $service) use(&$test_one, &$test_two, &$test_three) { // Add a new var $test_one = $request->query('test', 'dog'); // Modify a current var $test_two = $request->query('page', 7); // Modify a current var $test_three = $request->query(array('per_page' => 10)); }); $this->klein_app->dispatch($request); $expected_uri = parse_url($this->klein_app->request()->uri(), PHP_URL_PATH); $this->assertSame($expected_uri . '?' . $query_string . '&test=dog', $test_one); $this->assertSame($expected_uri . '?' . str_replace('page=2', 'page=7', $query_string), $test_two); $this->assertSame($expected_uri . '?' . str_replace('per_page=3', 'per_page=10', $query_string), $test_three); }