Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing in Laravel 5.5 : Class env does not exist

i am starting tests in laravel

i created a ".env.testing" file with this

APP_NAME=myApp
APP_ENV=testing
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://myApp.localhost

DB_CONNECTION=sqlite_testing

i added this in the "connections" part of config/database.php file

...
'sqlite_testing' => [
    'driver'   => 'sqlite',
    'database' => ':memory:',
    'prefix'   => '',
],
...

in the phpunit.xml file, i added :

<env name="DB_CONNECTION" value="sqlite_testing" />

and i created a UserTest feature :

class UserTest extends TestCase
{

    public function setUp()
    {
        parent::setUp();
        Artisan::call('migrate');
        Artisan::call('db:seed');
    }

    public function tearDown()
    {
        parent::tearDown();
        Artisan::call('migrate:reset');
    }


    public function testCanAccessUserSpace()
    {
        $user = factory(User::class)->create();

        $response = $this->actingAs($user)
            ->get('/home');


        $response->assertStatus(200);
    }
}

But when i run the tests, i have this :

ReflectionException: Class env does not exist

What's wrong with my config ?

thanks

like image 644
pop_up Avatar asked Feb 13 '18 12:02

pop_up


People also ask

What is class env does not exist error in Laravel?

Sometimes, there is an error in Laravel called Class env does not exist specially when you are trying to do PHP unit test on any specific class. In that case, although your test is right, however, it's always shows that Class env does not exist as a result the test is failed.

How do I run a test in Laravel?

After installing a new Laravel application, execute the vendor/bin/phpunit or php artisan test commands to run your tests. When running tests, Laravel will automatically set the configuration environment to testing because of the environment variables defined in the phpunit.xml file.

How does Laravel test work in PHPUnit?

When running tests, Laravel will automatically set the configuration environment to testing because of the environment variables defined in the phpunit.xml file. Laravel also automatically configures the session and cache to the array driver while testing, meaning no session or cache data will be persisted while testing.

Does PHP artisan dusk fail with class env?

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.


2 Answers

I had same error while running the phpunit:

ReflectionException: Class env does not exist

Here was another problem, I've installed package telescope and phpunit tried to load it while testing:

ReflectionException: Class env does not exist
/var/www/html/mat2/vendor/laravel/telescope/src/Telescope.php:263
/var/www/html/mat2/vendor/laravel/telescope/src/Telescope.php:222

and so on. I've added to phpunit.xml:

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

Afterwards, tests are running fine.
So, it may be package testing related error.

like image 173
frank_108 Avatar answered Sep 21 '22 13:09

frank_108


If you are using php artisan test --env=testing, add TELESCOPE_ENABLED=false to your .env.testing file and run php artisan config:cache --env=testing.

like image 44
Alvi Avatar answered Sep 25 '22 13:09

Alvi