channaveer's avatar

SQLite no such tables.

I am setting up a Test case for the existing project. But I am getting SQLite no such tables error.

Following is my setup

phpunit.xml

<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>

AuthControllerTest.php

class AuthControllerTest extends TestCase
{
    use RefreshDatabase;

    /**
     * @test
     */
    public function guest_user_can_view_login_page()
    {
        $this->withoutExceptionHandling();

        $response = $this->get(route("auth.login"));

        $response->assertViewIs("auth.login");
    }
}

Also, there is a code in AppServiceProvider where we write a piece of code that needs to be accessed in every Blade View files.

$channels = (new ChannelService())->getCachedOrFreshChannels();

view()->composer('*', function ($view) use ($channels) {
    $view->with([
        'channels'                      => $channels->filter(function ($channel) {
            if (empty($channel->course_category_id)) {
                return $channel;
            }
        }),
        //... other code snippets for view page
    ])
})

How to handle the test cases for such situations for which the code is in AppServiceProvider

When I comment the code of AppServiceProvider I get the following error

BadMethodCallException: SQLite doesn't support dropping foreign keys (you would need to re-create the table).

I know that from Laravel 5.7 they have changed few things where SQLlite doesn't support the dropping of foreign keys. How should I handle this case. I mean should I put some condition in migrations and check if the database is SQLite then dont do this or that.

0 likes
3 replies
kevinbui's avatar

This thread might help. You might have to enable foreign_key_constraints.

1 like
channaveer's avatar

How to fix the issue in AppServiceProvider for the following code.

view()->composer('*')

I am getting why AppServiceProvider code throws error while Testing.

Please or to participate in this conversation.