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

Most upvoted comments

You could just add <env name="TELESCOPE_ENABLED" value="false"/> to the phpunit.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 in phpunit.xml it didn’t work. I only got the tests to pass after runnnig php artisan optimize:clear first.

Two Solutions:

Solution One: (mentioned several times above):

  1. Open phpunit.xml
  2. Add <env name="TELESCOPE_ENABLED" value="false"/>

Solution Two:

  1. Copy .env to .env.testing
  2. Open .env.testing
  3. Change TELESCOPE_ENABLED=true to TELESCOPE_ENABLED=false

If Testing With Dusk:

  1. Copy .env to .env.testing
  2. Open .env.testing
  3. Change TELESCOPE_ENABLED=true to TELESCOPE_ENABLED=false
  4. Copy .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 the false 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

<server name="TELESCOPE_ENABLED" value="false"/>

But if you update it to this, it will work

<server name="TELESCOPE_ENABLED" value="0"/>

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 the false 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

<server name="TELESCOPE_ENABLED" value="false"/>

But if you update it to this, it will work

<server name="TELESCOPE_ENABLED" value="0"/>

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:

30) Tests\Feature\DailyCheckins\WeeklyCheckinsControllerTest::it_allow_users_to_record_a_weekly_checkin_for_themselves
ReflectionException: Class env does not exist

/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:779
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:658
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:609
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:735
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:1222
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:482
/var/www/html/app/Providers/TelescopeServiceProvider.php:22
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/HigherOrderCollectionProxy.php:60
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/HigherOrderCollectionProxy.php:60
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/Collection.php:455
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/HigherOrderCollectionProxy.php:61
/var/www/html/vendor/laravel/telescope/src/Telescope.php:232
/var/www/html/vendor/laravel/telescope/src/Telescope.php:201
/var/www/html/vendor/laravel/telescope/src/Telescope.php:235
/var/www/html/vendor/laravel/telescope/src/Telescope.php:358
/var/www/html/vendor/laravel/telescope/src/Watchers/QueryWatcher.php:43
/var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:360
/var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:209
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:826
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:682
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:635
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:459
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:411
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/Processor.php:32
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2595
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1318
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:823
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:788
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:651
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:206
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/Collection.php:418
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:207
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:181
/var/www/html/tests/Concerns/AttachJwtToken.php:33
/var/www/html/tests/Feature/WeeklyCheckins/WeeklyCheckinsControllerTest.php:20

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 not testing this is done in app/Providers/AppServiceProvider.php as such:

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    if ($this->app->environment() != 'testing') {
        $this->app->register(TelescopeServiceProvider::class);
    }
}

This works since PHPUnit sets the APP_ENV=TESTING as described in the documentation. An alternative solution could be to add TELESCOPE_ENABLED=false to the phpunit.xml file.

For Dusk I simply added the TELESCOPE_ENABLED=false to the .env.dusk.local as suggested by @mikeminckle

Even 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?

You could just add <env name="TELESCOPE_ENABLED" value="false"/> to the phpunit.xml-file.

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.

if ($this->app->isLocal() || $this->app->runningUnitTests())
if ($this->app->environment(['local', 'testing']))

It may caused by the $this['env'] is not defined when the environment is testing \Illuminate\Foundation\Application::isLocal:

public function isLocal()
{
    return $this['env'] === 'local';
}

In short, the \App\Providers\TelescopeServiceProvider::register as changed below:

public function register()
{
    // Telescope::night();

    $this->hideSensitiveRequestDetails();

    Telescope::filter(function (IncomingEntry $entry) {
        if (App::environment(['local', 'testing'])) {
            return true;
        }

        return $entry->isReportableException() ||
            $entry->isFailedRequest() ||
            $entry->isFailedJob() ||
            $entry->isScheduledTask() ||
            $entry->hasMonitoredTag();
    });
}

If anyone else gets confused like I did about where to put the env variable in phpunit.xml, it goes inside the php tag.

<phpunit>
    <php>
        <env name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

@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 the TelescopeServiceProvider 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.

        // Disable the filter while in local or testing environment.
        $disableFilter = $this->app->environment(['local', 'testing']);
        Telescope::filter(function (IncomingEntry $entry) use ($disableFilter) {
            if ($disableFilter) {
                return true;
            }

            return $entry->isReportableException() ||
                $entry->isFailedJob() ||
                $entry->isScheduledTask() ||
                $entry->hasMonitoredTag();
        });

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):

  • Existing Laravel 5.7 app
  • run phpunit - all tests pass
  • install Telescope
  • run phpunit - ReflectionException: Class env does not exist

While this is easy to fix (either with TELESCOPE_ENABLED=false in phpunit.xml or, as @lionslair mentioned, in AppServiceProvider) 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

  • Install a fresh copy of Laravel v5.7.16
  • Install Telescope v1.0.8
  • Run phpunit
ReflectionException: Class env does not exist

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.

Telescope::filter(function (IncomingEntry $entry) {
    if (in_array(config()->get('app.env'), ['local', 'testing'])) {
        return true;
    }

     return $entry->isReportableException() ||
           $entry->isFailedRequest() ||
           $entry->isFailedJob() ||
           $entry->isScheduledTask() ||
           $entry->hasMonitoredTag() ||
           $entry->isSlowQuery();
});

I’m not sure why this is closed. This issue is still present.

@gordonhung’s workaround works for great for me:

if (App::environment(['local', 'testing'])) {

Or as I like to do, replace the two instances in app/Providers/TelescopeServiceProvider.php:

if ($this->app->isLocal()

to:

if (app()->environment(['local', 'testing'])) {

Update for those still experiencing this issue, if you commonly run config:cache while developing, the suggested solution of adding TELESCOPE_ENABLED=false to phpunit.xml will only actually work if you run optimize:clear.

I commonly run config:cache during dev, should cached config take precedence over phpunit.xml? Is this something that can be checked when running tests? Not sure if this the intended behavior @taylorotwell @themsaid ?

this fixed it for me

Update for those still experiencing this issue, if you commonly run config:cache while developing, the suggested solution of adding TELESCOPE_ENABLED=false to phpunit.xml will only actually work if you run optimize:clear.

I commonly run config:cache during dev, should cached config take precedence over phpunit.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 the phpunit.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.

    /*
    |--------------------------------------------------------------------------
    | Telescope Master Switch
    |--------------------------------------------------------------------------
    |
    | This option may be used to disable all Telescope watchers regardless
    | of their individual configuration, which simply provides a single
    | and convenient way to enable or disable Telescope data storage.
    |
    */

    'enabled' => env('TELESCOPE_ENABLED', false), // Change from true to false here.

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 where config:cache could not be run without error, so now that I have fixed that – I am hoping my tests CAN test against the config: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 a config: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.