LoginSignup
0
0

More than 3 years have passed since last update.

Laravelのテストメソッド抜粋

Posted at

環境

Laravel5.8

前処理・後処理

    protected function setUp()
    {
        parent::setUp();
        // 前処理
    }

    public function tearDown()
    {
        parent::tearDown();
        // 後処理
    }

データベース

Artisan::call('migrate:refresh');
$this->seedseed($class = 'DatabaseSeeder');
$this->assertDatabaseHas($table, $data, $connection = null);
$this->assertDatabaseMissing($table, $data, $connection = null);
$this->assertSoftDeleted($table, $data, $connection = null);

ファクトリー

// インスタンスの作成
$user = factory(User::class)->make();
$user = factory(User::class)->make(["name" => "Abigail"]);
// インスタンスの保存
$user = factory(App\User::class)->create();

認証

$this->actingAs($user, $driver = null);
$this->assertAuthenticated($guard = null);
$this->assertGuest($guard = null);
$this->assertAuthenticatedAs($user, $guard = null);
$this->assertCredentials($credentials, $guard = null);
$this->assertInvalidCredentials($credentials, $guard = null);

Httpリクエスト

$response = $this->followRedirects($response);
$response = $this->get($uri, $headers = []);
$response = $this->post($uri, $data = [], $headers = []);
$response = $this->put($uri, $data = [], $headers = []);
$response = $this->patch($uri, $data = [], $headers = []);
$response = $this->delete($uri, $data = [], $headers = []);

レスポンス

$response->assertStatus($code);

$response->assertCookie($cookieName, $value = null);
$response->assertCookieMissing($cookieName);
$response->assertSessionHas($key, $value = null);
$response->assertSessionMissing($key);

$response->assertViewIs($value); //レスポンスが指定したビューかどうか
$response->assertViewHas($key, $value = null); //レスポンスのビューが指定したデータを持っている
$response->assertViewMissing($key);
$response->assertLocation($uri);
$response->assertRedirect($uri);

$response->assertSee($value);
$response->assertDontsee($value);
$response->assertSeeText($value);
$response->assertDontSeeText($value);

メール

Mail::fake();
Mail::assertNothingSent();

Mail::assertSent(OrderShipped::class, 2);
Mail::assertNotSent(AnotherMailable::class);
Mail::assertSent(OrderShipped::class, function ($mail) use ($user) {
    return $mail->order->id === $order->id &&
            $mail->hasTo($user->email) &&
            $mail->hasCc('...') &&
            $mail->hasBcc('...');
});

$failures = Mail::failures();

PHPUnit

$this->assertTrue($condition, $message = '');
$this->assertFalse($condition, $message = '');
$this->assertNull($variable, $message = '');
$this->assertSame($expected, $actual, $message = '');   // ===
$this->assertEquals($expected, $actual, $message = ''); // ==
$this->assertEmpty($actual, $message = '');
$this->assertNotEmpty($actual, $message = '');
$this->assertRegExp($pattern, $string, $message = '');
$this->assertContains($needle, $haystack, $message = '');
$this->assertCount($expectedCount, $haystack, $message = '');
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0