单元测试我的Symfony2捆绑包时如何显示PHP错误?

时间:2014-02-09 18:45:55

标签: symfony phpunit

目前,在为我的Symfony2软件包编写单元测试时,我明确设置ini_set('display_errors', 1)以确保我在编写单元测试时看到任何错误。这对于致命错误特别有用,例如由方法名称中的拼写错误导致的错误。

实施例:     

# src/Foo/BarBundle/Tests/Security/RoutePermissionCheckerTest.php

namespace Foo\BarBundle\Tests\Security;

use Symfony\Bundle\FrameworkBundle\Tests\TestCase;

class RoutePermissionCheckerTest extends TestCase
{
    public function testIndex()
    {
        ini_set('display_errors', 1);
        $this->nonExistentMethod();
    }
}

输出:

Configuration read from /Users/maurits_dekkers/Sites/hobby/symfony/saas-seed/app/phpunit.xml.dist
...
Fatal error: Call to undefined method Bb\UserBundle\Tests\Security\RoutePermissionCheckerTest::nonExistentMethod() in /Users/maurits_dekkers/Sites/hobby/symfony/saas-seed/src/Bb/UserBundle/Tests/Security/RoutePermissionCheckerTest.php on line 18

Call Stack:
    0.0314     635432   1. {main}() /Users/maurits_dekkers/pear/bin/phpunit:0
    0.5501    1191328   2. PHPUnit_TextUI_Command::main() /Users/maurits_dekkers/pear/bin/phpunit:46
    0.5502    1192056   3. PHPUnit_TextUI_Command->run() /Users/maurits_dekkers/pear/share/pear/PHPUnit/TextUI/Command.php:129
    1.2241    7133440   4. PHPUnit_TextUI_TestRunner->doRun() /Users/maurits_dekkers/pear/share/pear/PHPUnit/TextUI/Command.php:176
    1.2619    7653720   5. PHPUnit_Framework_TestSuite->run() /Users/maurits_dekkers/pear/share/pear/PHPUnit/TextUI/TestRunner.php:349
   14.0128   37804208   6. PHPUnit_Framework_TestSuite->run() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestSuite.php:705
   14.0130   37804608   7. PHPUnit_Framework_TestSuite->runTest() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestSuite.php:745
   14.0130   37804608   8. PHPUnit_Framework_TestCase->run() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestSuite.php:775
   14.0131   37804608   9. PHPUnit_Framework_TestResult->run() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestCase.php:783
   14.0131   37805600  10. PHPUnit_Framework_TestCase->runBare() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestResult.php:648
   14.0134   37846840  11. PHPUnit_Framework_TestCase->runTest() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestCase.php:838
   14.0134   37848304  12. ReflectionMethod->invokeArgs() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestCase.php:983
   14.0134   37848336  13. Bb\UserBundle\Tests\Security\RoutePermissionCheckerTest->testIndex() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestCase.php:983

但是,通过配置设置必须有更好的方法。我已经尝试在我的config_test.yml中添加一个额外的Monolog设置:

# app/config/config_test.yml
monolog:
    handlers:
        console:
            type:   console
            bubble: false
            level: info

但这不会导致在我的单元测试期间报告PHP错误。单元测试只是停止运行。

是否有一种简单的方法可以将Symfony2项目配置为在单元测试期间始终报告错误?

1 个答案:

答案 0 :(得分:3)

您可能希望在PHPUnit配置文件(true)中将以下参数设置为app/config/phpunit.xml

  • convertErrorsToException
  • convertNoticesToExceptions
  • convertNoticesToExceptions