2

I'm trying to get a Laravel project running at my new work. The developer who left didn't specify how to get the code up and running in the handover document and no-one else knows either.

The SQLite database is created with :memory:, and php artisan migrate works without errors.

...
Migrating: 2016_06_01_000004_create_oauth_clients_table
Migrated:  2016_06_01_000004_create_oauth_clients_table
...

The method beginDatabaseTransaction() in the trait (standard Laravel code) is being executed, but none of the tables exist afterwards. It's as if the database is being created but the migration is not being executed.

<?php

namespace Illuminate\Foundation\Testing;

trait DatabaseTransactions
{
    /**
     * Handle database transactions on the specified connections.
     *
     * @return void
     */
    public function beginDatabaseTransaction()
    {
        $database = $this->app->make('db');

        foreach ($this->connectionsToTransact() as $name) {
            $database->connection($name)->beginTransaction();
        }

        $this->beforeApplicationDestroyed(function () use ($database) {
            foreach ($this->connectionsToTransact() as $name) {
                $connection = $database->connection($name);

                $connection->rollBack();
                $connection->disconnect();
            }
        });
    }

Test code (the last shown instruction fails):

<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Laravel\Passport\ClientRepository;

class PassportTest extends TestCase
{
    use DatabaseTransactions;

    public function test_basic_oauth_token_authentication(){
      $clientRepository = new ClientRepository();
      $client = $clientRepository->createPersonalAccessClient(
          null, 'Test Personal Access Client', $this->baseUrl
      );

Command line:

vendor/bin/phpunit tests/Feature/PassPortTest

Error message:

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: oauth_clients
Caused by  
Doctrine\DBAL\Driver\PDOException: SQLSTATE[HY000]: General error: 1 no such table: oauth_clients
Caused by
PDOException: SQLSTATE[HY000]: General error: 1 no such table: oauth_clients

How can I ensure that the tables have been created as part of each test?

15
  • 1
    Try calling artisan migrate in the setUp function Commented Aug 21, 2018 at 12:07
  • 1
    @DigitalDrifter Another site suggested calling Artisan::call('migrate'); as the first thing in th test, but that had no effect. Commented Aug 21, 2018 at 12:09
  • 1
    Is there anything extra going on in the TestCase class? Commented Aug 21, 2018 at 12:12
  • 3
    Use RefreshDatabase trait. laravel.com/docs/5.6/… Commented Aug 21, 2018 at 12:12
  • 1
    @DigitalDrifter OK. Using RefreshDatabase instead of DatabaseTransactions gets me past that error (to an unrelated error). Trying touse both gives a PHP error due to conflicting method names. Commented Aug 21, 2018 at 12:19

2 Answers 2

3

Use the RefreshDatabase trait instead of the DatabaseTransactions trait (they have conflicts if you try to use both in the same class).

See the documents about resetting the database after each test.

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

Comments

2

There's a caveat when using :memory: in your SQLite config. The memory location that your command-line migrations are hitting is not the same one that the test is attempting to talk to. Each instance of your app basically spins up a new virtual temporary database.

You have two options. Either replace the :memory: config with an explicit filename, or make your test's setUp() method actually initiate the \Aritsan::call('migrate') call. I wouldn't recommend the latter though, just because it could be slow, and I'm not entirely certain the migrations would actually stick if you have Transactions active at the time.

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.