2018-05-08 #laravel

Creating a Laravel Package For Auth Tests

In this tutorial we're going to create a new Laravel package for the Auth tests we wrote in a previous tutorial.

Auth Tests

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.

How To Create Your Own Laravel Package

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 php artisan.

Looking at the Laravel documentation we can move files from the package to the project by using a number of different methods.

  • publishes
  • mergeConfigFrom
  • loadRoutesFrom
  • loadMigrationsFrom
  • loadTranslationsFrom
  • loadViewsFrom

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.


namespace Dappa\AuthTests;

use Illuminate\Support\ServiceProvider;

 * Auth test service provider
class AuthTestsServiceProvider extends ServiceProvider
     * Bootstrap application services
    public function boot()
            __DIR__ . '/Stubs/tests/Feature/Auth/ForgotPasswordTest.php' => base_path('tests/Feature/Auth/ForgotPasswordTest.php'),
            __DIR__ . '/Stubs/tests/Feature/Auth/LoginTest.php' => base_path('tests/Feature/Auth/LoginTest.php'),
            __DIR__ . '/Stubs/tests/Feature/Auth/RegisterTest.php' => base_path('tests/Feature/Auth/RegisterTest.php'),
            __DIR__ . '/Stubs/tests/Feature/Auth/ResetPasswordTest.php' => base_path('tests/Feature/Auth/ResetPasswordTest.php'),

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 config/app.php file.

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 tests folder.

Auth Tests Repository


Reliable uptime monitoring and instant alerts for any website downtime.

Get 50% off forever using the code 50OFFUP.