nvs debug to-test
nvs debug to-test prints a test made from one request that your server recorded.
The test sends the same request to your program with Core\Test::request. It checks the status, the Content-Type header, the Location header of a redirect, and the body. A JSON body is compared as data, so spaces in it do not matter. Save the test in your project and run it with nvs test .. With --write <file>, the command writes the test to a new file.
The request must come from a development server with [debug] store = true in nvs.toml. Give the trace id that the debug viewer shows for the request.
Good to know: a header that was removed when the request was recorded, such as Authorization, gets the value 'redacted'. Replace it with a credential your tests may use.
The examples below show the options of the command, a test for a JSON page, and a test for a request with a removed Authorization header.
Examples
Print the options of nvs debug to test
nvs debug to-test --helpPrint a test that sends a recorded request again and checks the response....Options: --span <SPAN_ID> The span id of the request. You need it only when several requests have the same trace id
--write <FILE> Write the test to this new file. If the file exists, nothing is written...A test for a json page
<?nvs// Shows a JSON page and the test that `nvs debug to-test` prints for one request to it.use Core\Http\Method;use Core\Test;
class Orders{ #[Core\Route(path: '/orders/{id}', method: Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function show(uint $id): void { Core\Response::sendJson({ id: $id, status: 'paid', total: 4500 }); }}
class Gate implements Core\Router\Gate{ public function allows(Core\Router\Match $route): bool { return true; }
public function refused(Core\Router\Refusal $reason, ?Core\Router\Match $route): void { echo 'Not found.'; }
public function failed(?Core\Router\Match $route, ?Throwable $error): void { echo 'Something went wrong.'; }}
// This class is the output of `nvs debug to-test 0af7651916cd43dd8448eb211c80319c`.// `nvs test` runs it. The recorded body had spaces, and the test still passes.class GetOrders70af76519Test{ #[Test] public function givesTheRecordedResponse(): void { array<string> $headers = [ 'host' => 'localhost:8080', 'user-agent' => 'curl/8.9.1', 'accept' => '*/*', ]; var $response = Test::request(Method::Get, '/orders/7', { headers: $headers });
Test::assertEquals($response->status(), 200); Test::assertEquals($response->header('content-type'), 'application/json'); Test::assertEquals($response->json(), Core\Json::decode('{ "id": 7, "status": "paid", "total": 4500 }')); }}
if (Core\Env::context() == Core\Env::Context::Request) { Core\Router::dispatch(new Gate());} else { // `nvs run` sends the same request once and prints the response. var $response = Test::request(Method::Get, '/orders/7'); echo $response->status(), ' ', $response->body(), "\n";}200 {"id":7,"status":"paid","total":4500}A test for a request with a login
<?nvs// Shows the test that `nvs debug to-test` prints for a request with an `Authorization` header.use Core\Http\Method;use Core\Test;
class Orders{ #[Core\Route(path: '/orders', method: Method::Post)] #[Core\Access(allow: Core\Audience::Public)] public function create(): void { Core\Response::setStatus(201); Core\Response::sendJson({ id: 8, status: 'open' }); }}
// This gate only checks that the request has an `Authorization` header.// A real gate also checks the token.class Gate implements Core\Router\Gate{ public function allows(Core\Router\Match $route): bool { return Core\Request::header('Authorization') != null; }
public function refused(Core\Router\Refusal $reason, ?Core\Router\Match $route): void { echo 'Not allowed.'; }
public function failed(?Core\Router\Match $route, ?Throwable $error): void { echo 'Something went wrong.'; }}
// This class is the output of `nvs debug to-test 4bf92f3577b34da6a3ce929d0e0e4736`.// The server did not record the token, so the test sends the text `redacted`.class PostOrders4bf92f35Test{ #[Test] public function givesTheRecordedResponse(): void { array<string> $headers = [ 'host' => 'localhost:8080', // This value was removed when the request was recorded. Put a test credential here. 'authorization' => 'redacted', 'content-type' => 'application/json', ]; var $response = Test::request(Method::Post, '/orders', { headers: $headers, body: '{"item":"tea","quantity":2}' });
Test::assertEquals($response->status(), 201); Test::assertEquals($response->header('content-type'), 'application/json'); Test::assertEquals($response->json(), Core\Json::decode('{"id":8,"status":"open"}')); }}
if (Core\Env::context() == Core\Env::Context::Request) { Core\Router::dispatch(new Gate());} else { // Without the header, the gate stops the request. This prints `403 Not allowed.`. var $response = Test::request(Method::Post, '/orders'); echo $response->status(), ' ', $response->body(), "\n";}403 Not allowed.