在symfony应用程序中用PHPUnit测试事件监听器

131 阅读1分钟

假设我们使用一个事件监听器,在一个文本文件和应用程序日志中记录击中CustomerController::getOneAction($id) 端点/方法的用户请求。在这个例子中,我们将只是测试事件监听器,所以只需注意它。

CustomerController

namespace Inanzzz\ApplicationBundle\Controller;
services:

CustomerControllerListener

namespace Inanzzz\ApplicationBundle\Listener;
services:

日志文件

正如你在上面看到的,日志被保存在live 环境下的/var/logs/customer.log 文件中,但我们将在我们的test 环境下模拟它。创建一个名为/tests/Inanzzz/ApplicationBundle/Mock/log/customer.log 的文件,这样我们的测试就可以将日志保存在里面。

CustomerControllerListenerTest

namespace tests\Inanzzz\ApplicationBundle\Command;

结果

$ vendor/bin/phpunit