4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel(5.5)公式ドキュメントにないassertDatabaseHasのDBの指定方法

Last updated at Posted at 2019-10-08

要約

公式ドキュメント見て載ってなければソースコード見ろ。

背景

Laravelで複数のデータベースを跨ぐテストを実施する際のトランザクションの設定
で当然DBを指定してassertDatabaseHasをしたいが、DBの指定方法が公式ドキュメントにも載ってない。日本語英語も両方とも。

ソースコードを見ると


<?php

namespace Illuminate\Foundation\Testing\Concerns;

use Illuminate\Foundation\Testing\Constraints\HasInDatabase;
use PHPUnit\Framework\Constraint\LogicalNot as ReverseConstraint;
use Illuminate\Foundation\Testing\Constraints\SoftDeletedInDatabase;

trait InteractsWithDatabase
{
    /**
     * Assert that a given where condition exists in the database.
     *
     * @param  string  $table
     * @param  array  $data
     * @param  string  $connection
     * @return $this
     */
    protected function assertDatabaseHas($table, array $data, $connection = null)
    {
        $this->assertThat(
            $table, new HasInDatabase($this->getConnection($connection), $data)
        );

        return $this;
    }

こんな記述が...

具体的にはPHPUnitでこう書けば良い。

$this->assertDatabaseHas('users', [
    'name' => 'JMac',
    'email' => 'jmac@example.com'
], 'mysql2');
4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?