bfinlay / laravel-excel-seeder

Seed your database with Laravel using Excel and CSV files
Other
38 stars 8 forks source link

Using a SpreadsheetSeeder in a Feature Test triggers an exception #19

Closed raarevalo96 closed 1 year ago

raarevalo96 commented 1 year ago

What is happening?

When a test using the Illuminate\Foundation\Testing\RefreshDatabase trait is being seeded, DatabaseSeeder calls a Seeder that extends SpreadsheetSeeder, throwing the following exception:

Illuminate\Database\Connection::setEventDispatcher(): Argument #1 ($events) must be of type Illuminate\Contracts\Events\Dispatcher, null given, called in /var/www/***/vendor/laravel/framework/src/Illuminate/Foundation/Testing/RefreshDatabase.php on line 98

This contains the following code:

$connection = $database->connection($name);
$dispatcher = $connection->getEventDispatcher();
[...]
$connection->setEventDispatcher($dispatcher); // The exception is thrown here

Reading through this library, the file src/Writers/Database/DatabaseWriter.php contains the following line:

DB::connection()->unsetEventDispatcher();

Which directly references the exception message.

The test is being run by the php artisan test command. Removing the seeder that extends SpreadsheetSeeder fixes the error.

Environment

bfinlay commented 1 year ago

I will put together a test case for this and look into the issue.

raarevalo96 commented 1 year ago

If more information is needed I'll be happy to help!

bfinlay commented 1 year ago

There is a partial fix checked in.

It is a complete fix for Laravel 9.x and 10.x. It works for Laravel 5.x-8.x on PHP 7.x

It fails for Laravel 6.x-8.x on PHP 8.0 It fails for Laravel 8.x on PHP 8.1 and PHP 8.2

I want to fix the failing versions before doing a release.

In the meanwhile, could you update your composer.json to use "dev-master" as the version and try this out? Let me know if it works for you.

raarevalo96 commented 1 year ago

Just updated to "dev-master", I can confirm the fix is working, tests are now working as expected. Thank you for your help and for making this package. I'll be waiting for the release so you can close this issue.

bfinlay commented 1 year ago

This issue is fixed in release v3.3.3.