EckyEckyPtang's avatar

Laravel 8: phpunit giving error on test

Since today, when running phpunit tests, I get:

Illuminate\Contracts\Container\BindingResolutionException: Target class [DatabaseSeeder] does not exist.

That class is there under the database/seeds directory.

I don't know if this is related, but I recently upgraded to Laravel 8 and also used the new schema dump features and pruned all migrations.

PHPunit is using an in-memory sqlite databsae.

Anyone know what is happening here?

0 likes
10 replies
SarwarAhmed's avatar

Put it in the DatabaseSeeder.php

use Illuminate\Database\Seeder;

then run 

composer dumpautoload
EckyEckyPtang's avatar

Looks like Laravel 8 has the seeder classes namespaced now.

After doing that I'm getting the following error:

Target class [Database\Seeders\Faker\Generator] does not exist.

EckyEckyPtang's avatar

Thanks, I've adjusted it, but now a different problem occurs.

When running the tests, the migrations are not executed.

Is this because I used the new schema dump feature of Laravel 8 and I don't have any migrations left?

Does it not run the generated schema when doing tests?

EckyEckyPtang's avatar

This is the abstract testcase class that all tests are extended from

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    use RefreshDatabase;

    protected function setUp() : void
    {
        parent::setUp();
        Artisan::call('migrate:fresh --seed');
    }

    protected function login($user = null)
    {
        $user = $user ?: factory('App\User')->create();

        $this->actingAs($user);

        return $this;
    }
}
EckyEckyPtang's avatar

It's not making any difference...

The SQL file from the schema dump doesn't seem to execute with an inmemory sqlite database

4unkur's avatar

Yes, just run into this. This issue is indeed related to generated dump file and sqlite in memory db for testing. I was able to fix the issue by creating a test database and specifying it in phpunit.xml:

    <server name="DB_CONNECTION" value="mysql"/>
    <server name="DB_DATABASE" value="test_db"/>
EckyEckyPtang's avatar

I wonder if people just reply here to gain points or pimp their profile.

If you don't know the solution, why reply at all?

In the meantime, I found the solution and it's explained here: https://laravel.com/docs/8.x/migrations#squashing-migrations

If you have a SQLite database for your test environment, it's not going to take into account the Schema dump. So you either change your test database to mysql or put your squashed migrations back to their original state.

4unkur's avatar

As I said earlier: I have run into this issue and found this thread. Then I figured out the problem and found the solution. So I have posted a solution, so it might help others. Sorry, if my answer was not obvious to understand.

Please or to participate in this conversation.