猿问

如何更改 PHPUnit 错误消息以显示自定义错误

我已经用我自己的扩展了 PHP Exception 来添加额外的数据:


class MyException extends Exception {

   public $foo = [];


   public function __construct($message = '', $data = null, $code = 0) {

       $this->foo = $data;


       paret::__construct($message, $code);

   }

}

在执行正常请求时,这些错误会正确记录,我不想在$this->message.


运行测试时,我可以抛出它:


if (!$this->save()) {

    throw new MyException('Internal Server Error', ['log' => 'missing data'], 500);

}

PHPUnit 将输出:


MyException:内部服务器错误


我想要:


MyExeption:内部服务器错误;{“日志”:“缺失数据”}


如何扩展 PHPUnit 以能够$myException->foo与错误消息一起显示?


示例代码:


<?php


class SampleTest extends CTestCase

{

    public function testIndex()

    {

        $this->assertTrue($this->save());

    }


    protected function save()

    {

        $model = new Orders();

        $model->addError('id', 'ID is Required');


        if (!$model->validate()) {

            throw new HttpApiModelException(500, 'Failed to save', $model);

        }


        return true;

    }

}

用命令执行 common/vendor/bin/phpunit --configuration tests/phpunit.xml --verbose tests/functional/SampleTest.php


和输出:

小怪兽爱吃肉
浏览 133回答 2
2回答

aluckdog

我不确定这是否是最佳选择,但您可以实现测试结果打印机,例如:<?phpnamespace Tests;use PHPUnit\TextUI\ResultPrinter;class TestPrinter extends ResultPrinter{&nbsp; &nbsp; protected function printDefect(\PHPUnit\Framework\TestFailure $defect, $count)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->printDefectHeader($defect, $count);&nbsp; &nbsp; &nbsp; &nbsp; $ex = $defect->thrownException();&nbsp; &nbsp; &nbsp; &nbsp; // you can do whatever you need here,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // like check exception type, etc,&nbsp; &nbsp; &nbsp; &nbsp; // printing just line number here&nbsp; &nbsp; &nbsp; &nbsp; $this->write('Line #' . $ex->getLine() . "\n");&nbsp; &nbsp; &nbsp; &nbsp; $this->printDefectTrace($defect);&nbsp; &nbsp; }}并注册为要使用的打印机(假设使用 xml 配置,但也可以通过命令行完成):<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;backupGlobals="false"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;colors="true"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bootstrap="vendor/autoload.php"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printerClass="Tests\TestPrinter">&nbsp; &nbsp; <!-- note printerClass attribute above --></phpunit>这样做,您将获得与以下内容相似的输出:There was 1 error:1) Tests\SomeTest::testStuffLine #16LogicException: whatever(我只是做了一个简单的测试throw new \LogicException('whatever');)

呼如林

因此,如果您需要在每次运行测试时打印这些数据,而不是在生产时打印,那么为什么不扩展基本 Exception 类并检查您是否处于测试环境中,然后连接消息和数据。然后你所有的自定义异常来扩展这个新的 Exception 类。class BaseException extends Exception {&nbsp; &nbsp;public function __construct($message = '', $data = null, $code = 0) {&nbsp; &nbsp; &nbsp; &nbsp;if (env('ENVIRONMENT') === 'test') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$message .= ' ' . json_encode($data);&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;paret::__construct($message, $code);&nbsp; &nbsp;}}但是此实现将需要更改您的 MyException 类以使用此数据调用父构造函数。而不是这个paret::__construct($message, $code);现在你将拥有这个paret::__construct($message, $data, $code);并且还将新BaseException类扩展到您希望具有此功能的这些异常
随时随地看视频慕课网APP
我要回答