Foks's avatar
Level 15

Target class [env] does not exist.

Hi!

I'm running on PHP 8, Laravel 8.37 (With Laravel Telescope & Horizon). The application is working just fine, but when I try to run my feature tests this comes

Target class [env] does not exist.

And all my test fails

1 like
16 replies
tykus's avatar

You don't want/need Telescope enabled in the Testing environment

// phpunit.xml
<env name="TELESCOPE_ENABLED" value="false"/>
3 likes
Foks's avatar
Level 15

@tykus My phpunit.xml has it already

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
  <coverage processUncoveredFiles="true">
    <include>
      <directory suffix=".php">./app</directory>
    </include>
  </coverage>
  <testsuites>
    <testsuite name="Unit">
      <directory suffix="Test.php">./tests/Unit</directory>
    </testsuite>
    <testsuite name="Feature">
      <directory suffix="Test.php">./tests/Feature</directory>
      <directory suffix="Test.php">./Modules/Fortify/Tests/Feature</directory>
    </testsuite>
  </testsuites>
  <php>
    <server name="APP_ENV" value="testing"/>
    <server name="BCRYPT_ROUNDS" value="4"/>
    <server name="CACHE_DRIVER" value="array"/>
    <server name="DB_CONNECTION" value="sqlite"/>
    <server name="DB_DATABASE" value=":memory:"/>
    <server name="MAIL_MAILER" value="array"/>
    <server name="QUEUE_CONNECTION" value="sync"/>
    <server name="SESSION_DRIVER" value="array"/>
    <server name="TELESCOPE_ENABLED" value="false"/>
	<env name="TELESCOPE_ENABLED" value="false"/>
  </php>
</phpunit>

2 likes
tykus's avatar

Ok, it is already there as a server environment variable.

Do you also have a .env.testing file?

Foks's avatar
Level 15

@tykus I don't have a .env.testing file, as it was working just fine without it before. I was aware of it was already there as a server variable, weren't sure if there was a difference.

tykus's avatar

Just to isolate the source of the issue as Telescope, can you see if this works on the commandline?

TELESCOPE_ENABLED=false ./vendor/bin/phpunit
tisuchi's avatar

@foks Try this-

php artisan config:cache --env=testing

and

php artisan config:clear
11 likes
Foks's avatar
Level 15

@tisuchi Well it fixed some of it, but most of the test still fails

Foks's avatar
Foks
OP
Best Answer
Level 15

@tisuchi I did.

But I solved it by stashing the changes in git, and then reset the branch to the last commit, and then I copied the tests from the stash and it worked. No clue why it broke, but it works now

kosarinin's avatar

Hey! For someone who will face a similar error you can do the following:

Go to vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel::handle method and put within the catch block

dump($e);die;

In my case there was a parsing error:

result = {ParseError} [7]
 message = "syntax error, unexpected '=>' (T_DOUBLE_ARROW)"
 *Error*string = ""
 code = {int} 0
 file = "/app/config/services.php"
 line = {int} 41
 *Error*trace = {array} [5]
 *Error*previous = null
1 like
pinsard's avatar

Years later, this tip is still invaluable, even on Laravel 12.

I inserted an if (app()->isProduction()) inside the config/logging.php file and... bang! Thing is, such an innocent code easily passes uncaught to the sore eyes of an old programmer that is only trying to survive just another day hahaha.

Thanks a lot!

Christofer's avatar

In my case it was caused by this in my config/app.php

app()->isLocal()

this also doesn't work app()->environment('local')

What does work: (env('APP_ENV') === 'local')

In this case, I guess a better alternative is to set a string in .env and just explode it to get the array.

5 likes
stuartcusack's avatar

Similar problems to @Christofer on Laravel 11.

I was defining my default password structure in app\Providers\AppServiceProvider as descibed by the official documentation:

//app\Providers\AppServiceProvider
Password::defaults(function () {
  $rule = Password::min(10);
  
 // causes Target class [env] does not exist.
  return $this->app->isProduction()
    ? $rule->mixedCase()->uncompromised()->numbers()->symbols()
    : $rule;
});

This works:

return app()->environment('production')
  ? $rule->mixedCase()->uncompromised()->numbers()->symbols()
   : $rule;

Please or to participate in this conversation.