I've been having some weird errors with phpunit and Laravel Dusk. My first issue was the enviornment variables set in phpunit.xml were not being honored. I am guessing that was b/c of the local .env file. I solved that by using force=true on variables in my XML file. As a side question, is that the correct solution?
My real question is on the weird error in the subject line. During the setup process, I get this error:
PDOException: SQLSTATE[HY000]: General error: 1 table "migrations" already exists
However, before the parent::setUp() is run (where this error is thrown), I tested dd(env('DB_CONNECTION')) and dd(env('DB_DATABASE'))
They returned 'sqlite' and ':memory:' as expected. So....if it's an in memory database, how does the DB table already exist?
I am at a loss on why nothing is working. I had to add force="true" to all env vars in phpunit.xml to get this far and then removed the :memory: database. Now it just says the tables don't exist so it's obviously not running migrations.
I will also add I am on Homestead if that matters at all.
My phpunit.xml (I removed the DB_DATABASE entry for :memory: since it was giving the error above.
How my TestClass is setup. It fails when trying to create a user, says the table doesn't exist (Migrations are all in place; it's worked in the past...)
<?php
namespace Tests\Unit;
use App\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
class ApplicationTest extends TestCase
{
use DatabaseMigrations;
public function setUp()
{
//dd(env('DB_CONNECTION')); // these are correct
//dd(env('DB_DATABASE'));
parent::setUp();
$this->user = create('App\User');
$this->signIn($this->user);
}
}
OK I finally got there. Because I am using a hybrid mysql & mongodb setup, my User model required this:
class User extends SparkUser
{
use HybridRelations;
protected $connection = 'mysql';
I updated my user constructor to look like this, and all tests pass:
public function __construct(array $attributes = [])
{
// For unit tests
if (env('APP_ENV') === 'testing') {
$this->setConnection('sqlite');
}
parent::__construct($attributes);
}