How to Run Laravel Database Seeder from PHPUnit Test setUp?
Since version 5.8 you can do: // Run the DatabaseSeeder… $this->seed(); // Run a single seeder… $this->seed(OrderStatusesTableSeeder::class); Take a look at the documentation
Since version 5.8 you can do: // Run the DatabaseSeeder… $this->seed(); // Run a single seeder… $this->seed(OrderStatusesTableSeeder::class); Take a look at the documentation
I’m doing it this way: creating seeder with artisan: php artisan make:seeder UsersTableSeeder then you open the file and you enter users: use Illuminate\Database\Seeder; class UsersTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { DB::table(‘users’)->insert([ ‘name’ => ‘User1′, ’email’ => ‘user1@email.com’, ‘password’ => bcrypt(‘password’), ]); … Read more
DB::statement(‘SET FOREIGN_KEY_CHECKS=0;’); App\User::truncate(); DB::statement(‘SET FOREIGN_KEY_CHECKS=1;’); And it works!
All you need to do is make an artisan call db:seed in the setUp function <?php use Illuminate\Foundation\Testing\DatabaseMigrations; class ExampleTest extends TestCase { use DatabaseMigrations; public function setUp(): void { parent::setUp(); // seed the database $this->artisan(‘db:seed’); // alternatively you can call // $this->seed(); } /** * A basic functional test example. * * @return void … Read more
You can use attach() or sync() method on a many-to-many relationship. There are multiple ways you can approach this. Here one of them: // Populate roles factory(App\Role::class, 20)->create(); // Populate users factory(App\User::class, 50)->create(); // Get all the roles attaching up to 3 random roles to each user $roles = App\Role::all(); // Populate the pivot table … Read more
The attributes you pass to the create function will be passed into your model definition callback as the second argument. In your case you don’t even need to access those attributes, since they’ll automatically be merged in: $business = factory(App\Business::class)->create(); factory(App\User::class, 5)->create([ ‘business_id’ => $business->id, ]); Adapt this to your needs.
Some times it could be due to importing the wrong TestCase use PHPUnit\Framework\TestCase; [WRONG: and throws this error] use Tests\TestCase; [CORRECT]