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.