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
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.
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.
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.
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 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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With