stomaskov's avatar

Proper way to write integration test for Laravel package

So, I need to test Laravel package API endpoints. I'm using Testbench. Sounded simple enough. Run migrations, seed and make a request.

BUT, this package depends on few other packages which all have migrations, not to mention a User model that comes with Laravel. They are all part of a larger application that acts as a dashboard.

First problem is running migrations since the packages are using loadMigrationsFrom() method and I guess their ServiceProviders are not read or executed when testing and Testbench is working differently from Laravel migration loader.

Manually calling artisan migrate before or after "main" migration doesn't work because they intertwine by timestamp.

My idea: Only run unit tests in isolation. Write integration test to be run as part of the "main app" installation when all models and migrations are present.

Does this make sense? Is there a better way to write and run these tests?

0 likes
3 replies
bobbybouwmann's avatar

So you have two options here. Like you said yourself writing isolated unit tests would probably test enough already for the package. The one that integrates with your package can then decide if they want to write any tests against that.

The other option is stripping your package more to a smaller versions where it doesn't depend on all these things. Maybe your package should be split into multiple smaller package so they are testable and so on.

Looking in the testbench documentation you can also perform the migrations you want to perform: https://github.com/orchestral/testbench#using-migrations

stomaskov's avatar

@BOBBYBOUWMANN - I'm the one actually using the package in a larger, administrator interface app. Stripping the package is not really an option here but creating migrations just for the test might work.

Thanks for the response and the idea.

bobbybouwmann's avatar

Well maybe a package is not the best approach. Maybe a domain layer for this specific part is a better solution. This way it's still isolated from the rest and testable within the application ;)

Please or to participate in this conversation.