lara65535's avatar

Models Using Invalid Memory Sqlite DB

AuthenticationTest

<?php

namespace Tests\Unit\Authentication;

use App\Club;
use Artisan;
use Tests\TestCase;

/**
 * Route::post('/do/login', 'Auth\LoginController@login')->name('do.login');
 * Route::post('/do/logout', 'Auth\LoginController@logout')->name('do.logout');
 */
class AuthenticationTest extends TestCase
{
    public function testLoginPage()
    {
//        dd(Club::find(1)); <-- WORKS!
        $this->get(route('index'))
            ->assertStatus(200); // <-- FAILS
    }
}

Which extends...

<?php

namespace Tests;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\Artisan;

class TestCase extends BaseTestCase
{
    use RefreshDatabase;

    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        putenv('DB_CONNECTION=testing');

        $app = require __DIR__ . '/../bootstrap/app.php';

        $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

        return $app;
    }

    public function setUp()
    {
        parent::setUp();

        Artisan::call('db:seed');
    }
}

Now this single test will not work. I have a View Service Provider file that captures a club, but it keeps saying the club table does not exist, but it does as the dd(Club::find(1)) inside the test works...

ViewServiceProvider

<?php

namespace App\Providers;

use App\Alert;
use App\Club;
use Carbon\Carbon;
use function dd;
use Exception;
use Illuminate\Support\ServiceProvider;

/**
 * Class ViewComposerServiceProvider
 *
 * @package App\Providers
 */
class ViewComposerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services
     *
     * @return void
     */
    public function boot()
    {
        try {
            view()->share('club', Club::whereId(1)
                ->with(['president', 'vice_president', 'secretary', 'treasurer', 'trap_chairman', 'scorekeeper'])
                ->first()
            );
            view()->share('alerts', Alert::whereDate('expiration', '>', Carbon::today()->toDateString())->get());
        } catch (Exception $exception) {
// Unit tests fail here with a table not found exception...
//            dd($exception->getMessage(), config('database.default')); 
        }
    }

    /**
     * Register the application services
     *
     * @return void
     */
    public function register()
    {
    }
}

What am I missing? Why is the application not using the in memory database that exists and trying to use a different one?

0 likes
1 reply
lara65535's avatar
lara65535
OP
Best Answer
Level 1

Strange stuff is going on with the ServiceProviders when using an in memory database. I ended up moving the code for the view variables into a middleware which successfully uses the in memory address.

Please or to participate in this conversation.