我正在 Laravel 中对应用程序进行测试,我想更改环境变量的默认配置。 在 Laravel 文档中:
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.
如何从 phpunit.xml
更改环境?
这是文件:
<?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>
最佳答案
只需添加 <php>
包含测试环境变量的部分:
<?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>
关于php - laravel 编辑 phpunit.xml 以在测试时更改环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30462907/