New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

web-chefs/laravel-app-spawn

Laravel custom application instance bootstrap creator. Used predominantly for doing Laravel specific testing.
5,913
Install
composer require web-chefs/laravel-app-spawn
Latest Version:1.6
PHP:^7.0|^8.0
Laravel:5 – 8
License:MIT
Last Updated:Sep 20, 2021
Links: GitHub  ·  Packagist
Maintainer: web-chefs

LaraAppSpawn

Latest Version on Packagist Software License Total Downloads

Laravel Custom Application Spawner is primarily used for creating a Application instance in a unit testing environment, allowing you to interact with Laravel in your tests.

By default it will use a SQLite in memory database, allowing you to run migrations and use a fully functional database during your tests.

It is up to you to migration and seed this test database.

Install

Via Composer

$ composer require web-chefs/laravel-app-spawn --dev

Basic usage example


use Illuminate\Foundation\Testing\TestCase;
use WebChefs\LaraAppSpawn\ApplicationResolver;

class MyTest extends TestCase
{
    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        // Root of my app, used as a fallback for location of /database when
        // database.path in config is null.
        $appRoutePath = __DIR__;

        // Resolve Application
        $resolver  = ApplicationResolver::makeApp($appRoutePath);
        $this->app = $resolver->app();

        // Run our database migrations if required
        $this->artisan('migrate:refresh', [ '--force' => 1 ]);

        return $this->app;
    }

}

Example with custom configs

use Illuminate\Support\Arr;
use Illuminate\Foundation\Testing\TestCase;
use WebChefs\LaraAppSpawn\ApplicationResolver;

class MyTest extends TestCase
{
    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        // Root of my app, used as a fallback for location of /database when
        // database.path in config is null.
        $appRoutePath = __DIR__;

        // Build Resolver config
        $config = ApplicationResolver::defaultConfig();
        Arr::set($config, 'database.connection', $this->connectionName);
        Arr::set($config, 'queue.connection', $this->connectionName);

        // Resolve Application
        $resolver  = ApplicationResolver::makeApp($appRoutePath, $config);
        $this->app = $resolver->app();

        // Run our database migrations if required
        $this->artisan('migrate:refresh', [ '--force' => 1 ]);

        return $this->app;
    }

}

TravisCI

This was originally developed for WebChefs\QueueButler and for testing multiple version of Laravel using the same tests.

To see how that is possible see WebChefs\QueueButler .travis.yml.

Contributing

All code submissions will only be evaluated and accepted as pull-requests. If you have any questions or find any bugs please feel free to open an issue.

Credits

License

The MIT License (MIT). Please see License File for more information.

Related Packages

web-chefs/db-lojack

Laravel Database Query Logger and debug helper.

4,585 3
anthonyedmonds/laravel-database-log

Store your Laravel logs in the database

6,066 1
lahaxearnaud/clockwork

Server-side component of Clockwork, a Chrome extension for PHP development

373 1
windwardrail/fast-migrations

Make tests run faster by using fresh copies of pre-migrated databases.

7 0
sofa/laravel-db-queries-alert

Insanely simple check against DB querying abuse in Laravel app AKA Check Your Qu...

2,537 7

Version History

Version Released PHP Laravel License
1.6 ^7.0|^8.0 >=5.3 MIT
1.5 ~7.0 >=5.3 MIT
V1.4 ~5.6|~7.0 5.* MIT
1.3 ~5.6|~7.0 5.* MIT
1.2 ~5.6|~7.0 5.* MIT