Unit test interface test in Symfony4


symfony version 4.1.*, use composer create-project symfony/website-skeleton to install.

Target: Test the interface in the current project in a unit test case. Use PHPUnit.

坑1 (send request to use browser-kit):

browser-kit can only send interface requests to the current application, and the request does not pass index.php. The problem with

is that if the current application is configured according to the official guide configuration, such as database connection configuration in .env, then if the request does not go through index.php, the .env file cannot be loaded. It is also impossible to connect to the database. If you want to implement the configuration in .env, you need to modify the framework structure, which is very unfriendly to the novice.

However, you can use xdebug for debugging because it is in the same process.

坑2 (send request to use curl):

For example, use GuzzleHttp, so the request sent is a real http request, so it will be executed in full according to the http request method, just like a real request. come.

This will pass index.php, so there is no need to adjust the framework structure.

However, if this is not the current process, then it is impossible to use xdebug, because xdebug will block all php-fpm processes once it is started.