Skip to content

Instantly share code, notes, and snippets.

@MarceloHoffmeister
Forked from Alymosul/README.md
Last active September 11, 2020 18:52
Show Gist options
  • Save MarceloHoffmeister/9df156f89069b61eb7bef881c251b79a to your computer and use it in GitHub Desktop.
Save MarceloHoffmeister/9df156f89069b61eb7bef881c251b79a to your computer and use it in GitHub Desktop.
[Laravel] Seeding data in testing as part of the application build | Semeando dados em testes como parte da construção do aplicativo

A trait SeedDatabase junto com a classe SeedDatabaseState dá ao seu projeto Laravel a habilidade de fazer a seed do banco de dados antes de rodar as test suites, o que melhora a velocidade e tona-se melhor do que executar as seeds do banco de dados antes de cada teste.

Além disso, tem a opção de executar seeds personalizadas em vez dos seeds que são chamados no método run() da classe DatabaseSeeder. Você pode conseguir isso da seguinte forma:

SeedDatabase trait along with SeedDatabaseState class gives your Laravel project the ability to seed the testing database once before running the full suite tests, which improves the speed of the tests than seeding the testing database before each test.

Also, it has the option to run custom seeders instead of the seeders that are called in the run() method of the DatabaseSeeder class you can achieve that as follows:

Testcase.php

public function setUp()
{
    parent::setUp();
    SeedDatabaseState::$seeders = [RolesSeeder::class, PermissionSeeder::class];
    $this->seedDatabase();
}

Instruções:

  • Coloque o SeedDatabase.php e o SeedDatabaseState.php na pasta de testes em seu projeto Laravel e atualize o TestCase.php para usar a trait SeedDatabase e faça as mudanças no método setup() para chamar $this->seedDatabase() depois é só chamar o parent::setup().

Instructions:

  • Place the SeedDatabase.php and the SeedDatabaseState.php into the tests folder in your Laravel project and update the TestCase.php to use the SeedDatabase trait and make the changes in the setup() method to call $this->seedDatabase(). After just calling the parent::setup().
<?php
namespace Educar\Support\Traits;
use Educar\Support\Database\SeedDatabaseState;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\Seeder;
use Illuminate\Foundation\Testing\RefreshDatabaseState;
trait SeedDatabase
{
/**
* Seeds the database.
*
* @return void
*/
public function seedDatabase()
{
if (! SeedDatabaseState::$seeded) {
$this->runSeeders(SeedDatabaseState::$seeders);
$this->refresh();
if (SeedDatabaseState::$seedOnce) {
SeedDatabaseState::$seeded = true;
}
}
}
protected function refresh()
{
if (! RefreshDatabaseState::$migrated) {
$this->artisan('migrate:refresh');
$this->app[Kernel::class]->setArtisan(null);
RefreshDatabaseState::$migrated = true;
}
$this->beginDatabaseTransaction();
}
/**
* Calls specific seeders if possible.
*
* @param array $seeders
*/
public function runSeeders(array $seeders)
{
if (empty($seeders)) {
$this->artisan('db:seed');
$this->app[Kernel::class]->setArtisan(null);
return;
}
$this->getSeederInstance()->call($seeders);
}
/**
* Builds a quick seeder instance.
*
* @return Seeder
*/
private function getSeederInstance()
{
return
new class() extends Seeder {
public function run()
{
}
};
}
}
<?php
namespace Tests;
class SeedDatabaseState
{
/**
* Indicates if the test database has been seeded.
*
* @var bool
*/
public static $seeded = false;
/**
* Indicates if the seeders should run once at the beginning of the suite.
*
* @var bool
*/
public static $seedOnce = false;
/**
* Runs only these registered seeders instead of running all seeders.
*
* @var array
*/
public static $seeders = [];
}
<?php
namespace Tests;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication, RefreshDatabase, SeedDatabase;
// aqui ou no teste
// here or on test
public function setUp(): void
{
parent::setUp();
$this->seedDatabase();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment