Laravelでは、PHPUnitが標準で組み込まれています。
なのでセットアップ完了後からすぐにテストコードを書き始めることができます!
「何故テストコードを書かないといけないのか」に関しては、他の方の記事が素晴らしいのでこちらを見ていただければと思います。
Feature
とUnit
の使い分け
tests
配下に、Feature
とUnit
のディレクトリがあります。
Feature
にはControllerやRouting、Middlewareなどの機能テストを実装します。
それ以外の単体テストをUnit
で実装します。
テストの作成
// Featureディレクトリにテストを作成する
$ php artisan make:test UserTest
// Unitディレクトリにテストを作成する
$ php artisan make:test UserTest --unit
テストの実行
// Featureディレクトリのテストを実行する
$ vendor/bin/phpunit tests/Feature/UserTest
// Utilディレクトリのテストを実行する
$ vendor/bin/phpunit tests/Util/UserTest
// 全体をテストするときは以下でもOK
$ php artisan test
ケーススタディ
①ビューのテスト
$response = $this->get('/')
で、'/'にアクセスした結果のレスポンスを取得できます。
そして、$response->assertStatus(200)
で、そのレスポンスが問題ないこと(ステータスが200 OKであること)をチェックしています。
assertViewIs("welcome")
で、welcome
というビューが表示されるかどうかをチェックします。
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class RouteTest extends TestCase
{
public function testRouteTop()
{
$response = $this->get("/");
$response->assertStatus(200)->assertViewIs("welcome");
}
}
②ログイン状況のテスト
以下は未ログインユーザーのテストです。
$response->assertRedirect("/login");
でリダイレクトされ、/login
に移動します。
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class RouteTest extends TestCase
{
public function testGuestCreate()
{
$response = $this->get("/create");
$response->assertRedirect("/login");
}
}
以下はログインユーザーのテストです。
actingAs($user)
でログインします。
<?php
namespace Tests\Feature;
// models
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class RouteTest extends TestCase
{
use RefreshDatabase;
public function testAuthCreate()
{
// Userモデルを準備
$user = factory(User::class)->create();
// ログインして投稿画面へアクセスすることを実行
$response = $this->actingAs($user)->get("/create");
// レスポンスを検証
$response->assertStatus(200)->assertViewIs("/create");
}
}
テストのコードの書き方のパターンに「AAA」というのがあります。
Arrage-Act-Assert、日本語でいうと準備・実行・検証となります。
よく使うアサートなどをまとめます。
【assertStatus】レスポンスのステータスが指定したものと正しいか
$response->assertStatus(200);
【assertViewIs】指定したビューが返る
$response->assertViewIs("welcome");
【assertRedirect】指定したURIへリダイレクト
$response->assertRedirect("/login");
【actingAs】ログインする
$response = $this->actingAs($user)
トレイトに関して
コードを再利用するための仕組みをトレイトといいます。
トレイトは、継承と似たPHPの機能です。
汎用性の高いメソッドなどをトレイトとしてまとめておき、他の複数のクラスで共通して使う、という使い方をします。
【RefreshDatabase】データベースに対して行ったアクションをすべて白紙にする
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class RouteTest extends TestCase
{
use RefreshDatabase;
public function testRouteTop()
{
$response = $this->get("/");
$response->assertStatus(200)->assertViewIs("welcome");
}
}