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=false
in my.env.dusk.local
or.env
solved 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=false
inphpunit.xml
it didn’t work. I only got the tests to pass after runnnigphp artisan optimize:clear
first.Two Solutions:
Solution One: (mentioned several times above):
phpunit.xml
<env name="TELESCOPE_ENABLED" value="false"/>
Solution Two:
.env
to.env.testing
.env.testing
TELESCOPE_ENABLED=true
toTELESCOPE_ENABLED=false
If Testing With Dusk:
.env
to.env.testing
.env.testing
TELESCOPE_ENABLED=true
toTELESCOPE_ENABLED=false
.env.testing
to.env.dusk.local
Recently 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
false
as a value, but if you type any other value it picks it up, there seems to be something odd with how the config loads thefalse
value. Not sure if this is an issue with PHP Unit or if it’s an issue with Laravel. it also only happens with<server
but if you use<env
per @shanerbaner82 it will work fine as well.Editing file
phpunit.xml
This 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:clear
I 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
TelescopeServiceProvider
when the environment is nottesting
this is done inapp/Providers/AppServiceProvider.php
as such:This works since PHPUnit sets the
APP_ENV=TESTING
as described in the documentation. An alternative solution could be to addTELESCOPE_ENABLED=false
to thephpunit.xml
file.For
Dusk
I simply added theTELESCOPE_ENABLED=false
to the.env.dusk.local
as 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
App
Facades 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::register
as 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::filter
callback 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::filter
function in theTelescopeServiceProvider
resolved 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 clear
php artisan config:clear
Solved 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 exist
While this is easy to fix (either with
TELESCOPE_ENABLED=false
inphpunit.xml
or, 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
/telescope
dashboard 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.16
v1.0.8
phpunit
Just 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:cache
while developing, the suggested solution of addingTELESCOPE_ENABLED=false
tophpunit.xml
will only actually work if you runoptimize:clear
.I commonly run
config:cache
during 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.xml
file 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.php
file.You will then have to set:
TELESCOPE_ENABLED=true
in 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:clear
fixes the error for me.Regarding
config:cache
- I actually inherited an app whereconfig:cache
could not be run without error, so now that I have fixed that – I am hoping my tests CAN test against theconfig:cache
values 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:cache
and then run tests without this error. 😦+1 for documenting.
TELESCOPE_ENABLED=false
in.env.testing
works 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.