Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Christofer's avatar

Test Successful, but fails in full phpunit test all

Issue: Test does not work when running phpunit on whole test folder.

My test passes "green" when I use --filter TestFunctionName

Update: It may have something to do with boot::saving() calculating and updating the record when saving.

I've spent a long time today trying to debug code and fix code that was not broken, waaahhh. ;-(

Is this one of those "Testing with static functions is bad" scenarios... "eloquent is bad for testing," etc.?

Just hoping one of you have been through this and know the solution, or at lest the cause so that I can avoid it when testing.

I'm suspecting now I may need to group my tests into folders that wont cause clashes... or something like that :-/ Or, it's quite possible I've broken some serious law/principle of coding and it's clashing in the static universe. :-)

0 likes
5 replies
MikeHopley's avatar

This usually happens because the test database is not being reset between tests.

Tests should be independent. You want a separate testing database, and it should be reset to a default state before every test.

This can be configured in various ways. Laravel comes with a few useful traits, including RefreshDatabase in 5.5.

3 likes
Christofer's avatar

@MikeHopley Okay, thank you.

I'd like to know if this kind of issue can occur during normal operation, when an operation requires creating and updating multiples of the same model type. e.g. a Sale with many Items.

I feel as though the underlying issue has something to do with boot static::saving(), but I haven't been able to prove it yet. Though that is where the code is to update the details and where the information magically disappears.

MikeHopley's avatar
Level 17

I'd like to know if this kind of issue can occur during normal operation, when an operation requires creating and updating multiples of the same model type. e.g. a Sale with many Items.

Er...I wouldn't expect that to happen, no.

Honestly, I would fix the obvious problem first. Check that your test database is refreshing between tests. This is an extremely common issue.

I actually have a test that checks for this. Try running this, or something similar:

<?php
namespace Tests\Feature;

use App\User;
use Tests\Testcase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class DatabaseSetupTest extends Testcase
{
    use RefreshDatabase;

    public function test_changes_persist_inside_test()
    {
        assertGreaterThan(0, User::count());

        User::truncate();

        assertEquals(0, User::count());
    }

    public function test_resets_database_between_tests()
    {
        assertGreaterThan(0, User::count());
    }
    
}

I keep this test around. When it fails, I know my testing database setup has gone wrong. I also have a similar (more complex) "sanity check" test for my Laravel Dusk setup, which I find very useful.

If you comment out the use RefreshDatabase line, the test will fail.

1 like
MikeHopley's avatar

No problem, hope it solves the issue. :) If nothing else, you can rule it out, which is also useful.

Please or to participate in this conversation.