Pest is a great tool for writing tests – personally I prefer Pest over PHPUnit.
Now that you have developed a Filament plugin or are in the process of developing one, you might want to use Pest for your plugin's tests as well. In this trick I'll show you how to use Pest in your plugin.
First, you need to require Pest and Orchestra Testbench:
composer require pestphp/pest orchestra/testbench --dev -W
Next, create the following file tests/Pest.php
:
<?php use YourUsername\YourPackage\Tests\TestCase; uses(TestCase::class)->in(__DIR__);
Next, update your tests/TestCase.php
to the following:
<?php namespace YourUsername\YourPackage\Tests; use BladeUI\Heroicons\BladeHeroiconsServiceProvider;use BladeUI\Icons\BladeIconsServiceProvider;use Filament\FilamentServiceProvider;use Filament\Forms\FormsServiceProvider;use Filament\Support\SupportServiceProvider;use Livewire\LivewireServiceProvider;use Orchestra\Testbench\TestCase as Orchestra;use YourUsername\YourPackage\YourPackageServiceProvider; class TestCase extends Orchestra{ protected function setUp(): void { parent::setUp(); } protected function getPackageProviders($app): array { return [ YourPackageServiceProvider::class, LivewireServiceProvider::class, FilamentServiceProvider::class, // Only if you are requiring the admin panel. FormsServiceProvider::class, SupportServiceProvider::class, BladeIconsServiceProvider::class, // Only if you are requiring the admin panel. BladeHeroiconsServiceProvider::class, // Only if you are requiring the admin panel. ]; } public function getEnvironmentSetUp($app): void { config()->set('database.default', 'testing'); // (include __DIR__.'/../database/migrations/your_migration_name.php.stub')->up(); }}
Finally, create or update you phpunit.xml
or phpunit.xml.dist
to something like the following:
<?xml version="1.0" encoding="UTF-8"?><phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" beStrictAboutOutputDuringTests="true" verbose="true"> <testsuites> <testsuite name="Package Test Suite"> <directory>tests</directory> </testsuite> </testsuites> <coverage> <include> <directory suffix=".php">./src</directory> </include> </coverage> <php> <env name="APP_KEY" value="SOME_VALID_DEMO_APP_KEY"/> <env name="DB_CONNECTION" value="sqlite"/> <env name="DB_DATABASE" value=":memory:"/> </php></phpunit>
Now you can run your plugin testsuite using vendor/bin/pest
and everything should work! 🎉
No comments yet…