Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rhand's avatar
Level 6

Add Laravel to existing Laravel Package

We have a Let's Encrypt SSL Generator package we are working on.

Issue is that this package does not have Laravel installed so php artisan make:test SslServiceTest failed. I did install Laravel Installer globally with composer but that does not seem to help. So was considering adding Laravel to the package. However, how to go about this. I want to run command from the root and not inside a package inside a package. How should I do this?

package url is https://github.com/smart48/le-ssl-laravel-package

0 likes
5 replies
LaryAI's avatar
Level 58

To add tests to your Laravel package, you don't need to install the entire Laravel framework within your package. Instead, you should use the orchestra/testbench package, which provides a lightweight testing environment for Laravel packages.

Here's how you can set up your package for testing with orchestra/testbench:

  1. Require orchestra/testbench in your package's composer.json file under the require-dev section:
"require-dev": {
    "orchestra/testbench": "^6.0" // Use the appropriate version for your Laravel version
}
  1. Run composer update to install the package.

  2. Create a base TestCase class inside your package's tests directory. This class will extend Orchestra\Testbench\TestCase and will be the base class for all your package's test cases.

// tests/TestCase.php

namespace YourPackageNamespace\Tests;

use Orchestra\Testbench\TestCase as OrchestraTestCase;

abstract class TestCase extends OrchestraTestCase
{
    protected function getPackageProviders($app)
    {
        // Register your package service providers here
        return [
            \YourPackageNamespace\YourServiceProvider::class,
        ];
    }

    protected function getEnvironmentSetUp($app)
    {
        // Perform environment setup
    }
}
  1. Now, you can create your test class by extending the base TestCase you just created.
// tests/Feature/SslServiceTest.php

namespace YourPackageNamespace\Tests\Feature;

use YourPackageNamespace\Tests\TestCase;

class SslServiceTest extends TestCase
{
    /** @test */
    public function it_tests_something()
    {
        // Your test code here
    }
}
  1. To run your tests, you can use PHPUnit directly from the root of your package:
./vendor/bin/phpunit

By following these steps, you can test your Laravel package without having to install Laravel itself within the package. This approach keeps your package lightweight and focused on its functionality while still allowing you to write and run tests as if it were part of a Laravel application.

martinbean's avatar
Level 80

Issue is that this package does not have Laravel installed so php artisan make:test SslServiceTest failed

@rhand Well no, because if it’s a package, then you’re supposed to install it into a Laravel application, from where you’d then run the command.

1 like
rhand's avatar
Level 6

@martinbean I see. That makes sense in ways. Still it would be nice to have the commands available so I can easily add tests to a package without adding it to a Laravel application. Test orchestra can be used I saw. Also saw I can add PHP unit test of course as well as missing Http package from Illuminate. But yes, still cannot then run Artisan commands

martinbean's avatar

@rhand The commands are available. In applications where you install your package.

1 like
puklipo's avatar

Create a test file manually.

The default artisan command cannot be used when developing a package.

There is no better way than using orchestra/testbench.

Run package command manually via testbench

./vendor/bin/testbench package:command

Run package command in tests. $this->artisan() can be used without an artisan file.

class CommandTest extends TestCase
{
    public function test_command()
    {
        $this->artisan('package:command')
             ->assertSuccessful();
    }
}
1 like

Please or to participate in this conversation.