In this tutorial we're going to create a new Laravel package for the Auth tests we wrote in a previous tutorial.
We want to move this into it's own Laravel package service provider so that we can reuse these tests on any new Laravel project we start.
If you don't know how to create your own Laravel package please read one of my previous articles.
The most important file we need to add to our package is the Service Provider, this will tell Laravel how to use our package. Now we need to think about the best way of importing our tests into our Laravel app.
We can store these tests into a stubs folder and copy them into our project tests folder. There are two ways we can do this, by creating our own Laravel command to move the tests or we can reuse the functionality we get from
Looking at the Laravel documentation we can move files from the package to the project by using a number of different methods.
As you can see there is a
publishes method that will copy the files from the package to the project. But if the files already exist they will not be copied unless you use the
--force flag. This is exactly the functionality we want and we can add this to the service provider by using the following code.
Now we just need to move the test files into their own folder/repo with this service provider and add this service provider to our
When the service provider is registered in your
app.php file you can use the command
php artisan vendor:publish command, this will give you a list of registered service providers. Enter the number for the auth tests service provider and the tests will be moved into your project