Lars-Janssen's avatar

Tests fails with sqlite but works with mysql?

Hi,

I've got a method that looks like this:

protected function saveDescriptions($descriptionsShort, $descriptionsLong, $productId)
{
    /*
     * Check if product description short already exists
     * otherwise create one.
     */

    foreach ($descriptionsShort as $description) {
        if(! ProductDescriptionModel::where(
            'description', $description
        )->where(
            'product_id', $productId
        )->where('type', 'short')->first()) {

            /*
            * If product description short does not exists create one.
            */

            ProductDescriptionModel::create([
                'product_id'    => $productId,
                'description'   => $description,
                'type'          => 'short'
            ]);
        }
    }

This is my test:

 /** @test */
    public function it_creates_product_descriptions_short_correctly()
    {
        $this->assertDatabaseMissing('product_description', [
            "product_id" => 1,
            "description" => "Short description...",
            "type" => "short"
        ]);

        $this->assertDatabaseMissing('product_description', [
            "product_id" => 1,
            "description" => "test",
            "type" => "short"
        ]);

        $this->saveProducts(__dir__ . '/xml/Descriptions.xml');

        $this->assertDatabaseHas('product_description', [
            "product_id" => 1,
            "description" => "Short description...",
            "type" => "short"
        ]);

        $this->assertDatabaseHas('product_description', [
            "product_id" => 1,
            "description" => "test",
            "type" => "short"
        ]);
    }

With this settings in my phpunit.xml file it fails:

<php>
        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="sqlite" />
        <env name="DB_DATABASE" value=":memory:" />
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>

but with this it works:

 <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_DEFAULT" value="sqlite_testing" />
    </php>

What could be the problem? Thanks!

0 likes
5 replies
ohffs's avatar

When you say 'it fails' what do you mean?

lostdreamer_nl's avatar

I think you dont migrate / seed your testing DB in between your tests.

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

This will not create a DB for you that can be saved to disk, so before running any tests, you have to do a complete migration or ou will have an empty DB with no tables.

But like ohffs says: It would be easier for us if we knew how it fails

Lars-Janssen's avatar

@ohffs @lostdreamer_nl sorry for the late reaction. It fails like this:

2) Tests\Unit\Product\Description\ProductDescriptionShortTest::it_creates_product_descriptions_short_correctly
Failed asserting that a row in the table [product_description] matches the attributes {
    "product_id": 1,
    "description": "test",
    "type": "short"
}.

Found: [
    {
        "id": "1",
        "product_id": "1",
        "description": "Test",
        "type": "short",
        "created_at": "2018-01-16 15:03:52",
        "updated_at": "2018-01-16 15:03:52"
    },
    {
        "id": "2",
        "product_id": "1",
        "description": "Short description...",
        "type": "short",
        "created_at": "2018-01-16 15:03:52",
        "updated_at": "2018-01-16 15:03:52"
    },
    {
        "id": "3",
        "product_id": "1",
        "description": "Test",
        "type": "long",
        "created_at": "2018-01-16 15:03:52",
        "updated_at": "2018-01-16 15:03:52"
    }
] and 1 others.

if I put it like this in my phpunit file:

 <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_DEFAULT" value="sqlite_testing" />
    </php>

It works?

Cronix's avatar
Cronix
Best Answer
Level 67

It looks like the problem is you're searching for "test" when it's stored as "Test" (sqlite is case sensitive by default). So just change your search values in the queries to match those that are actually in the database.

1 like

Please or to participate in this conversation.