Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReflectionException: Class config does not exist #99

Closed
OwenMelbz opened this issue Feb 3, 2017 · 34 comments
Closed

ReflectionException: Class config does not exist #99

OwenMelbz opened this issue Feb 3, 2017 · 34 comments

Comments

@OwenMelbz
Copy link

OwenMelbz commented Feb 3, 2017

Hi guys, recently upgraded a 5.3 project to 5.4 and all seemed good.

Today I started to implement Dusk however had hit an issue when running the example test

☁  footy-finance [5.4] ⚡ php artisan dusk
PHPUnit 6.0.0 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 162 ms, Memory: 6.00MB

There was 1 error:

1) Tests\Browser\ExampleTest::testBasicExample
ReflectionException: Class config does not exist

/Users/owen/Sites/footy-finance/vendor/laravel/framework/src/Illuminate/Container/Container.php:681
/Users/owen/Sites/footy-finance/vendor/laravel/framework/src/Illuminate/Container/Container.php:565
/Users/owen/Sites/footy-finance/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:105
/Users/owen/Sites/footy-finance/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:263
/Users/owen/Sites/footy-finance/vendor/laravel/dusk/src/TestCase.php:203
/Users/owen/Sites/footy-finance/vendor/laravel/dusk/src/TestCase.php:40

I've had a look at line 40 of TestCase.php and its

public function baseUrl()
{
    return config('app.url');
}

So it does look like something to do with the global config helper anybody have any ideas?

I'm running

  • PHP 7.0.14
  • Laravel/Framework 5.4.8
  • Laravel/Dusk 1.0.5
  • PHPUnit 6.0

The full composer.lock can be seen https://gist.github.com/OwenMelbz/c05172b33f6eb4483e37a56469b53722

Fingers crossed you guys have some ideas!

Cheers :)

@thecrypticace
Copy link

I've noticed this too. Currently seeing if I can track down what change caused this. I thought it was the @before annotation on the method that calls ->baseUrl() but that has been like that for 2 months according to GitHub so I'm not sure what it is.

@thecrypticace
Copy link

PHPUnit 6.0 is the problem here. I upgraded to it too and methods annotated with @before get called before setUp.

cc @taylorotwell afaict, it seems like the solution here would be to do the same thing you did with the testing traits and call them explicitly during setUp. Thoughts? I'll open a PR if that is the route you would like to go.

@jaesung2061
Copy link
Contributor

@thecrypticace PR would be awesome :)

@reganjohnson
Copy link

Subbed: Having the same issues with PHPUnit 6.0.

@thecrypticace
Copy link

If I have time this evening I'll investigate what it takes to make Dusk compatible with both. There'll definitely be challenges because PHPUnit 6 was a rather big BC break.

@jaesung2061
Copy link
Contributor

@thecrypticace any luck?

@GertjanRoke
Copy link

Hello guys, can you give us an update on the progress till so far?

@reganjohnson
Copy link

Any update on this bug? Still affecting latest versions.

reganjohnson referenced this issue Mar 10, 2017
@vkulov
Copy link

vkulov commented Mar 23, 2017

Any workaround for this? Other than downgrading phpunit to 5.0

@tomfordweb
Copy link

Following for a fix for phpunit 6.0

@deleugpn
Copy link
Contributor

Thanks @tomfordweb for bumping this.

@PortionLK
Copy link

PortionLK commented Aug 7, 2017

I got this same issue, but not related to phpunit.
The issue was I added values containing spaces to my .env file.
If you have values contains spaces in your .env file, surround them with quotes and then it'll be fixed.

if you run php artisan clear-compiled while having this issue, it will tell the solution ;)

@moxx
Copy link
Contributor

moxx commented Sep 21, 2017

same here!

MAIL_NAME=company - service
#fix is
MAIL_NAME="company - service"

Spaces in .env, without quotes are not valide. But this error msg is confusing.

@SeinopSys
Copy link

I just ran into this as well, adding quotes fixed it. The error message really isn't helpful.

@khanhtran3005
Copy link

@moxx it worked for me.
Thanks!!

@spmsupun
Copy link

spmsupun commented Mar 26, 2018

Had same problem and i've deleted bootstrap/cache/config.php and ran php artisan clear-compiled , now it's working fine

@crodas
Copy link

crodas commented Apr 4, 2018

Do you have a setUp method? If so, be sure to call parent::setup() as the first statement in that function.

@pablorsk
Copy link

pablorsk commented Apr 9, 2018

I was have the same problem. Solution:

Check your TestCase.php, content:

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
}

Off course, you need this file on the same folder CreatesApplication.php.

@sohaieb
Copy link

sohaieb commented Aug 19, 2018

Hope that this can help you too

barryvdh/laravel-dompdf#192 (comment)

@EaseOnConsole
Copy link

go to the .env file. Make sure that the title contains no space or special character

@zouxiaozhu
Copy link

Do you have a setUp method? If so, be sure to call parent::setup() as the first statement in that function.

i got this problem, now reslove by add code parent::setUp()

@rahulniraula-zz
Copy link

saved my day after adding parent::setUp()

@jrbecart
Copy link

jrbecart commented Sep 3, 2019

Sometime you will get this error if your test function run in a separate process.

/**
* @runInSeparateProcess
*/
public function testMyTest()
{
  // Your test
}

@rijalmohamad
Copy link

Had same problem and i've deleted bootstrap/cache/config.php and ran php artisan clear-compiled , now it's working fine

sangat bagus sekali

@Casmo
Copy link

Casmo commented Nov 19, 2019

Removing (any) dd() or dump() lines in the application fixed the issue for me.

@julian-sawicki-xxtract
Copy link

julian-sawicki-xxtract commented Dec 11, 2019

Do you have a setUp method? If so, be sure to call parent::setup() as the first statement in that function.

I just want to point out how that looks like in a source file.
Below you can see a situation which causes the ReflectionException.

class MyFirstTest extends TestCase
{
    
    public function setUp() {
        $resourceObject = new ResourceObjectMock;
    }
    
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testResource()
    {
        $this->assertTrue(true);
    }
} 

Below parent::setup(); is added to the setup method in the Test class.
This solution fixed the ReflectionException problem.

class MyFirstTest extends TestCase
{
    
    public function setUp() {
        parent::setup();
        $resourceObject = new ResourceObjectMock;
    }
    
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testResource()
    {
        $this->assertTrue(true);
    }
} 

@jv2222
Copy link

jv2222 commented Dec 15, 2019

Just want to note that in Laravel 5.8.35 with PHPUnit 7.5.17 this works...

protected function setUp(): void
{
    parent::setUp();
    // Do something
}

protected function tearDown(): void
{
    // Do something
    parent::tearDown();
}

Note: it's critical to to run you code after parent::setUp(); and before parent::tearDown(); this was the only way I could get it to work.

@Mult1Hunter
Copy link

Also check if you accidentally extended wrong TestCase class with your test class.

@joelmellon
Copy link

Also check if you accidentally extended wrong TestCase class with your test class.

Laravel's Unit/ExampleTest.php extends PHPUnit's TestCase class which doesn't bootstrap the Laravel app obviously, so if you're expecting app functionality like config() you need to extend Tests\TestCase like Feature/ExampleTest.php does.

@mafudelaptu
Copy link

mafudelaptu commented Apr 21, 2020

Also check if you accidentally extended wrong TestCase class with your test class.

Laravel's Unit/ExampleTest.php extends PHPUnit's TestCase class which doesn't bootstrap the Laravel app obviously, so if you're expecting app functionality like config() you need to extend Tests\TestCase like Feature/ExampleTest.php does.

Thanks!

@khanimranm
Copy link

@Casmo Thank You. Removing dump( ) and dd( ) in different files related to the test has helped me to resolve the problem.

@illusive-ch
Copy link

@Casmo Thank You. Removing dump( ) and dd( ) in different files related to the test has helped me to resolve the problem.

this fixed it for me

@crowdy
Copy link

crowdy commented May 3, 2021

I've experienced the same thing. my fix is

//use PHPUnit\Framework\TestCase; // this alse causes ReflectionException: Class config does not exist
use Tests\TestCase; // this is the right one

@sizeg
Copy link

sizeg commented Jun 24, 2022

Sometime you will get this error if your test function run in a separate process.

Same for me

public function setUp()
{
    parent::setUp();

    \Laravel\Sanctum\Sanctum::actingAs($this->id);
}

public function testOne() //This test passed
{
    $this->assertTrue(true);
}

/**
 * @runInSeparateProcess
 */
public function testTwo() //This test failed
{
    $this->assertTrue(true);
}

The testTwo passed only if:

  1. Run just it directly
  2. The only test in class
  3. Each test in class specified as @runInSeparateProcess
Error stack trace
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     788920   1. {main}() Standard input code:0
    1.8993    8516808   2. require_once('/var/www/config/sanctum.php') Standard input code:677
    1.9060    8528864   3. Laravel\Sanctum\Sanctum::currentApplicationUrlWithPort() /var/www/config/sanctum.php:21
    1.9060    8528864   4. config($key = 'app.url', $default = ???) /var/www/vendor/laravel/sanctum/src/Sanctum.php:37
    1.9060    8528864   5. app($abstract = 'config', $parameters = ???) /var/www/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:275
    1.9060    8528864   6. Illuminate\Foundation\Application->make($abstract = 'config', $parameters = []) /var/www/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:119
    1.9060    8528864   7. Illuminate\Container\Container->make($abstract = 'config', $parameters = []) /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:836
    1.9060    8528864   8. Illuminate\Foundation\Application->resolve($abstract = 'config', $parameters = [], $raiseEvents = ???) /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php:694
    1.9060    8528864   9. Illuminate\Container\Container->resolve($abstract = 'config', $parameters = [], $raiseEvents = TRUE) /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:851
    1.9060    8529240  10. Illuminate\Container\Container->build($concrete = 'config') /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php:758

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests