andybeak
7/12/2016 - 2:49 PM

Laravel 5.2 controlling database seeds and migrations

Laravel 5.2 controlling database seeds and migrations

<?php

/**
 * These tests are used to test whether users are allowed to access reflections
 */

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

/**
 * Class AccessReflectionsTest
 */
class AuditEventsTest extends TestCase
{

    use DatabaseTransactions;

    /**
     * setUp
     * @access public
     *
     *
     */
    public function setUp()
    {
        parent::setUp();
    }
    
    public function testMyTest()
    {
        parent::$needsSeed = true;   // you shouldn't need to do this but it's an option
    }
<?php

class TestCase extends Illuminate\Foundation\Testing\TestCase
{
      /**
     * Tracks whether we must (re)seed
     * @var bool
     */
    protected static $needsSeed = true;
    
        /**
     * setupBeforeClass
     * @static
     * @access public
     *
     *
     */
    public function setup()
    {
        parent::setUp();

        if (self::$needsSeed) {

            $start = microtime(true);

            Artisan::call('migrate:refresh', ['--force' => true]);

            DB::commit();

            $this->seed();

            self::$needsSeed = false;

            $seedTime = microtime(true) - $start;

            Log::info(__METHOD__ . " : Migrate and Seed took " . $seedTime);
        }

    }
}