7

I am running Laravel, on OS X using MAMP, and I am trying to do some unit testing, using PHPUnit, and sqlite, however when I run my tests, I get the error

General error: 1 no such table: users

I've tried running the artisan, to manually migrate the database, using the --env=testing, which runs fine, however I still get the error. I even call Artisan::call('migrate'); in the SetUp method.

/app/config/testing/database.php

return [
    'default' => 'sqlite',

    'connections' => [
        'sqlite' => [
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => ''
        ],
    ]
];

EDIT: Migration file:

Schema::create('users', function($table) {
    $table->increments('id');
    $table->string('email', 32)->unique();
    $table->string('password', 64);
    $table->string('username', 32)->unique();
    $table->dateTime('birthdate');
    $table->enum('status', array('ACTIVE', 'INACTIVE'));
    $table->timestamps();
});

Schema::create('roles', function($table) {
    $table->increments('id');
    $table->string('name', 32);
});

Schema::create('role_user', function ($table) {
    $table->increments('id');

    $table->unsignedInteger('user_id');
    $table->unsignedInteger('role_id');

    $table->foreign('user_id')->references('id')->on('users');
    $table->foreign('role_id')->references('id')->on('roles');

});
3
  • Can you show us your migration that create the table users ? Which DB do you use in the rest of your app ? MySQL ? Commented Feb 21, 2014 at 7:47
  • Yes, it's mysql, I will add the migration file when i get back to work. Commented Feb 22, 2014 at 0:02
  • Added the migration in the edit Commented Feb 23, 2014 at 21:48

2 Answers 2

10

SQLite doesn't support the ENUM type : http://www.sqlite.org/datatype3.html

That's why the users table isn't created. You will have to run your tests on a classic MySQL table or remove all the ENUM types.

Sign up to request clarification or add additional context in comments.

2 Comments

Solved my problem, went with a "Enum" table instead. Thank you.
I've been struggling with this for the past 2 years
7

Running migrations before running the test doesn't work because, as your config shows, you db is in memory, so every time you run a new test, it runs migrations for each testing session and completely destroys all testing data when it finishes.

To use the memory database, run your migrations each time you run your tests, extend your classes from TestCase with something like:

<?php

class TestCase extends Illuminate\Foundation\Testing\TestCase {

  public function createApplication()
  {
      $unitTesting = true;
      $testEnvironment = 'testing';
      return require __DIR__.'/../../bootstrap/start.php';
  }

  public function setUp()
  {
      parent::setUp();
      $this->prepareForTests();
  }
  private function prepareForTests()
  {
      Artisan::call('migrate');
      Artisan::call('db:seed');
  }
  public function tearDown()
  {
      parent::tearDown();
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.