stueynet's avatar

In memory database leads to The table is empty.

Decided to try out the in memory DB for testing on a new project. Using Homestead and the latest Laravel. Can't seem to get things working no matter what I try. Been through all of the threads on all the sites and nothing seems to be working.

1) Tests\Feature\UserTest::a_guest_should_be_able_to_register
Failed asserting that a row in the table [users] matches the attributes {
    "email": "[email protected]",
    "name": "TestUser"
}.

The table is empty.

Here is my stuff:


//config/database.php


    'connections' => [
        'testing' => [
            'driver' => 'sqlite',
            'database' => ':memory:',
            'prefix' => '',
        ],


// phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="MAIL_DRIVER" value="array"/>
    </php>
</phpunit>

I have been clearing my config cache constantly.

TestCase.php uses the RefreshDatabase trait and I have even done this in a number of places down the testing chain to confirm its the right database being used during the tests:

        $connection = config('database.default');

        $driver = config("database.connections.{$connection}.database");
        dd($driver);

Anyone have any other tricks they know of to get this working?

0 likes
20 replies
staudenmeir's avatar

Does this affect multiple/all of your tests? Are you sure that the tests are working? Have you run them on a different database before?

1 like
stueynet's avatar

All the setup is correct. Tried it with multiple database types including :memory: and a full mysql database. Been troubleshooting this for two weeks as I continue to build with no tests.

staudenmeir's avatar

Wrap the code that should create the user in \DB::enableQueryLog(); [...] dd(\DB::getQueryLog()); and check the output.

stueynet's avatar

Here is is:

        \DB::enableQueryLog();
        $this->post('register', [
            'username' => 'TestUser',
            'email' => '[email protected]',
            'password' => 'Password123',
            'password_confirmation' => 'Password123',
            'terms' => '1',
        ]);

        dd(\DB::getQueryLog());

Output:

[]
tykus's avatar

So, no query....do you have a validation failure? Can you dump the response?

$response = $this->post('register', [
    'username' => 'TestUser',
    'email' => '[email protected]',
    'password' => 'Password123',
    'password_confirmation' => 'Password123',
    'terms' => '1',
]);
$response->dump();
stueynet's avatar

Weird it seems to be dumping the whoops page. Edit I don't have the ->dump() method.

stueynet's avatar

This is the entire test file:

<?php

namespace Tests\Feature;

use App\User;
use Tests\TestCase;

class UserTest extends TestCase
{
    /**
     * @test
     */
    public function a_guest_should_be_able_to_register()
    {
        $this->post('register', [
            'username' => 'TestUser',
            'email' => '[email protected]',
            'password' => 'Password123',
            'password_confirmation' => 'Password123',
            'terms' => '1',
        ]);

        $this->assertDatabaseHas('users', [
            'email' => '[email protected]',
            'username' => 'TestUser',
        ]);
    }

    /**
     * @test
     */
    public function a_guest_should_be_able_to_see_the_register_page()
    {
        $response = $this->get('/register');

        $response->assertStatus(200);
    }

    /**
     * @test
     */
    public function a_guest_should_be_able_to_see_the_login_page()
    {
        $response = $this->get('/login');

        $response->assertStatus(200);
    }

    /**
     * @test
     */
    public function a_loggedin_user_should_not_be_able_to_see_the_login_page()
    {
        $user = factory(User::class)->create();
        $this->actingAs($user);

        $response = $this->get('/login');

        $response->assertRedirect('/dashboard');
    }

    /**
     * @test
     */
    public function a_loggedin_user_should_not_be_able_to_see_the_register_page()
    {
        $user = factory(User::class)->create();
        $this->actingAs($user);

        $response = $this->get('/register');

        $response->assertRedirect('/dashboard');
    }



    /**
     * @test
     */
    public function a_guest_should_be_able_to_login_with_valid_credentials()
    {
        $user = factory(User::class)->create();

        $response = $this->post('/login', [
            'email' => $user->email,
            'password' => 'secret'
        ]);
        $response->assertStatus(302);
        $this->assertAuthenticatedAs($user);
    }

    /**
     * @test
     */
    public function a_loggedin_user_can_see_the_dashboard()
    {
        $user = factory(User::class)->create();
        $this->actingAs($user);

        $response = $this->get('/dashboard');

        $response->assertOk();
    }
}

and here is my Testcase file

<?php

namespace Tests;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

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

tykus's avatar

No dump(); what Laravel version?

Is that code snippet from your test?

EDIT did you assign the (TestResponse) result of $this->post(.... to a variable, and then call dump() on that?

stueynet's avatar

Laravel 5.6. The whole file is above but here is a screencast attempting to dump the things:

https://d.pr/v/kNczwW

$response->dump();

It would appear that the test response is an instance of Query Builder.

tykus's avatar

The dumped response appears to be the Whoops page; question is why? Your test should capture the Exception rather than Whoops package. Are you somehow running your tests in the local environment rather than testing?

Can you dd(get_class($response)); you should expect Illuminate\Foundation\Testing\TestResponse?

stueynet's avatar

One of the million things I tried. I have dumped all the env, every database config etc... nothing seems to work. Here is a screencast proving the environment

https://d.pr/v/6YwEAH

stueynet's avatar

And yeah its a TestResponse:

vagrant@homestead:~/Code/wpcore-app2$ vendor/bin/phpunit
PHPUnit 7.3.2 by Sebastian Bergmann and contributors.

F"Illuminate\Foundation\Testing\TestResponse"
vagrant@homestead:~/Code/wpcore-app2$
tykus's avatar

Struggling here too....

  • Can you confirm if there is a POST route with /register URI?
  • Can you check that the route is being hit correctly; maybe dd(__FILE__) in the first line of the controller to ensure you are getting to where you expect?
stueynet's avatar

Yeah it’s insane. I can tell you it’s the same issue with other tests. And definitely have the routes in place. I’m going to try your test above when I get back to the office. Thanks for the help so far really appreciated!

stueynet's avatar

Yup correct file and everything. This is for sure going to be one of insanely stupid things I am sure of it.

D9705996's avatar

What are the contents of the whoops page (You should also be able to get the error from storage/logs/Laravel.log)

I'm wondering if it might be something like a missing APP_KEY but error message will help determine

Tray2's avatar
Tray2
Best Answer
Level 74

Stupid question have you tried to use /registerinstead of just register?

Try adding $this.->withoutExceptionHandling(); in the beginning of the test.

And what is the content of the oops page?

1 like
Tray2's avatar

You are very welcome.

I've made that mistake too ans pulled what little is left of my hair ;)

Ossy's avatar

@STUEYNET - Bro am having this exact same issue, i have tried what you later did to resolve it, still having the same issue, please help

Please or to participate in this conversation.