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

abkrim's avatar
Level 13

Testing in package laravel.

I like testing my packages.

I always get hit with this topic.

My package works for me, but it is without testing because I feel unable to configure the testing environment with PestPHP.

I can test the app, using my package, but not test "in the package"

TestCase

<?php

namespace Abkrim\ApiSage\Tests;

use Orchestra\Testbench\TestCase as Orchestra;
use Abkrim\ApiSage\ServiceProvider;

class TestCase extends Orchestra
{
    protected function getPackageProviders($app): array
    {
        return [
            ServiceProvider::class,
        ];
    }
}

Pest.php

<?php

uses(
    Tests\TestCase::class
)->in('Unit');

Demotwest.php

<?php

use Tests\TestCase;

test('confirm environment is set to testing', function () {
    expect(config('app.env'))->toBe('testing');
})->uses(TestCase::class);

test('sum', function () {
    $result = 3;
    expect($result)->toBe(3);
});

Last test pass but first.

Illuminate\Contracts\Container\BindingResolutionException: Target class [config] does not exist.
at vendor/laravel/framework/src/Illuminate/Container/Container.php:912
at vendor/laravel/framework/src/Illuminate/Container/Container.php:795
at vendor/laravel/framework/src/Illuminate/Container/Container.php:731
at vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:120
at vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:274
at packages/abkrim/api-sage/tests/Unit/DemoTest.php:6
Caused by
ReflectionException: Class "config" does not exist

at vendor/laravel/framework/src/Illuminate/Container/Container.php:912
at vendor/laravel/framework/src/Illuminate/Container/Container.php:795
at vendor/laravel/framework/src/Illuminate/Container/Container.php:731
at vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:120
at vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:274
at packages/abkrim/api-sage/tests/Unit/DemoTest.php:6
0 likes
1 reply
LaryAI's avatar
Level 58

It seems like the issue you're encountering is related to the Laravel application not being booted properly within your test environment, which is why the config helper is not available.

To resolve this, you should ensure that your base TestCase class is setting up the application correctly. Since you're using Orchestra Testbench, you're on the right track. However, you might need to make sure that you're using the correct namespace for your TestCase class in your Pest tests.

Here's a step-by-step guide to ensure your testing environment is configured correctly:

  1. Verify that your TestCase class is in the correct namespace and that it extends Orchestra\Testbench\TestCase. It looks like you've done this correctly.

  2. In your Pest.php file, make sure you're using the correct namespace for your TestCase. It should match the namespace of your TestCase class. For example, if your TestCase is in the Abkrim\ApiSage\Tests namespace, your Pest.php should look like this:

<?php

uses(Abkrim\ApiSage\Tests\TestCase::class)->in('Unit');
  1. In your test file (e.g., DemoTest.php), you don't need to use the TestCase class again since you've already specified it in Pest.php. You can remove the ->uses(TestCase::class) part from your tests:
<?php

test('confirm environment is set to testing', function () {
    expect(config('app.env'))->toBe('testing');
});

test('sum', function () {
    $result = 3;
    expect($result)->toBe(3);
});
  1. Ensure that your TestCase class is bootstrapping the application correctly. You might want to add a setUp method to your TestCase class to boot the application:
<?php

namespace Abkrim\ApiSage\Tests;

use Orchestra\Testbench\TestCase as Orchestra;
use Abkrim\ApiSage\ServiceProvider;

class TestCase extends Orchestra
{
    protected function getPackageProviders($app): array
    {
        return [
            ServiceProvider::class,
        ];
    }

    protected function setUp(): void
    {
        parent::setUp();

        // Perform environment setup
    }
}
  1. Finally, make sure that your test classes are autoloaded correctly by Composer. You should have an autoload-dev section in your composer.json that looks something like this:
"autoload-dev": {
    "psr-4": {
        "Abkrim\ApiSage\Tests\": "tests/"
    }
}

After making these changes, run composer dump-autoload to regenerate the autoload files.

Now, try running your tests again. If everything is set up correctly, your tests should be able to use the config helper and any other Laravel features you need.

Please or to participate in this conversation.