From 25dbc9f4b5b15384ff1e9cd2272e292e76f2a80e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Capello?= Date: Thu, 12 Mar 2026 18:01:26 -0300 Subject: [PATCH 1/2] Add support to post other content types --- src/Visit.php | 25 +++++++++++++++++++++---- tests/VisitTest.php | 7 +++++++ tests/public/index.php | 4 ++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Visit.php b/src/Visit.php index 369c386..da31f47 100644 --- a/src/Visit.php +++ b/src/Visit.php @@ -48,8 +48,22 @@ public function get(string $path):Visit { return $this->simulateRequest("GET", $path); } - public function post(string $path, array $data):Visit { - return $this->simulateRequest("POST", $path, http_build_query($data)); + public function post(string $path, array|string $data, string $content_type = 'application/x-www-form-urlencoded'):Visit { + if (is_array($data)) { + switch($content_type) { + case "application/x-www-form-urlencoded": + $data = http_build_query($data); + break; + case "application/json": + $data = json_encode($data); + break; + } + } + return $this->simulateRequest("POST", $path, $data, $content_type); + } + + public function postJson(string $path, array|string $data):Visit { + return $this->post($path, $data, "application/json"); } public function assertStatusCode(int $expected_code):Visit { @@ -98,7 +112,10 @@ public function followRedirect():Visit { return $this->simulateRequest("GET", $this->redir_location); } - private function simulateRequest(string $method, string $path, ?string $data = null):Visit { + private function simulateRequest(string $method, + string $path, + ?string $data = null, + string $content_type = 'application/x-www-form-urlencoded'):Visit { $this->method = $method; $this->path = $path; @@ -121,7 +138,7 @@ private function simulateRequest(string $method, string $path, ?string $data = n 'SCRIPT_NAME' => basename($script), 'SCRIPT_FILENAME' => $script, 'CONTENT_LENGTH' => ($data ? strlen($data): 0), - 'CONTENT_TYPE' => 'application/x-www-form-urlencoded', + 'CONTENT_TYPE' => $content_type, 'HTTP_COOKIE' => (!empty($this->cookies) ? implode('; ', $this->cookies) . ';': '')]); $desc = [["pipe", "r"], diff --git a/tests/VisitTest.php b/tests/VisitTest.php index cde085b..fbd69d9 100644 --- a/tests/VisitTest.php +++ b/tests/VisitTest.php @@ -50,4 +50,11 @@ public function testMocking(): void ->get('/bye') ->assertSee('Bye World'); } + + public function testPostJson(): void + { + visit()->postJson("/json", + ["first_name" => "John", "last_name" => "Doe"]) + ->assertSee('application/json: {"first_name":"John","last_name":"Doe"}'); + } } diff --git a/tests/public/index.php b/tests/public/index.php index 911bab5..31f2b19 100644 --- a/tests/public/index.php +++ b/tests/public/index.php @@ -19,6 +19,10 @@ echo bye_world(); break; + case '/json': + echo "{$_SERVER['CONTENT_TYPE']}: ". file_get_contents('php://input'); + break; + default: http_response_code(404); break; From 77f4c42d7c6947c6a52cf685cf154624f3c5fd57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Capello?= Date: Fri, 13 Mar 2026 18:19:25 -0300 Subject: [PATCH 2/2] Add cgi.nph=1 directive to make php-cgi always send the Status header --- src/Visit.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Visit.php b/src/Visit.php index da31f47..ebd1e6e 100644 --- a/src/Visit.php +++ b/src/Visit.php @@ -152,6 +152,7 @@ private function simulateRequest(string $method, " -d cgi.force_redirect=0" . " -d session.save_path=/tmp" . " -d opcache.jit=disable " . + " -d cgi.nph=1" . $script, $desc, $pipes, env_vars:$env);