Hello,
I'm just starting out with implementing unit tests in Laravel using Pest. I have an application running with FilamentPHP, and I have conducted a few tests that operate with a GitHub action.
It happens that some tests fail, but not consistently. I have a problem retrieving the object that has just been created by the factory... For exemple :
php No query results for model [App\Models\Order] 1 //Not always ID 1
From time to time a test fails with this error, the next time, when I run the tests again, the test that had an error works but the next one fails. In short, I'm having trouble understanding why...
A configuration issue? A problem with the test DB? My factories?
I tried in two ways, either directly with the factory or by retrieving the Model (see examples below).
it('can validate an order from IN_ORDER status', function () {
$order = Order::factory()->create(['status' => Status::IN_ORDER]);
// If I dd() the $order, I have a result
livewire(ViewOrder::class, ['record' => $order->getRouteKey()])
->call('validateOrder')
->assertHasNoActionErrors();
$order->refresh();
expect($order->status)->toBe(Status::VALIDATED);
$activity = Activity::where('subject_id', $order->id)
->where('event', 'validate')
->latest()
->first();
expect($activity)->not->toBeNull();
});
And another one :
it('builds the correct title with the reference', function () {
Order::factory()->create(['reference' => '123456']);
$order = Order::latest()->first();
$component = livewire(
ViewOrder::class,
['record' => $order->getRouteKey()]
); // Here it tells me that getRouteKey() is null.
$translationKey = $component->instance()->getTitle();
$expectedTitle = __($translationKey) . ' #' . $order->reference;
expect($expectedTitle)->toContain('#123456');
});
Here my TestCase.php :
<?php
namespace Tests;
use Database\Seeders\production\ShieldSeeder;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->seed(ShieldSeeder::class);
}
}
And my phpunit.xml :
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory>tests/Feature</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>app</directory>
</include>
</source>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
<env name="APP_LOCALE" value="en"/>
<env name="APP_FALLBACK_LOCALE" value="en"/>
<env name="APP_FAKER_LOCALE" value="en_US"/>
</php>
</phpunit>
Do you have any idea where the problem might be coming from?
Thank you 😅