telescope: ReflectionException: Class env does not exist
After installing Telescope, all my Dusk tests are throwing this error:
1) Tests\Browser\PatientsTest::it_can_create_a_patient
ReflectionException: Class env does not exist
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Container/Container.php:779
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Container/Container.php:658
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Container/Container.php:609
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:733
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Container/Container.php:1222
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:497
/Users/noogic/data/projects/fisio/app/Providers/TelescopeServiceProvider.php:22
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Support/HigherOrderCollectionProxy.php:60
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Support/HigherOrderCollectionProxy.php:60
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Support/Collection.php:442
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Support/HigherOrderCollectionProxy.php:61
/Users/noogic/data/projects/fisio/vendor/laravel/telescope/src/Telescope.php:206
/Users/noogic/data/projects/fisio/vendor/laravel/telescope/src/Telescope.php:179
/Users/noogic/data/projects/fisio/vendor/laravel/telescope/src/Telescope.php:209
/Users/noogic/data/projects/fisio/vendor/laravel/telescope/src/Telescope.php:233
/Users/noogic/data/projects/fisio/vendor/laravel/telescope/src/Watchers/CacheWatcher.php:63
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:360
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:209
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Cache/Repository.php:486
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Cache/Repository.php:92
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php:323
/Users/noogic/data/projects/fisio/vendor/laravel/telescope/src/Watchers/DumpWatcher.php:43
/Users/noogic/data/projects/fisio/vendor/laravel/telescope/src/RegistersWatchers.php:48
/Users/noogic/data/projects/fisio/vendor/laravel/telescope/src/Telescope.php:94
/Users/noogic/data/projects/fisio/vendor/laravel/telescope/src/TelescopeServiceProvider.php:27
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:87
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:31
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Container/Container.php:572
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:793
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:776
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:777
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php:17
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:206
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:297
/Users/noogic/data/projects/fisio/tests/CreatesApplication.php:18
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:91
/Users/noogic/data/projects/fisio/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:68
/Users/noogic/data/projects/fisio/vendor/laravel/dusk/src/TestCase.php:23
/Users/noogic/data/projects/fisio/tests/Browser/PatientsTest.php:16
The only “test” that is passing is the login test.
<?php
namespace Tests\Browser;
use App\Entities\Patient;
use App\Entities\User;
use Database\Builders\PatientBuilder;
use Tests\Browser\Pages\Login;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
class PatientsTest extends DuskTestCase
{
/** @test */
function login()
{
$this->browse(function (Browser $browser) {
$browser
->visit(new Login)
->signIn()
;
});
}
/** @test */
function it_can_create_a_patient()
{
$this->browse(function (Browser $browser) {
$browser
->pause(200)
->visit('/patients')
->resize(1024, 768)
->waitFor('@patients.create.showForm')
->press('@patients.create.showForm')
->waitFor('@patients.create.form')
->type('@patients.create.name', 'test patient')
->type('@patients.create.email', 'tp@test.com')
->press('@patients.create.submit')
->pause(400)
;
});
$this->assertDatabaseHas('patients', ['name' => 'test patient', 'email' => 'tp@test.com']);
}
I’m using laravel version v5.7.12 and phpunit tests are working well, only dusk is failing.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 17
- Comments: 76 (8 by maintainers)
Commits related to this issue
- Fixing: ReflectionException: Class env does not exist see https://github.com/laravel/telescope/issues/347 — committed to joachimdieterich/laravel-curriculum-adminlte3 by joachimdieterich 5 years ago
- Preparations for PHPUnit testing There is a strange conflict between phpunit and telescope, so unfortunately i had to disable telescope in the testing environment. See https://github.com/laravel/tele... — committed to askuri/bauassets by askuri 4 years ago
You could just add
<env name="TELESCOPE_ENABLED" value="false"/>to thephpunit.xml-file.setting
TELESCOPE_ENABLED=falsein my.env.dusk.localor.envsolved the issue.Not sure this is really an issue but maybe needs to be documented.
I was getting the same error, and even with
TELESCOPE_ENABLED=falseinphpunit.xmlit didn’t work. I only got the tests to pass after runnnigphp artisan optimize:clearfirst.Two Solutions:
Solution One: (mentioned several times above):
phpunit.xml<env name="TELESCOPE_ENABLED" value="false"/>Solution Two:
.envto.env.testing.env.testingTELESCOPE_ENABLED=truetoTELESCOPE_ENABLED=falseIf Testing With Dusk:
.envto.env.testing.env.testingTELESCOPE_ENABLED=truetoTELESCOPE_ENABLED=false.env.testingto.env.dusk.localRecently upgraded to Laravel 8 from Laravel 7, I just started experiencing this issue. I am running PHP 7.4.
After some debugging, I found that PHPUnit will ignore
falseas a value, but if you type any other value it picks it up, there seems to be something odd with how the config loads thefalsevalue. Not sure if this is an issue with PHP Unit or if it’s an issue with Laravel. it also only happens with<serverbut if you use<envper @shanerbaner82 it will work fine as well.Editing file
phpunit.xmlThis will not work
But if you update it to this, it will work
You saved my day! Same problem on Laravel 8. After this change I’ve also had to clear config cache using
php artisan config:clearI am experiencing this issue as well, but only when I run all my tests, when run individually, they all pass, when run together only one of them passes. Here is an example of the failure message:
I add TELESCOPE_ENABLED=false to my testing environment file and it’s work fine. Thanks
Thanks for the explenation @themsaid and @mikeminckle, so here is what I did for PHPUnit:
Only register the
TelescopeServiceProviderwhen the environment is nottestingthis is done inapp/Providers/AppServiceProvider.phpas such:This works since PHPUnit sets the
APP_ENV=TESTINGas described in the documentation. An alternative solution could be to addTELESCOPE_ENABLED=falseto thephpunit.xmlfile.For
DuskI simply added theTELESCOPE_ENABLED=falseto the.env.dusk.localas suggested by @mikeminckleEven though this solves the issue, I don’t really think this is a viable solution. This means that all of our tests are only run without Laravel Telescope, but our production server will be running with Laravel Telescope. Does anyone know what is causing this?
This worked for me like a charm
Why this issue is closed? That error still exists.
Same case with @DiederikvandenB , I would like to use Telescope in the test and found same root cause. The different is, I use
AppFacades to determinate the environment because that use$this->app()trigger same error:ReflectionException: Class env does not exist.It may caused by the
$this['env']is not defined when the environment is testing\Illuminate\Foundation\Application::isLocal:In short, the
\App\Providers\TelescopeServiceProvider::registeras changed below:If anyone else gets confused like I did about where to put the env variable in phpunit.xml, it goes inside the php tag.
@bilfeldt disable Telescope while running your tests as @mikeminckler suggests.
The problem before was a cache key stored very early by dusk and causing
Telescope::filtercallback to be invoked and by this time there’s no access to the environment available.People have suggested disabling Telescope in testing environments to resolve this issue, but that was not really an answer for me since I like to use Telescope to debug my failing tests.
I found out that commenting out the default
Telescope::filterfunction in theTelescopeServiceProviderresolved the issue, but not being able to use the filter is not ideal either. I decided to grab the environment earlier on and pass it through to the callback.I’ve runned
php artisan clearphp artisan config:clearSolved for me
I still have this issue with Laravel 5.8 and Telescope 2.0.2.
Obviously, disabling stuff for tests is a terrible workaround. If Telescope would cause any errors in production, they wouldn’t be caught, and the application would crash once deployed.
Please create a fix for this.
@mvdnbrk Another example (as happened to me):
phpunit- all tests passphpunit-ReflectionException: Class env does not existWhile this is easy to fix (either with
TELESCOPE_ENABLED=falseinphpunit.xmlor, as @lionslair mentioned, inAppServiceProvider) the first impression certainly isn’t great.@zanechua not for me. In my case, I have to disable this watchers in order to make it work:
Command, Dump, Log, Model, Notification, Query and Schedule
None worked for me. I have manually paused monitoring from
/telescopedashboard and it worked.@themsaid can’t anything be done about this? it’s affecting all new installs for those of us that use tests
v5.7.16v1.0.8phpunitJust needed an hour or so to figure this out 😒
Just ran into this issue. Wanted to use telescope with dusk as well. Fix for me was to use
config()->get('app.env')instead of$this->app->environment()in the TelescopeServiceProvider.php filter.I’m not sure why this is closed. This issue is still present.
@gordonhung’s workaround works for great for me:
Or as I like to do, replace the two instances in
app/Providers/TelescopeServiceProvider.php:to:
this fixed it for me
Update for those still experiencing this issue, if you commonly run
config:cachewhile developing, the suggested solution of addingTELESCOPE_ENABLED=falsetophpunit.xmlwill only actually work if you runoptimize:clear.I commonly run
config:cacheduring dev, should cached config take precedence overphpunit.xml? Is this something that can be checked when running tests? Not sure if this the intended behavior @taylorotwell @themsaid ?just ran into this issue, can’t telescope check if
testing?<env name="TELESCOPE_ENABLED" value="FALSE" />added to my phpunit.xml
As suggested by @Livijn , by adding
<env name="TELESCOPE_ENABLED" value="false"/>to thephpunit.xmlfile it fixes this issue.@themsaid since this is an issue with a fresh install of laravel + laravel/telescope, would it be possible to have this note in the official documentation in laravel website? Thanks a lot for the great work and support 😃
Which version of Telescope fixed this @themsaid? It seems to still be present in version
1.0.7.Sometimes, you might need to run
composer dump-autoload.You can change the default to false for telescope by updating your
config/telescope.phpfile.You will then have to set:
TELESCOPE_ENABLED=truein your .env file. This will ensure that you don’t have it accidentally turned on in environments you don’t want such as production and unit testing.So first,
config:clearfixes the error for me.Regarding
config:cache- I actually inherited an app whereconfig:cachecould not be run without error, so now that I have fixed that – I am hoping my tests CAN test against theconfig:cachevalues to prove through testing that the cached config does not break anything. Based on all this though, so far it seems you cannot do aconfig:cacheand then run tests without this error. 😦+1 for documenting.
TELESCOPE_ENABLED=falsein.env.testingworks for me.Temporary solution, set:
'enabled' => env('TELESCOPE_ENABLED', env('APP_ENV') !== 'testing'),in telescope.php
the <env name="TELESCOPE_ENABLED" value="false"/> solution made no difference for me.
Also dusk appears to create its own phpunit xml file. Did not solve the issue when I added it there either.