Čamo wrote a reply+100 XP
3mos ago
Čamo wrote a reply+100 XP
4mos ago
Čamo wrote a reply+100 XP
4mos ago
As I found out the CommandFinished is not allowed by default in Laravel 12 in tests https://laravel.com/docs/12.x/console-tests#console-events It has to be allowed by adding trait use WithConsoleEvents. But it does not work although I add the trait to the script.
Čamo wrote a reply+100 XP
4mos ago
Čamo wrote a reply+100 XP
4mos ago
Čamo wrote a reply+100 XP
4mos ago
Čamo wrote a reply+100 XP
4mos ago
Čamo started a new conversation+100 XP
4mos ago
Čamo started a new conversation+100 XP
4mos ago
I would like to implement some logic after all tests in Laravel 12 application finished. I found this blog from 2024 https://laravel.com/docs/12.x/events#main-content which is based on CommandFinished native event. So I made a CommandFinishedListener. If I run php artisan event:list I see the lister in the list. So it is obviously registered. If I run any command it works BUT as I run php artisan test the listener does not run. All tests pass but the event does not trigger. Thanks for any help.
<?php
namespace App\Listeners;
use App\Services\BaseService;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Support\Facades\Log;
class CommandFinishedListener
{
/**
* Create the event listener.
*/
public function __construct()
{
//
}
/**
* Handle the event.
*/
public function handle(CommandFinished $event)
{
Log::info('CommandFinishedListener: ' . $event->command); // Any other command log this line for example php artisan event:list
if ($event->command === 'test') {
// Do something...
}
}
}
Čamo liked a comment+100 XP
5mos ago
Čamo wrote a reply+100 XP
5mos ago
I am facing the same problem right now. It seems there is still no solution on framework level. What I found it this blog. did not test it yet but hope it will help https://sarahjting.com/blog/laravel-paratest-multiple-db-connections
Čamo wrote a reply+100 XP
5mos ago
Čamo liked a comment+100 XP
5mos ago
I've had the same issue in the past, check out the Parallel Testing Hooks---specifically setUpTestDatabase().
Note that the hooks will be called for every process, so if you only want to run something once then you'll need to do something like:
// Executed when a test database is created...
ParallelTesting::setUpTestDatabase(function ($database, $token) {
if ($token === 1) {
// create databases
}
});
Čamo liked a comment+100 XP
5mos ago
Hi !
I'm currently working on an API that needs to have multiple database connections. At the moment it uses : 1 MySQL + 1 PostgreSQL + 1 MongoDB. MySQL and PostgreSQL may be merged in the future, but it will at least keep a MongoDB in parallel of the PostgreSQL. Connection to MongoDB is handled by the jenssegers/mongodb package
This API is tested with Pest, but the current ~90 tests take around 2 minutes to run, and this is about 5% of the total features that will be added. So I wanted to setup parallel testing, but only the MySQL (main database connection) has the testing_X databases created.
Does anyone know what I can override to handle the other connections so that it also creates the testing_X databases on PostgreSQL and MongoDB ?
I've already altered the default TestCase to call a custom migrate:fresh when using the DatabaseMigrations trait, so that it can reset the databases for all three connections (which I know is the main performance issue at the moment in my tests), but I have not been able to find how to intercept the parallel testing setup procedure.