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:
- Require
orchestra/testbenchin your package'scomposer.jsonfile under therequire-devsection:
"require-dev": {
"orchestra/testbench": "^6.0" // Use the appropriate version for your Laravel version
}
-
Run
composer updateto install the package. -
Create a base TestCase class inside your package's
testsdirectory. This class will extendOrchestra\Testbench\TestCaseand 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
}
}
- 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
}
}
- 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.