2

I'm making tests for an application in Laravel and I want to change the default config of the environment variable. In Laravel documentation:

When running unit tests, Laravel will automatically set the configuration environment to testing. You are free to create other testing environment configurations as necessary. The testing environment variables may be configured in the phpunit.xml file.

How can I change the environment from phpunit.xml?

Here is the file:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
     backupStaticAttributes="false"
     bootstrap="bootstrap/autoload.php"
     colors="true"
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     processIsolation="false"
     stopOnFailure="false"
     syntaxCheck="false"
>
   <testsuites>
      <testsuite name="Application Test Suite">
         <directory>./app/tests/</directory>
      </testsuite>
   </testsuites>
</phpunit>
1
  • you can accept my answer for further readers if it was helpful :) Commented Jun 5, 2015 at 11:55

2 Answers 2

2

Just add <php> section with variables for testing environment:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false">
    <testsuites>
        …
    </testsuites>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="DB_DATABASE" value="…"/>
        <env name="DB_USERNAME" value="…"/>
        <env name="DB_PASSWORD" value="…"/>
    </php>
</phpunit>
Sign up to request clarification or add additional context in comments.

1 Comment

i want to use my default .env.php on testing environment and not have to add .env.testing.php or edit phpunit.xml ! thx
1

You can have PHPUnit load the .env file of your choice during setup. In TestCase ...

public function createApplication()
{
    $app = require __DIR__ . '/../bootstrap/app.php';

    Dotenv::load(__DIR__ . '/../', '.env.testing');

    $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

    $this->baseUrl = env('APP_URL', $this->baseUrl);

    return $app;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.