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

scibuff's avatar

Test error (only when running suite) - Target class [config] does not exist.

I have a simple test class, with two tests

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class MyBasicTest extends TestCase {

    /** @test */
    public function test1(){
        dump( config('app.name') );
        dump( config('app.env') );
        $this->assertTrue(true);
    }

    /** @test */
    public function test2(){
        dump( config('app.name') );
        dump( config('app.env') );
        $this->assertTrue(true);
    }
}

when I run each test separately everything works fine, but when I test the entire class, I get Target class [config] does not exist.

 Tests\Unit\MyBasicTest::test2
Illuminate\Contracts\Container\BindingResolutionException: Target class [config] does not exist.

/home/vagrant/code/apps/my-app/vendor/laravel/framework/src/Illuminate/Container/Container.php:807
/home/vagrant/code/apps/my-app/vendor/laravel/framework/src/Illuminate/Container/Container.php:687
/home/vagrant/code/apps/my-app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:796
/home/vagrant/code/apps/my-app/vendor/laravel/framework/src/Illuminate/Container/Container.php:633
/home/vagrant/code/apps/my-app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:781
/home/vagrant/code/apps/my-app/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:119
/home/vagrant/code/apps/my-app/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:275
/home/vagrant/code/apps/my-app/tests/Unit/MyBasicTest.php:30

Caused by
ReflectionException: Class config does not exist

/home/vagrant/code/apps/my-app/vendor/laravel/framework/src/Illuminate/Container/Container.php:805
/home/vagrant/code/apps/my-app/vendor/laravel/framework/src/Illuminate/Container/Container.php:687
/home/vagrant/code/apps/my-app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:796
/home/vagrant/code/apps/my-app/vendor/laravel/framework/src/Illuminate/Container/Container.php:633
/home/vagrant/code/apps/my-app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:781
/home/vagrant/code/apps/my-app/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:119
/home/vagrant/code/apps/my-app/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:275
/home/vagrant/code/apps/my-app/tests/Unit/MyBasicTest.php:30

Basically, it seems that if I call config more then once in the second test during the run, it throws the BindingResolutionException

p.s. when I do phpunit --filter test2 everything works just fine; the error happens only if I run phpunit --filter MyBasicTest

Any ideas?

p.s.2. of course, my real working app test which throws these errors is much more complex, but I've narrowed it down to this simple case (calling config twice from two different tests)

vagrant@homestead: $ php artisan --version
Laravel Framework 7.10.3
0 likes
21 replies
bobbybouwmann's avatar

This normally happens when the framework hasn't been booted. In most cases, you're extending the wrong TestCase class. If you extend the PHPUnit base TestCase class the framework isn't booted.

It seems that you extend the correct class Tests\TestCase. Did you change anything in this basic class? Most important this is that you always call setUp and the parent method

abstract class TestCase extends BaseTestCase
{
    public function setUp(): void
    {
        parent::setUp();

        // Do your extra thing here
    }
}
17 likes
Belal.Mazlom's avatar

I was using TestCase from PHPUnit by mistake Thank you that helped me to notice the issue

scibuff's avatar

Thank you but that's not it (I was aware of the boot problems). My TestCase is empty

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
}

Just to make it clearer ... the issue is not running the test2 method, it is calling the dump(config()) second time in it. If there is only a single call to dump(config()) the tests pass!

It is almost as if when the first test1 is done, it unbinds the app('config') and then second test2 doesn't manage to finish on time ..

3 likes
JohnnyBigodes's avatar

Do you have a solution for this?

It seems that I am having almost the same problem, but in my case it is Redis.

I am checking an api route and the test passes in the console but not on my IDE (PHPStorm).

thiagolcks's avatar

Hi!

I'm having the same issue. If I call dump(route('any')) twice in the same test I get the error: Target class [config] does not exist.

dump(route('route-a'));
dump(route('route-b'));

BUT, if I put the value in a variable, it works:

$a = route('route-a');
$b = route('route-b');
dump($a);
dump($b);

Any idea?

1 like
yao1997's avatar

For my case this issue happens when a testsuite is finished and the second testsuite starts running.

Jmac's avatar

Did you ever find a solution for this?

michaelhume's avatar

I'm curious as well - Having a similar issue that just appeared in an old test suite.

michaelhume's avatar

Just as an update - it turned out to be an issue related to a package upgrade.

alexmanase's avatar

Hi! Can you be a little bit more specific. I have the same error and I can't find the issue.

Thanks!

3 likes
jaumebalust's avatar

In my case I was calling ´parent::tearDown()´ at the begining of my tearDown() function, which made the code after it invalid . The solution was simply bring ´parent::tearDown()´ at the end of it .

10 likes
Second2None's avatar

This fixed it for me as well. Finally. lol thank you!

1 like
eagle1's avatar

Yes, this fixed it for me as well. Good catch!

darkestmon's avatar

In my case... implementing tearDown() and not calling parent::tearDown() removed the errors. Am now bracing myself for whatever consequences this may cause.

public function tearDown(): void
{
    // parent::tearDown();
}
2 likes
emiliosh's avatar

Thx, it worked for me too, nothing else worked. I'm on Laravel 6.

abdulkhan's avatar

Failing code due to the wrong import by ID and I missed it

namespace Tests\Unit;

use App\Services\ThreePLC\ThreePLC;
use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{

    /**
     * A basic test example.
     *
     * @return void
     */
    public function test_that_true_is_true()
    {
        $config = config('threeplc');
        dd($config);
        $client = app(ThreePLC::class);
        $auth = $client->getToken();
        dd($auth);
    }
}

Worked

namespace Tests\Unit;

use App\Services\ThreePLC\ThreePLC;
use Tests\TestCase;


class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function test_that_true_is_true()
    {
        $config = config('threeplc');
        dd($config);
        $client = app(ThreePLC::class);
        $auth = $client->getToken();
        dd($auth);
    }
}
2 likes
lordisp's avatar

@abdulkhan this is not entirely correct. Because by changing the namespace from the PHPUnit TestClass to Laravel's TestClass, you boot the Laravel application for the tests, which change the whole purpose of UnitTesting.

UnitTest means, you test just a single portion of the application but not the application itself. In other words you want to the a single class.

For a Feature Test, you need to boot the application, which is actually the effect of your change.

Long story short: Your change changes the behaviour from UnitTest to a FeatureTest

2 likes
SiZE's avatar

Have the same issue. My app output some content to buffer. To fix error with contents already sent, I've added a phpDoc notation @runInSeparateProcess. But then get new error with Sanctum and config, as described above.

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

         \Laravel\Sanctum\Sanctum::actingAs(
           \App\Models\Customer::query()->find(1)
        );

        // @todo Заполнение БД
        \Illuminate\Support\Facades\Config::set('test.customer.id', 4119);
    }

    /**
     * @runInSeparateProcess
     */
    public function testSomeTest() {}
Fatal error: Uncaught ReflectionException: Class config does not exist in /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 879

Illuminate\Contracts\Container\BindingResolutionException: Target class [config] does not exist. in /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 879

Call Stack:
    0.0012     789312   1. {main}() Standard input code:0
    1.8366    8493664   2. require_once('/var/www/config/sanctum.php') Standard input code:678
    1.8429    8505720   3. Laravel\Sanctum\Sanctum::currentApplicationUrlWithPort() /var/www/config/sanctum.php:21
    1.8429    8505720   4. config($key = 'app.url', $default = ???) /var/www/vendor/laravel/sanctum/src/Sanctum.php:37
    1.8429    8505720   5. app($abstract = 'config', $parameters = ???) /var/www/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:275
    1.8429    8505720   6. Illuminate\Foundation\Application->make($abstract = 'config', $parameters = []) /var/www/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:119
    1.8429    8505720   7. Illuminate\Container\Container->make($abstract = 'config', $parameters = []) /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:836
    1.8429    8505720   8. Illuminate\Foundation\Application->resolve($abstract = 'config', $parameters = [], $raiseEvents = ???) /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php:694
    1.8429    8505720   9. Illuminate\Container\Container->resolve($abstract = 'config', $parameters = [], $raiseEvents = TRUE) /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:851
    1.8429    8506096  10. Illuminate\Container\Container->build($concrete = 'config') /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php:758
da_Mask's avatar

For anyone coming from Google, you can get this same error when using Pest, when using the skip() method or similar, if you are trying to evaluate a condition from somewhere in your app. If you run into this, you need to add a callable to access the underlying TestCase, so that

test(
)->skip( ! Features::hasApiFeatures() ,'API support is not enabled.');

becomes

test(
)->skip( fn() =>  ! Features::hasApiFeatures() ,'API support is not enabled.');

I hope this helps someone.

2 likes
S-taro's avatar

In my case, the problem was that the code in the destructor was not executing. I wrote $this->createApplication(); at the end of the test so that Laravel is running at the time the destructor is called.

Please or to participate in this conversation.