Core\Test\SentRequest::method
$sentRequest->method(): Core\Http\Methodmethod() returns the HTTP method of one request that your program sent in a test. You get the request from Core\Test::sentHttp(), after Core\Test::answerHttp has set up a fake web service.
The result is a case of the Core\Http\Method enum. A request sent with Core\Http\Client::get returns Core\Http\Method::Get. A request sent with Core\Http\Client::post returns Core\Http\Method::Post. A request sent with Core\Http\Client::request returns the method you gave it. You can compare the result with == or use it in a match.
The examples below show how to read the method of one request, the method of each request in a list, and a test that checks a client removes an item with DELETE.
Return value
Returns Core\Http\Method. A Core\Http\Method case. A request sent with Core\Http\Client::get returns Core\Http\Method::Get, a request sent with Core\Http\Client::post returns Core\Http\Method::Post, and the other methods work the same way.
Examples
Read the method
<?nvs// Shows how to read the HTTP method of a request your program sent.
use Core\Http\Client;use Core\Http\Method;use Core\Test;
Test::answerHttp('https://api.example.com/notes', 201);Client::post('https://api.example.com/notes', { json: ['title' => 'Shopping'] });
// `method()` returns a `Core\Http\Method` case.// The program used `Client::post`, so this prints `POST`.Method $method = Test::sentHttp()[0]->method();echo $method == Method::Post ? 'POST' : 'not POST', "\n";POSTEvery request has its own method
<?nvs// Shows the method of each request when a program sends several requests.
use Core\Http\Client;use Core\Http\Method;use Core\Test;use Core\Test\SentRequest;
Test::answerHttp('https://api.example.com/*', 200);
Client::get('https://api.example.com/users/7');Client::put('https://api.example.com/users/7', { json: ['name' => 'Ada'] });// `Client::request` takes the method as its first argument.Client::request(Method::Options, 'https://api.example.com/users');
// `Core\Http::methodName` converts each method to text.// This prints `GET`, `PUT` and `OPTIONS`, one on each line.foreach (Test::sentHttp() as SentRequest $request) { echo Core\Http::methodName($request->method()), "\n";}GETPUTOPTIONSTest that removing an item sends delete
<?nvs// Shows how to test that a shopping cart client removes an item with a DELETE request.
use Core\Http\Client;use Core\Http\Method;use Core\Test;
class CartClient{ // Removes one item from the cart on the shop service. public static function remove(uint $itemId): int { var $response = Client::delete('https://shop.example.com/cart/items/' . $itemId); return $response->status(); }}
Test::answerHttp('https://shop.example.com/cart/items/*', 204);CartClient::remove(42);
// A GET or a POST to the same URL would not remove the item.// The test checks that the client used DELETE.Test::assertCount(Test::sentHttp(), 1);Test::assertSame(Test::sentHttp()[0]->method(), Method::Delete);
// This prints `the item was removed with DELETE`.echo "the item was removed with DELETE\n";the item was removed with DELETE