title: Laravel phpunit multiple databases tests not running
body: <p>We have a multi-tenancy set up with one main and other tenant databases and having some problems running non-sqlite tests. Databases are mysql. </p>
  
  <p>Currently trying to run tests against the main database and one tenant database. The connections in the model constructors are set like so: </p>
  
  <p>BaseMainModel </p>
  
  <p><code>$this-&gt;connection = config('database.main');</code></p>
  
  <p>BaseTenantModel</p>
  
  <p><code>$this-&gt;connection = config('database.tenant');</code></p>
  
  <p>I observe the following:</p>
  
  <p>Test are running fine with an sqlite in memory databse with the following settings in phpunit.xml:</p>
  
  <pre><code>&lt;env name="APP_ENV" value="testing"/&gt;
  &lt;env name="CACHE_DRIVER" value="array"/&gt;
  &lt;env name="SESSION_DRIVER" value="array"/&gt;
  &lt;env name="QUEUE_DRIVER" value="sync"/&gt;
  &lt;env name="APP_DEBUG" value="true"/&gt;
  &lt;env name="DB_CONNECTION" value="testing"/&gt;
  &lt;env name="DB_CONNECTION_TENANT" value="testing"/&gt;
  </code></pre>
  
  <p>In <code>config.database.connections</code></p>
  
  <pre><code>    'testing' =&gt; [
          'driver' =&gt; 'sqlite',
          'database' =&gt; ':memory:',
          'prefix' =&gt; '',
      ],
  </code></pre>
  
  <p>However, when running without the sqlite in memory database (so commenting out my two phpunit lines for <code>&lt;env name="DB_CONNECTION" value="testing"/&gt;</code> and <code>&lt;env name="DB_CONNECTION_TENANT" value="testing"/&gt;</code>) and running on my normal database, the tests start hanging after the first test. </p>
  
  <p>So for example, if I have 5 tests in a file <code>SomeTests.php</code>, the first one completes, and the process hangs on the second one. </p>
  
  <p>If I add the <code>protected $connectionsToTransact = ['main', 'tenant'];</code> to my <code>TestCase</code> class, even the first test doesn't complete. </p>
  
  <p>Another thing to note is that I am overriding the <code>refreshTestDatabase()</code> method of the <code>RefreshDatabase</code> trait by creating my own trait and using laravel's trait. My trait looks like so:</p>
  
  <pre><code>trait RefreshDatabase
  {
  use BaseRefreshDatabase;
  
  /**
   * Refresh the in-memory database.
   *
   * @return void
   */
  protected function refreshInMemoryDatabase()
  {
      $this-&gt;artisan('migrate');
  
      $path = 'database' . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR . 'tenants';
  
      Artisan::call('migrate', [
          '--database' =&gt; 'testing',
          '--path' =&gt; $path
      ]);
  
      $this-&gt;app[Kernel::class]-&gt;setArtisan(null);
  }
  
  /**
   * Refresh a conventional test database.
   *
   * @return void
   */
  protected function refreshTestDatabase()
  {
      if (!RefreshDatabaseState::$migrated) {
  
          $this-&gt;artisan('migrate:fresh');
  
          $path = 'database' . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR . 'tenants';
  
          $this-&gt;artisan('migrate:fresh', [
              '--path' =&gt; $path,
              '--database' =&gt; config('database.tenant')
          ]);
  
          $this-&gt;app[Kernel::class]-&gt;setArtisan(null);
  
          RefreshDatabaseState::$migrated = true;
      }
  
      $this-&gt;beginDatabaseTransaction();
  }
  }
  </code></pre>
  
  <p>I tried dumping in my test's <code>setUp</code> function: </p>
  
  <pre><code>class RelationsTest extends TestCase
  {
  use RefreshDatabase;
  
  protected function setUp()
  {
      parent::setUp();
      dump("here");  // this is logged
      $this-&gt;product = factory(Product::class)-&gt;create();
      dump("product created"); // this is not. However I dumped in the model constructors and it did enter the Product constructor
      $this-&gt;someClass= factory(SomeClass::class)-&gt;create();
      $this-&gt;user = factory(User::class)-&gt;create();
      $this-&gt;otherClass = factory(OtherClass::class)-&gt;create();
  
      $this-&gt;yetAnotherClass= factory(YetAnotherClass::class)-&gt;create([
          'owner_id' =&gt; $this-&gt;user-&gt;id,
          'product_id' =&gt; $this-&gt;product-&gt;id,
          'someClass_id' =&gt; $this-&gt;someClass-&gt;id,
      ]);
  }
  </code></pre>
  
  <p>Interestingly, if I run every test in the file separately, they run fine. So something happens in between the tests that I don't understand! </p>
  
  <p>I have a feeling it has something to do with my model connections (or something). Because if I run two basic tests with just <code>assertTrue(true)</code> (not using factories or touching the database), they run fine. </p>
  
  <p>Help is much appreciated! Thanks :)</p>