1

I'm learning Laravel and Functional tests. On laracast we have in phpunit.xml:

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

I can not test on mysql database because when I switch my DB_CONNECTION param to "mysql" I have error "PDOException: SQLSTATE[HY000] [1049] Unknown database ':memory:'".

When I don't use memory all tables are removed from my database:
My Test Class looks like that:

class ThreadsTest extends TestCase
{

    use DatabaseMigrations;

    /** @test */
    public function a_user_can_browse_threads()
    {
        $response = $this->get('/threads');
        $response->assertStatus(200);
    }
}

Does anybody know how can I test in memory with MySQL? I can't find any information

3 Answers 3

2

You can run with separete database

    <env name="DB_CONNECTION" value="mysql"/>
    <env name="DB_HOST" value="localhost"/>
    <env name="DB_DATABASE" value="test"/>
    <env name="DB_USERNAME" value="root" />
    <env name="DB_PASSWORD" value="" />
Sign up to request clarification or add additional context in comments.

Comments

0

That's not possible. You can't run MySQL as an in-memory database as you can with SQLite.

6 Comments

So how we can test on MySQL ? All tables are removed after run tests. What is correct way to testing on MySQL database ?
Do you have MySQL installed on your system?
Yes. Only MySQL (XAMPP)
Create a separate database for your tests and add it to phpunit.xml. You can wrap your tests in transactions to rollback the changes: laravel.com/docs/5.6/…
An application written for MySQL doesn't always work with SQLite because of missing database features. Even if it does, testing with SQLite is not a replacement for testing with the production environment's DBMS.
|
0

Simply change the parameters

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

to

<php>
    <env name="DB_CONNECTION" value="mysql"/>
    <env name="DB_DATABASE" value="your_test_instance"/>
</php>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.