Symfony 如何加载端到端测试的测试环境?
How does Symfony load the test environment for end-to-end tests?
我正在处理另一个使用端到端测试和 .env
文件的 PHP 项目。然而,在 运行 测试之前,我需要修改 .env
文件以指向测试数据库(而不是开发数据库)。当我在 Symfony 项目上工作时,我认为我不需要这样做,它只是自动加载测试环境。
我从以前使用旧版本的一些经验得知,过去每个环境都有不同的前端控制器,例如 app.php
、app_dev.php
等,但据我所知,现在情况并非如此.
Symfony 如何知道加载端到端测试的测试环境?
使用哪个环境一般在phpunit.xml.dist
中设置。
与 Symfony 相比,这更像是 PhpUnit。
你应该有一个像这样的条目:
<php>
<server name="APP_ENV" value="test" force="true" />
<!-- ... -->
</php>
通过使用 force=true
,它将覆盖任何现有 APP_ENV
环境变量的值。
WebTestCase
将“模拟”请求,如 here. If you use something like Panther, the tests will fire up an internal web server and make "real" HTTP requests, as explained here 所述。
无论哪种情况,应用程序使用的 APP_ENV
都是在 PhpUnit 配置中定义的。
phpunit-bridge
的弹性配方包括一个 phpunit.xml
file,它将 APP_ENV
变量设置为 test
。这会触发 symfony 加载适当的 .env.test
文件。
在测试期间,通常不使用前端控制器,因为桥会实例化一个 Request
对象并将其直接传递给应用程序内核。但是在使用 PantherTestCase
的端到端测试中,框架在内置的 PHP 网络服务器中启动项目,该服务器仍然由环境变量控制。
这在 the book 中比文档本身的测试章节有更好的解释:
The $client variable simulates a browser. Instead of making HTTP calls
to the server though, it calls the Symfony application directly. This
strategy has several benefits: it is much faster than having
round-trips between the client and the server, but it also allows the
tests to introspect the state of the services after each HTTP request.
并且在 Panther announcement:
However, WebTestCase doesn’t use a real web browser. It simulates one
with pure PHP components. It doesn’t even use the HTTP protocol: it
creates instances of HttpFoundation’s Request objects, pass them to
the Symfony kernel, and allows to assert on the HttpFoundation
Response instance returned by the app.
[...]
Under the hood Panther has:
- started your project with the built-in PHP webserver on localhost:9000
- started the version of Chromedriver shipped with the library to automate your local Chrome
- executed the browsing scenario defined in the test with Chrome in headless mode
我正在处理另一个使用端到端测试和 .env
文件的 PHP 项目。然而,在 运行 测试之前,我需要修改 .env
文件以指向测试数据库(而不是开发数据库)。当我在 Symfony 项目上工作时,我认为我不需要这样做,它只是自动加载测试环境。
我从以前使用旧版本的一些经验得知,过去每个环境都有不同的前端控制器,例如 app.php
、app_dev.php
等,但据我所知,现在情况并非如此.
Symfony 如何知道加载端到端测试的测试环境?
使用哪个环境一般在phpunit.xml.dist
中设置。
与 Symfony 相比,这更像是 PhpUnit。
你应该有一个像这样的条目:
<php>
<server name="APP_ENV" value="test" force="true" />
<!-- ... -->
</php>
通过使用 force=true
,它将覆盖任何现有 APP_ENV
环境变量的值。
WebTestCase
将“模拟”请求,如 here. If you use something like Panther, the tests will fire up an internal web server and make "real" HTTP requests, as explained here 所述。
无论哪种情况,应用程序使用的 APP_ENV
都是在 PhpUnit 配置中定义的。
phpunit-bridge
的弹性配方包括一个 phpunit.xml
file,它将 APP_ENV
变量设置为 test
。这会触发 symfony 加载适当的 .env.test
文件。
在测试期间,通常不使用前端控制器,因为桥会实例化一个 Request
对象并将其直接传递给应用程序内核。但是在使用 PantherTestCase
的端到端测试中,框架在内置的 PHP 网络服务器中启动项目,该服务器仍然由环境变量控制。
这在 the book 中比文档本身的测试章节有更好的解释:
The $client variable simulates a browser. Instead of making HTTP calls to the server though, it calls the Symfony application directly. This strategy has several benefits: it is much faster than having round-trips between the client and the server, but it also allows the tests to introspect the state of the services after each HTTP request.
并且在 Panther announcement:
However, WebTestCase doesn’t use a real web browser. It simulates one with pure PHP components. It doesn’t even use the HTTP protocol: it creates instances of HttpFoundation’s Request objects, pass them to the Symfony kernel, and allows to assert on the HttpFoundation Response instance returned by the app.
[...]
Under the hood Panther has:
- started your project with the built-in PHP webserver on localhost:9000
- started the version of Chromedriver shipped with the library to automate your local Chrome
- executed the browsing scenario defined in the test with Chrome in headless mode