【Laravel6】PHPUnitのdataProviderの中でFacade等がそのまま使えるようになってた
テストコード
Laravel PHPUnitのdataProviderでFacadeやfactory等のヘルパ関数を使う方法 - Qiita
↑より拝借、若干変更
<?php namespace Tests\Unit; use App\User; use Hash; use Tests\TestCase; class FacadeTest extends TestCase { /** * @param User $user * @param string $hash * @return void * @dataProvider dataProvider */ public function testBasicTest(User $user, string $hash): void { $this->assertNotNull($user); $this->assertNotNull($hash); } /** * @return array */ public function dataProvider(): array { // 5.8までは、無いとエラー // $this->createApplication(); $data = []; foreach (range(1, 100) as $i) { $data["TestCase$i"] = [ 'user' => factory(User::class)->make(), 'hash' => Hash::make('secret'), ]; } return $data; } }
5.8まで
bash-4.4# ./vendor/bin/phpunit tests/Feature/FacadeTest.php PHPUnit 7.5.15 by Sebastian Bergmann and contributors. W 1 / 1 (100%) Time: 291 ms, Memory: 4.00 MB There was 1 warning: 1) Warning The data provider specified for Tests\Unit\FacadeTest::testBasicTest is invalid. Unable to locate factory with name [default] [App\User]. WARNINGS! Tests: 1, Assertions: 0, Warnings: 1.
$this->createApplication()
しないとエラー
6
bash-4.4# ./vendor/bin/phpunit tests/Feature/FacadeTest.php PHPUnit 8.3.4 by Sebastian Bergmann and contributors. ............................................................... 63 / 100 ( 63%) ..................................... 100 / 100 (100%) Time: 7.68 seconds, Memory: 14.00 MB OK (100 tests, 200 assertions)
やったあ