F
Filamentโ€ข2mo ago
Alexandre

Unit test problem in a ViewRecord resource

I'm trying to implement unit tests on a custom ViewRecord page that I created for my Order resource. I cannot understand why, but Filament is unable to retrieve the model I just created in my tests. It's not consistent, but my tests fail one time out of two... Here an exemple of one test :
it('can close an order from VALIDATED status', function () {
$order = Order::factory()->create(['status' => Status::VALIDATED]);

livewire(ViewOrder::class, ['record' => $order->getRouteKey()])
->call('clotureOrder')
->assertHasNoActionErrors();

$order->refresh();
expect($order->status)->toBe(Status::CLOSED);

$activity = Activity::where('subject_id', $order->getRouteKey())
->where('event', 'cloture')
->latest()
->first();

expect($activity)->not->toBeNull();
});
it('can close an order from VALIDATED status', function () {
$order = Order::factory()->create(['status' => Status::VALIDATED]);

livewire(ViewOrder::class, ['record' => $order->getRouteKey()])
->call('clotureOrder')
->assertHasNoActionErrors();

$order->refresh();
expect($order->status)->toBe(Status::CLOSED);

$activity = Activity::where('subject_id', $order->getRouteKey())
->where('event', 'cloture')
->latest()
->first();

expect($activity)->not->toBeNull();
});
It fails if I run vendor/bin/pest from my terminal. If I run the test directly from PHPStorm, it works... Do you know where this might be coming from? Thank you for your help ๐Ÿ™‚
No description
No description
9 Replies
Dennis Koch
Dennis Kochโ€ข2mo ago
If I run the test directly from PHPStorm, it works... Do you know where this might be coming from?
What's the exact command you run? Maybe it's the difference in the Test Runner config in PHPStorm
Alexandre
AlexandreOPโ€ข2mo ago
From my terminal I run vendor\bin\pest And from PHPStorm I can see this in the console when I click on the play button :
C:\PHP\php.exe D:\laragon\www\XXXX\vendor\pestphp\pest\bin\pest --teamcity --configuration D:\laragon\www\XXXXX\phpunit.xml D:\laragon\www\XXXX\tests\Feature\Filament\pages\Order\ViewOrderTest.php --filter "/^(P\\)?Tests\\Feature\\Filament\\pages\\Order\\ViewOrderTest::it\scan\sclose\san\sorder\sfrom\sVALIDATED\sstatus(\swith\s(data\sset\s\".*\"|\(.*\))(\s\/\s(data\sset\s\".*\"|\(.*\)))*(\s#\d+)?)?$/"
C:\PHP\php.exe D:\laragon\www\XXXX\vendor\pestphp\pest\bin\pest --teamcity --configuration D:\laragon\www\XXXXX\phpunit.xml D:\laragon\www\XXXX\tests\Feature\Filament\pages\Order\ViewOrderTest.php --filter "/^(P\\)?Tests\\Feature\\Filament\\pages\\Order\\ViewOrderTest::it\scan\sclose\san\sorder\sfrom\sVALIDATED\sstatus(\swith\s(data\sset\s\".*\"|\(.*\))(\s\/\s(data\sset\s\".*\"|\(.*\)))*(\s#\d+)?)?$/"
I use the Pest Plugin ๐Ÿ™‚ If I click on the button to run all the tests in the file, it fails, but if I run each function one after the other, it works.
C:\PHP\php.exe D:\laragon\www\XXXX\vendor\pestphp\pest\bin\pest --teamcity --configuration D:\laragon\www\XXXX\phpunit.xml D:\laragon\www\XXXX\tests\Feature\Filament\pages\Order\ViewOrderTest.php
C:\PHP\php.exe D:\laragon\www\XXXX\vendor\pestphp\pest\bin\pest --teamcity --configuration D:\laragon\www\XXXX\phpunit.xml D:\laragon\www\XXXX\tests\Feature\Filament\pages\Order\ViewOrderTest.php
Dennis Koch
Dennis Kochโ€ข2mo ago
Is it maybe a side effect from another test? If you add ->only() to your test and run vendor/bin/pest again?
Alexandre
AlexandreOPโ€ข2mo ago
Hm... Interesting...
it('can close an order from VALIDATED status', function () {
$order = Order::factory()->create(['status' => Status::VALIDATED]);

livewire(ViewOrder::class, ['record' => $order->getRouteKey()])
->call('clotureOrder')
->assertHasNoActionErrors();

$order->refresh();
expect($order->status)->toBe(Status::CLOSED);

$activity = Activity::where('subject_id', $order->getRouteKey())
->where('event', 'cloture')
->latest()
->first();

expect($activity)->not->toBeNull();
})->only();
it('can close an order from VALIDATED status', function () {
$order = Order::factory()->create(['status' => Status::VALIDATED]);

livewire(ViewOrder::class, ['record' => $order->getRouteKey()])
->call('clotureOrder')
->assertHasNoActionErrors();

$order->refresh();
expect($order->status)->toBe(Status::CLOSED);

$activity = Activity::where('subject_id', $order->getRouteKey())
->where('event', 'cloture')
->latest()
->first();

expect($activity)->not->toBeNull();
})->only();
If I click to the PHPStorm play button on my test file : test fail (first screen). From terminal : it passes. I don't get it ๐Ÿคฃ
No description
No description
Dennis Koch
Dennis Kochโ€ข2mo ago
Can you use the exact same command? Is it the --configuration?
Alexandre
AlexandreOPโ€ข2mo ago
Ok if I copy/past the command from PHPStorm to my terminal and I also have a passed test ๐Ÿ™‚ So... Maybe something with the PHPUnit ? I've the same problem on GitHub with my action.
<?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>
<?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>
No description
Alexandre
AlexandreOPโ€ข2mo ago
Here my action on Github :
name: Tests
on:
push:
branches:
- main
- develop
pull_request:
branches:
- main
jobs:
tests:
name: ๐Ÿงช Run tests
runs-on: ubuntu-latest
steps:
- name: ๐Ÿ”Ž Checkout code
uses: actions/checkout@v4
- name: ๐Ÿ˜ Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
tools: composer:v2
coverage: none
- name: ๐Ÿ—๏ธ Set up Node
uses: actions/setup-node@v4
with:
node-version: 18
- name: ๐Ÿ’พ Cache Composer dependencies
uses: actions/cache@v4
with:
path: ~/.composer/cache
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: ๐Ÿ’พ Cache npm dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- name: ๐Ÿ—ƒ๏ธ Install dependencies
run: |
composer install --no-interaction
npm ci
- name: ๐Ÿ“‚ Set up SQLite Database
run: |
cp .env.testing.example .env
touch database/database.sqlite
php artisan key:generate
php artisan migrate --force
php artisan db:seed --class="TestingSeeder" --force
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
APP_ENV: testing
CACHE_DRIVER: array
QUEUE_CONNECTION: sync
SESSION_DRIVER: array
- name: ๐Ÿ”ฌ Run tests
run: composer test //launch pest --parallel
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
APP_ENV: testing
name: Tests
on:
push:
branches:
- main
- develop
pull_request:
branches:
- main
jobs:
tests:
name: ๐Ÿงช Run tests
runs-on: ubuntu-latest
steps:
- name: ๐Ÿ”Ž Checkout code
uses: actions/checkout@v4
- name: ๐Ÿ˜ Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
tools: composer:v2
coverage: none
- name: ๐Ÿ—๏ธ Set up Node
uses: actions/setup-node@v4
with:
node-version: 18
- name: ๐Ÿ’พ Cache Composer dependencies
uses: actions/cache@v4
with:
path: ~/.composer/cache
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: ๐Ÿ’พ Cache npm dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- name: ๐Ÿ—ƒ๏ธ Install dependencies
run: |
composer install --no-interaction
npm ci
- name: ๐Ÿ“‚ Set up SQLite Database
run: |
cp .env.testing.example .env
touch database/database.sqlite
php artisan key:generate
php artisan migrate --force
php artisan db:seed --class="TestingSeeder" --force
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
APP_ENV: testing
CACHE_DRIVER: array
QUEUE_CONNECTION: sync
SESSION_DRIVER: array
- name: ๐Ÿ”ฌ Run tests
run: composer test //launch pest --parallel
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
APP_ENV: testing
Dennis Koch
Dennis Kochโ€ข2mo ago
Ok if I copy/past the command from PHPStorm to my terminal and I also have a passed test ๐Ÿ™‚
Hm, this is weird then.
Alexandre
AlexandreOPโ€ข2mo ago
Yeah... I don't understand why. All the other tests on my resources are working... The retrieval of orders is problematic, but I cannot quite grasp why... The error on GitHub is also the same. Is there a specific configuration required for the database?
No description

Did you find this page helpful?