gpt4 book ai didi

symfony - 如何在单元测试我的 Symfony2 包时显示 PHP 错误?

转载 作者:行者123 更新时间:2023-12-02 18:38:39 30 4
gpt4 key购买 nike

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

示例:

# 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 项目以在单元测试期间始终报告错误?

最佳答案

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

  • convertErrorsToException
  • convertNoticesToExceptions
  • convertNoticesToExceptions

关于symfony - 如何在单元测试我的 Symfony2 包时显示 PHP 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21663298/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com