HTTP テスト
はじめに
Laravel は、アプリケーションに対して HTTP リクエストを行い、レスポンスを検査するための非常に流暢な API を提供しています。たとえば、以下に 定義されたフィーチャーテストを見てみてください:
<?php
test('the application returns a successful response', function () {
$response = $this->get('/');
$response->assertStatus(200);
});
<?php
namespace Tests\Feature;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*/
public function test_the_application_returns_a_successful_response(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}
}
get
メソッドはアプリケーションに GET
リクエストを行い、assertStatus
メソッドは返されたレスポンスが指定された HTTP ステータスコードを持つべきであることをアサートします。この単純なアサーションに加えて、Laravel にはレスポンスヘッダーやコンテンツ、JSON 構造などを検査するためのさまざまなアサーションも含まれています。
リクエストの作成
アプリケーションにリクエストを行うには、テスト内で get
、post
、put
、patch
、または delete
メソッドを呼び出すことができます。これらのメソッドは実際にはアプリケーションに対して "実際の" HTTP リクエストを発行しません。代わりに、ネットワークリクエスト全体が内部でシミュレートされます。
テストリクエストメソッドは Illuminate\Http\Response
インスタンスを返す代わりに、Illuminate\Testing\TestResponse
のインスタンスを返します。これにより、さまざまな便利なアサーション を提供し、アプリケーションのレスポンスを検査できます:
<?php
test('basic request', function () {
$response = $this->get('/');
$response->assertStatus(200);
});
一般的に、各テストはアプリケーションに対して1回のリクエストのみを行うべきです。1つのテストメソッド内で複数のリクエストが実行されると予期しない動作が発生する可能性があります。
便宜上、テスト実行時にはCSRFミドルウェアが自動的に無効になります。
リクエストヘッダのカスタマイズ
リクエストがアプリケーションに送信される前に、withHeaders
メソッドを使用してリクエストのヘッダをカスタマイズすることができます。このメソッドを使用すると、リクエストに任意のカスタムヘッダを追加することができます:
<?php
test('interacting with headers', function () {
$response = $this->withHeaders([
'X-Header' => 'Value',
])->post('/user', ['name' => 'Sally']);
$response->assertStatus(201);
});
<?php
namespace Tests\Feature;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic functional test example.
*/
public function test_interacting_with_headers(): void
{
$response = $this->withHeaders([
'X-Header' => 'Value',
])->post('/user', ['name' => 'Sally']);
$response->assertStatus(201);
}
}
クッキー
リクエストを行う前にクッキーの値を設定するために、withCookie
メソッドまたは withCookies
メソッドを使用することができます。withCookie
メソッドはクッキー名と値をそれぞれ2つの引数として受け取ります。一方、withCookies
メソッドは名前と値のペアの配列を受け取ります:
<?php
test('interacting with cookies', function () {
$response = $this->withCookie('color', 'blue')->get('/');
$response = $this->withCookies([
'color' => 'blue',
'name' => 'Taylor',
])->get('/');
//
});
<?php
namespace Tests\Feature;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function test_interacting_with_cookies(): void
{
$response = $this->withCookie('color', 'blue')->get('/');
$response = $this->withCookies([
'color' => 'blue',
'name' => 'Taylor',
])->get('/');
//
}
}
セッション / 認証
LaravelはHTTPテスト中にセッションとやり取りするためのいくつかのヘルパーを提供しています。まず、withSession
メソッドを使用して、セッションデータを指定された配列に設定することができます。これは、アプリケーションにリクエストを発行する前にセッションにデータをロードするのに便利です:
<?php
test('interacting with the session', function () {
$response = $this->withSession(['banned' => false])->get('/');
//
});
<?php
namespace Tests\Feature;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function test_interacting_with_the_session(): void
{
$response = $this->withSession(['banned' => false])->get('/');
//
}
}
通常、Laravelのセッションは現在認証されているユーザーの状態を維持するために使用されます。そのため、actingAs
ヘルパーメソッドは指定されたユーザーを現在のユーザーとして簡単に認証する方法を提供しま す。例えば、モデルファクトリを使用してユーザーを生成し認証することができます:
<?php
use App\Models\User;
test('an action that requires authentication', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)
->withSession(['banned' => false])
->get('/');
//
});
<?php
namespace Tests\Feature;
use App\Models\User;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function test_an_action_that_requires_authentication(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)
->withSession(['banned' => false])
->get('/');
//
}
}