当我在Symfony2项目上运行phpunit时,测试总是在第一次运行时失败:
$ phpunit -c app src/Project/MyBundle/Tests/Controller/SomeTest.php
PHPUnit 3.7.19 by Sebastian Bergmann.
Configuration read from src/main/app/phpunit.xml
E
Time: 1 second, Memory: 24.25Mb
There was 1 error:
1) Project'MyBundle'Tests'Controller'RegistrationTest::testStdRegistration
PHPUnit_Framework_Exception: PHP Fatal error: Interface 'Swift_Events_SendListener' not found in src/main/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Plugins/MessageLogger.php on line 19
PHP Stack trace:
PHP 1. {main}() -:0
PHP 2. require_once() -:542
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
如果我重新启动测试,一切都顺利通过。没有错误。我认为这一切都归结为文件缓存/测试/appTestDebugProjectContainer.php的存在在测试第一次运行之前,此文件不存在。在此运行之后,将创建它并在以下测试中使用。我一直认为Symfony2确保在第一次测试运行之前创建所有相关文件,但事实似乎并非如此。那么我该如何解决这个问题呢?我应该在测试开始前尝试手动预热缓存,还是必须修改测试以等待创建缓存文件?
在执行测试之前预热测试环境缓存,以避免此错误。
app/console cache:warmup --env=test