入力値が、指定のDBテーブルのコラムに値が存在することを判定。
以下の実行で、ダウンロードできます
$ git clone git@gist.github.com:e1a58e0921ce7ddb1beb4ffab57779b3.git tests/Unit/Validations/Exists
入力値が、指定のDBテーブルのコラムに値が存在することを判定。
以下の実行で、ダウンロードできます
$ git clone git@gist.github.com:e1a58e0921ce7ddb1beb4ffab57779b3.git tests/Unit/Validations/Exists
<?php | |
namespace Tests\Unit\Validations\Exists; | |
use Tests\TestCase; | |
use Illuminate\Foundation\Testing\RefreshDatabase; | |
use Validator; | |
use App\User; | |
class ExistsTest extends TestCase | |
{ | |
use RefreshDatabase; | |
/** | |
* @test | |
* @dataProvider provider_exists | |
*/ | |
public function exists($input, $expected) | |
{ | |
// \DB::enableQueryLog(); | |
// DBに1つだけレコードを作成 | |
$user = factory(User::class)->create([ | |
'email' => 'test@example.com' | |
]); | |
$v = Validator::make( | |
$input, | |
['email' => 'required|exists:users,email'] | |
); | |
$this->assertEquals($expected, $v->passes()); | |
// print_r(\DB::getQueryLog()); | |
} | |
public function provider_exists() | |
{ | |
return [ | |
[['email' => 'test@example.com'], true], | |
[['email' => 'test2@example.com'], false], | |
]; | |
} | |
} |