title: Laravel 5 Reseeding the Database for Unit Testing Between Tests
body: <p>I start with a seeded database and am trying to reseed the database between unit tests in Laravel 5.  In Laravel 4 I understand you could simply use Illuminate\Support\Facades\Artisan and run the commands</p>
  
  <pre><code>Artisan::call('migrate');
  Artisan::call('db:seed');
  </code></pre>
  
  <p>or you supposedly could do:</p>
  
  <pre><code>$this-&gt;seed('DatabaseSeeder');
  </code></pre>
  
  <p>before every test.  In Laravel 5 this appears to have been replaced by  </p>
  
  <pre><code>use DatabaseMigrations;
  </code></pre>
  
  <p>or </p>
  
  <pre><code>use DatabaseTransactions;
  </code></pre>
  
  <p>I have tried using these and have managed to get the tests to migrate the database; however, it doesn't actually reseed the data in the tables.  I have read through several forums complaining about this and have tried several different approaches calling these from the TestCase and inside every Test...adding the </p>
  
  <pre><code>    $this-&gt;beforeApplicationDestroyed(function () {
          Artisan::call('migrate');
          Artisan::call('migrate:reset');
          Artisan::call('db:seed');
          DB::disconnect();
      });
  </code></pre>
  
  <p>to the TestCase.php <code>tearDown()</code>...</p>
  
  <p>I have also tried adding </p>
  
  <pre><code>$this-&gt;createApplication();
  </code></pre>
  
  <p>to a method called in every test from TestCase.php </p>
  
  <p>Sometimes it just wipes my tables out completely.  Nothing I am finding on Laravel's site or in blogs seems to work.  Part of it is probably because I'm probably trying Laravel 4 methods in Laravel 5.  Is there any way to do this in Laravel 5?</p>
  
  <p>My code for the testcase.php looks like:</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Support\Facades\Artisan as Artisan;
  
  class TestCase extends Illuminate\Foundation\Testing\TestCase{
  
      use Illuminate\Foundation\Testing\WithoutMiddleware;
      use Illuminate\Foundation\Testing\DatabaseMigrations;
      use Illuminate\Foundation\Testing\DatabaseTransactions;
  
      protected $baseUrl = 'http://localhost';
  
  
      public function initializeTests(){
  
          $this-&gt;createApplication();
  
         Artisan::call('migrate');
         $this-&gt;artisan('migrate');
         Artisan::call('db:seed');
         $this-&gt;artisan('db:seed');
         $this-&gt;seed('DatabaseSeeder');
         $this-&gt;session(['test' =&gt; 'session']);
         $this-&gt;seed('DatabaseSeeder');
  
      }
  
      public function tearDown()
      {
        Mockery::close();
        Artisan::call('migrate:reset');
        $this-&gt;artisan('migrate:reset');
        Artisan::call('migrate:rollback');
        $this-&gt;artisan('migrate:rollback');
        Artisan::call('migrate');
        $this-&gt;artisan('migrate');
        Artisan::call('db:seed');
        $this-&gt;artisan('db:seed');
        $this-&gt;seed('DatabaseSeeder');
        DB::disconnect();
  
           foreach (\DB::getConnections() as $connection) {
               $connection-&gt;disconnect();
           }
  
        $this-&gt;beforeApplicationDestroyed(function () {
           Artisan::call('migrate:reset');
           $this-&gt;artisan('migrate:reset');
           Artisan::call('migrate:rollback');
           $this-&gt;artisan('migrate:rollback');
           Artisan::call('migrate');
           $this-&gt;artisan('migrate');
           Artisan::call('db:seed');
           $this-&gt;artisan('db:seed');
           $this-&gt;seed('DatabaseSeeder');
           DB::disconnect();
           foreach (\DB::getConnections() as $connection) {
               $connection-&gt;disconnect();
           }
        });
  
         $this-&gt;flushSession();
         parent::tearDown();
  
  
      }
  
      public function getConnection()
      {
          $Connection = mysqli_connect($GLOBALS['DB_DSN'], $GLOBALS['DB_USERNAME'], $GLOBALS['DB_PASSWORD'], $GLOBALS['DB_DATABASE']);
          $this-&gt;createDefaultDBConnection();
          return $this-&gt;Connection;
      }
  
      public function createApplication()
      {
          $app = require __DIR__.'/../bootstrap/app.php';
  
          $app-&gt;make(Illuminate\Contracts\Console\Kernel::class)-&gt;bootstrap();
  
          return $app;
      }
  
      /**
       * Magic helper method to make running requests simpler.
       *
       * @param $method
       * @param $args
       * @return \Illuminate\Http\Response
       */
      public function __call($method, $args)
      {
          if (in_array($method, ['get', 'post', 'put', 'patch', 'delete']))
          {
              return $this-&gt;call($method, $args[0]);
          }
  
          throw new BadMethodCallException;
      }
  
      /**
       * Create a mock of a class as well as an instance.
       *
       * @param $class
       * @return \Mockery\MockInterface
       */
      public function mock($class)
      {
          $mock = Mockery::mock($class);
  
          $this-&gt;app-&gt;instance($class, $mock);
  
          return $mock;
      }
  
  }
  </code></pre>
  
  <p>My Test looks something like</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Foundation\Testing\WithoutMiddleware;
  use Illuminate\Foundation\Testing\DatabaseMigrations;
  use Illuminate\Foundation\Testing\DatabaseTransactions;
  use Illuminate\Database\Seeder;
  use Illuminate\Support\Facades\Artisan;
  
  class CustomerRegistrationControllerTest extends TestCase
  {
  
      use DatabaseMigrations;
  
      protected static $db_inited = false;
  
      protected static function initDB()
      {
          echo "\n---Customer Registration Controller Tests---\n"; // proof it only runs once per test TestCase class
          Artisan::call('migrate');
          Artisan::call('db:seed');
      }
  
      public function setUp()
      {
  
          parent::setUp();
  
          if (!static::$db_inited) {
              static::$db_inited = true;
              static::initDB();
          }
  
  //        $this-&gt;app-&gt;refreshApplication();
          $this-&gt;artisan('migrate:refresh');
          $this-&gt;seed();
          $this-&gt;seed('DatabaseSeeder');
  
          $this-&gt;initializeTests();
  
  );
  
      }
  
  
      public function testSomething()
  
      {
  
  
          $this-&gt;Mock
              -&gt;shouldReceive('destroy')
              -&gt;with('1')
              -&gt;andReturn();
  
  
          $this-&gt;RegistrationController-&gt;postRegistration();
  //      $this-&gt;assertResponseStatus(200);
  
      }
  
  }
  </code></pre>