orchestral/testbench

SQLSTATE[HY000]: General error: 1 no such table: users

Closed this issue · 2 comments

  • Testbench Version: 7.x
  • Laravel Version: 9.19
  • PHP Version: 8.1.7
  • Database Driver & Version: sqlite in memory

Description:

Table not found when tests try to refresh the database.

Steps To Reproduce:

run composer test

You get this error:
SQLSTATE[HY000]: General error: 1 no such table: users

This is my service provider:

<?php

namespace Package\Namespace;

use Illuminate\Support\ServiceProvider;

class PackageServiceProvider extends ServiceProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'packageconfigfile');
    }
    /**
     * Bootstrap the application events.
     * 
     * @return void
     */
    public function boot()
    {
        
        // merge config
        if ($this->app->runningInConsole()) {
            $this->publishes([
               __DIR__.'/../config/config.php' => config_path('packageconfigfile.php'), 
            ], 'config');

            $this->registerMigrations();
        }
        
        // register migrations

        // register fusion auth client
    }

    protected function registerMigrations()
    {
        $this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
    } 

}

And this is the main TestCase.php file:

<?php

namespace Package\Namespace;

use Package\Namespace\FusionAuthLaravelServiceProvider;

class TestCase extends \Orchestra\Testbench\TestCase
{

    public function setUp(): void
    {
        parent::setUp();
    }
    
    protected function getEnvironmentSetUp($app)
    {
        $app['config']->set('database.default', 'testbench');
        $app['config']->set('database.connections.testbench', [
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => '',
        ]);
        include_once __DIR__ . '/../database/migrations/create_users_table.php.stub';
        (new \CreateUsersTable)->up();  
    }

    protected function getPackageProviders($app)
    {
      return [
        PackageServiceProvider::class,
      ];
    }
}

And this is the failing test due to this issue:

<?php

namespace Package\Namespace\Tests\Feature\Auth;

use Package\Namespace\Tests\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

class AuthenticationTest extends \Orchestra\Testbench\TestCase
{
    use RefreshDatabase;

    /** @test */
    public function test_authentication_is_working()
    {   
        User::factory()->create();
        $this->assertTrue(true);
    }
}

Any idea why this is happening?

Solved. I was extending the wrong class in AuthenticationTest

@celyes What was the correct class to extend?