使用 PHPUnit 进行功能测试给了我输入中意外的字符


Functional test with PHPUnit gives me Unexpected character in input

我正在开发一个带有Symfony2的Web应用程序。我的应用程序连接到一些外部服务,我用 Ruby 模拟了这些服务。这是我的红宝石模拟片段

#Bunch of stuff...
after do
    response['Content-type'] = 'application/json'
end
#Bunch of stuff...
get '/configure/set_result' do
    #Bunch of stuff...
    JSON.generate 'success' => true,
        "message" => "whatever"
end

在我正在做的一个功能测试中,我称之为此方法

<?php
class MyControllerTest extends WebTestCase {
    protected function setUp() {
        $this->client = static::createClient();
    }

    public function testWhatever() {
        $this->callMyMock(22);
        //Unimportant stuff
    }
    private function callMyMock($result) {
        $crawler = $this->client->request('GET', "http://localhost:5995/configure/set_result?resultCode=$result");
    }
}

问题是在日志中我收到了很多PHP警告(这实际上使PHPUnit的速度变慢了很多。下面是输出的示例:

WebApp.PHPUnitTests:
     [exec] PHPUnit 3.7.19 by Sebastian Bergmann.
     [exec]
     [exec] Configuration read from C:'Work'New Products Trunk'Software'WebApp'symfony2'app'phpunit.xml.dist
     [exec]
     [exec] PHP Warning:  Unexpected character in input:  '→' (ASCII=26) state=0 in C:'Work'New Products Trunk'Soft
endor'symfony'symfony'src'Symfony'Bridge'Twig'Extension'CodeExtension.php on line 140
     [exec] PHP Stack trace:
     [exec] PHP   1. {main}() C:'_Wallet'phpunit.phar:0
     [exec] PHP   2. PHPUnit_TextUI_Command::main() C:'_Wallet'phpunit.phar:527
     [exec] PHP   3. PHPUnit_TextUI_Command->run() phar://C:/_Wallet/phpunit.phar/PHPUnit/TextUI/Command.php:129
...
...
...
...

我做错了什么?

这是从 .phar 文件运行时的已知错误,因为在这种情况下,执行"突出显示"的函数不会获得文件名,而是获取 phar 的内容,因此会出现意外的输入。

一些背景:您的测试可能会触发一个异常,该异常由内部symfony ErrorHandler处理,该异常显示格式良好的堆栈跟踪,这种格式是触发此警告的原因。

它已在此 PR 中修复:https://github.com/symfony/symfony/pull/8721/files

详情请见本期:https://github.com/symfony/symfony/issues/7798