Framework: ReflectionException : Class env does not exist

Created on 26 Oct 2018  路  10Comments  路  Source: laravel/framework

  • Laravel Version: 5.7.10
  • PHP Version: 7.2.10
  • Database Driver & Version: Nothing connected or setup

Description:

When running PHPUnit tests an exception is thrown, ReflectionException : Class env does not exist;
ReflectionException : Class env does not exist
/var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php:779
/var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php:658
/var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php:609
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:733
/var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php:1222
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:487
/var/www/app/Providers/TelescopeServiceProvider.php:24
/var/www/vendor/laravel/framework/src/Illuminate/Support/HigherOrderCollectionProxy.php:60
/var/www/vendor/laravel/framework/src/Illuminate/Support/HigherOrderCollectionProxy.php:60
/var/www/vendor/laravel/framework/src/Illuminate/Support/Collection.php:442
/var/www/vendor/laravel/framework/src/Illuminate/Support/HigherOrderCollectionProxy.php:61
/var/www/vendor/laravel/telescope/src/Telescope.php:204
/var/www/vendor/laravel/telescope/src/Telescope.php:177
/var/www/vendor/laravel/telescope/src/Telescope.php:207
/var/www/vendor/laravel/telescope/src/Telescope.php:231
/var/www/vendor/laravel/telescope/src/Watchers/CacheWatcher.php:63
/var/www/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:360
/var/www/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:209
/var/www/vendor/laravel/framework/src/Illuminate/Cache/Repository.php:486
/var/www/vendor/laravel/framework/src/Illuminate/Cache/Repository.php:92
/var/www/vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php:304
/var/www/vendor/laravel/telescope/src/Watchers/DumpWatcher.php:43
/var/www/vendor/laravel/telescope/src/RegistersWatchers.php:48
/var/www/vendor/laravel/telescope/src/Telescope.php:94
/var/www/vendor/laravel/telescope/src/TelescopeServiceProvider.php:25
/var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29
/var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:87
/var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:31
/var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php:572
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:793
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:776
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:777
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php:17
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:206
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:297
/var/www/tests/CreatesApplication.php:18
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:91
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:68

While the exception mentions Laravel Telescope it isn't relevant. I removed it and it still throws the error for another custom service provider. I did move code out of my boot function in my service provider to the register function but it still throws the error.

Steps To Reproduce:

Run php vendor/phpunit/phpunit/phpunit --configuration phpunit.xml tests

Most helpful comment

https://github.com/laravel/framework/issues/26627#issuecomment-497276990

For someone still having the issue, it can be solved adding

<server name="TELESCOPE_ENABLED" value="false"/> in phpunit.xml.

All 10 comments

I have tried running composer update, composer dump as well as the various artisan commands to clear compiled resources.

I try these following commands.

$ composer create-project --prefer-dist laravel/laravel
$ cd laravel
$ composer require laravel/telescope
$ php vendor/phpunit/phpunit/phpunit --configuration phpunit.xml tests

Everything works normally for me.

Please update to 5.7.11 and check.

There was a bug in 5.7.10 around file permissions - might be related to this.

The issue was caused by my unit test itself. I was overriding the setup function and didn't call
the parent::setUp() function;

Adding this in case someone else has this issue....

I am not using setUp() and got the same error when I try to run a whole class of tests within PHPStorm.
Running a single test did not give me the error though.

I fixed it simply by setting the following in Storm Settings under Test Frameworks :

Default configuration File: ~/myproject/phpunit.xml
Default bootstrap file: ~/myproject/vendor/autoload.php

Experienced the same problem. Interesting enough after commenting out AppProvidersTelescopeServiceProvider::class from the providers list in app.php(config file) and ran my test suites, did not get the error.

Suspecting a call from TelescopeServiceProvider to the .env config file may be causing the problem when running the test suite

Using Laravel 5.7.19

Having this problem with laravel v5.6.39. Using the default TestCase.php and CreatesApplication.php files.

Not using telescope. I'm only using the env() function from my config files.

Strange thing is that the tests do finish, however I just get this error at the end:

 % phpunit
PHPUnit 7.5.4 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 205 ms, Memory: 24.00MB

OK (2 tests, 2 assertions)
PHP Fatal error:  Uncaught ReflectionException: Class env does not exist in /home/somefolder/vendor/laravel/framework/src/Illuminate/Container/Container.php:767
Stack trace:
#0 /home/somefolder/vendor/laravel/framework/src/Illuminate/Container/Container.php(767): ReflectionClass->__construct('env')
#1 /home/somefolder/vendor/laravel/framework/src/Illuminate/Container/Container.php(646): Illuminate\Container\Container->build('env')
#2 /home/somefolder/vendor/laravel/framework/src/Illuminate/Container/Container.php(601): Illuminate\Container\Container->resolve('env', Array)
#3 /home/somefolder/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(734): Illuminate\Container\Container->make('env', Array)
#4 /home/somefolder/vendor/laravel/framework/src/Illuminate/Container/Container.php(1210): Illuminate\Foundation\Application->make('env')
#5 /home/somefolder/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(487): Illuminate\ in /home/somefolder/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 767
PHP Stack trace:
PHP   1. {main}() /home/somefolder/vendor/phpunit/phpunit/phpunit:0
PHP   2. PHPUnit\TextUI\Command::main() /home/somefolder/vendor/phpunit/phpunit/phpunit:61
PHP   3. PHPUnit\TextUI\Command->run() /home/somefolder/vendor/phpunit/phpunit/src/TextUI/Command.php:162
PHP   4. PHPUnit\TextUI\TestRunner->doRun() /home/somefolder/vendor/phpunit/phpunit/src/TextUI/Command.php:206

I'm not getting these errors with any other commands, does anybody have an idea?

Update, when i remove this from my phpunit.xml, the errors go away:

<php>
        <env name="APP_ENV" value="testing"/> <!-- Errors depend on this line -->
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="MAIL_DRIVER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
</php>

https://github.com/laravel/framework/issues/26627#issuecomment-497276990

For someone still having the issue, it can be solved adding

<server name="TELESCOPE_ENABLED" value="false"/> in phpunit.xml.

#26627 (comment)

For someone still having the issue, it can be solved adding
<server name="TELESCOPE_ENABLED" value="false"/> in phpunit.xml.

Perfect !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mnpenner picture mnpenner  路  72Comments

itsgoingd picture itsgoingd  路  81Comments

thewinterwind picture thewinterwind  路  63Comments

nkeena picture nkeena  路  75Comments

JosephSilber picture JosephSilber  路  176Comments