laravel / telescope

An elegant debug assistant for the Laravel framework.
https://laravel.com/docs/telescope
MIT License
4.88k stars 582 forks source link

ReflectionException: Class env does not exist #347

Closed Noogic closed 6 years ago

Noogic commented 6 years ago

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.

cedaesca commented 5 years ago

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

opejovic commented 5 years ago

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

+1

DiederikvandenB commented 5 years ago

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();
        });
gordonhung commented 5 years ago

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();
    });
}
Patabugen commented 4 years ago

To add to @gordonhung 's workaround:

You need to add the Use for the facade, which is this: (it always takes me a bit of time to find these): use Illuminate\Support\Facades\App

And I also needed to add it to the hideSensitiveRequestDetails method: \App\Providers\TelescopeServiceProvider::hideSensitiveRequestDetails

pedzed commented 4 years ago

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'])) {
klocus commented 4 years ago

Why this issue is closed? That error still exists.

quaidesbalises commented 4 years ago

I confirm, still have the issue

dillingham commented 4 years ago

Open a new issue. Likely not seeing it.

pedzed commented 4 years ago

Open a new issue. Likely not seeing it.

If possible, it's better to reopen this one.

@Noogic could you reopen this issue?

Mloweedgar commented 4 years ago

I add TELESCOPE_ENABLED=false to my testing environment file and it's work fine. Thanks

I did the same and It worked like charm!

ChrisThompsonTLDR commented 4 years ago

We are seeing this when using Dusk and Telescope, but only when a Dusk test utilizes the setUp() method.

Disabling Telescope in phpunit.xml didn't seem to work.

Removing App\Providers\TelescopeServiceProvider.php from config/app.php worked.

jellisii commented 4 years ago

https://github.com/laravel/laravel/commit/5f9ee30e379390f9ce506d934314cb6ff6d7d355#diff-ea896f6d36682ab3ef2d6e3f3de5abce

christopherarter commented 4 years ago

Still seeing this issue on Laravel 7.20.0, Telescope 3.5.0 and Dusk v6.4.1.

I can run each test separately just fine, but php artisan dusk fails with Class env does not exist.

I have added <server name="TELESCOPE_ENABLED" value="false"/> in the correct place in the phpunit.xml file, run optimize:clear and config:cache, and I'm still getting this issue.

WyattCast44 commented 4 years ago

@christopherarter did you try running config:clear?

It sounds like you ran optimize:clear and then config:cache which would negate the effect of running optimize:clear?

Hope this helps 🙂

christopherarter commented 4 years ago

@WyattCast44 unfortunately not. Still having this issue.

It's basically not recognizing the values in the phpunit.xml file for any tests beyond the first one.

However, if anyone is still having this problem after trying the answers above, though this is not a fix, I ended up just putting TELESCOPE_ENABLED=false in my .env.dusk.local file and it's working.

shanerbaner82 commented 4 years ago

Not using dusk, added <env name="TELESCOPE_ENABLED" value="false"/> to phpunit.xml and everything seems to be working fine for me.

tomheadifen commented 4 years ago

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.

josezenem commented 4 years ago

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"/>
Shaggy84675 commented 4 years ago

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

fefogarcia commented 4 years ago

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"/>

I was just having the same issue after upgrading to Laravel 8 and this is the only thing that fixed it. Thank you.

Blair2004 commented 3 years ago

Sometimes, you might need to run composer dump-autoload.

valehasadli commented 3 years ago

This error still actual.

after TELESCOPE_ENABLED=false the php artisan test command is worked, but it's not the way to handle this issue.

I need a real solution to work at the same time with each functionality. Because after TELESCOPE_ENABLED = false command telescope does not work. And it's too much time every time turn on / turn off.

wocodes commented 3 years ago

Also, as an alternative, just changing this 'enabled' => env('TELESCOPE_ENABLED', true), to 'enabled' => env('TELESCOPE_ENABLED', config('app.env')!=='testing'),

in your telescope config file solves it too. As telescope will only be activated when the env is changed to testing by phpunitxml.

Dhermann27 commented 2 years ago

Same here. It'd really be nice to see telescope entries for my dusk testcases. +1

TWithers commented 1 year ago

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();
});