Skip to content

Instantly share code, notes, and snippets.

@mikakane
Last active December 2, 2021 14:58
Show Gist options
  • Save mikakane/dad182ad9f2039a7328a9ec32a171c74 to your computer and use it in GitHub Desktop.
Save mikakane/dad182ad9f2039a7328a9ec32a171c74 to your computer and use it in GitHub Desktop.
how to test laravel package inside package

Laravel Package

Create Package

create package directory

$ mkdir modules
$ mkdir modules/some-module
$ cd modules/some-module

create src dir, and README.md

$ mkdir src
$ touch README.md

put composer.json

{
    "name": "hoge/piyo",
    "authors": [
        ...
    ],
    "autoload": {
        "psr-4": {
            "Hoge\\Piyo\\": "./src"
        }
    },
    "require": {},
    "extra": {
        "laravel": {
            "providers": [
                "\\Hoge\\Piyo\\HogePiyoServiceProvider"
            ]
        }
    }
}

put .gitignore

/vendor
.env
composer.lock

TestSetup PHPUnit

install phpunit and laravel.

$ composer require laravel/laravel phpunit/phpunit fzaninotto/faker --dev

copy essential files from vendor/laravel/laravel

$ cp vendor/laravel/laravel/phpunit.xml ./phpunit.xml
$ cp -R vendor/laravel/laravel/tests ./tests

add autoload-dev, like below.

    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },

after that, you should run composer dump-autoload.

put laravel.php on your root of package.

<?php

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__."/vendor/laravel/laravel/bootstrap")
);

/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
*/

$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

// If you need some ServiceProvider, you can install it here.
//$app->register(...);

// for database
$app->useDatabasePath(__DIR__."/database");
// for .env
$app->useEnvironmentPath(__DIR__);
/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/

return $app;

Finally edit your tests/CreatesApplication.php

<?php

namespace Tests;

use Illuminate\Foundation\Application;
use Illuminate\Contracts\Console\Kernel;

trait CreatesApplication
{
    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        /** @var Application $app */
        $app = require __DIR__.'/../laravel.php';
        $app->make(Kernel::class)->bootstrap();
        return $app;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment