VisualPHPUnit 2.2给了我BAD JSON

时间:2014-10-20 11:44:19

标签: php json phpunit

我正在使用VisualPHPUnit 2.2和PHPUnit 4.3.1(与phar一起安装)和selenium,如果我在命令行中运行它可以工作,如果我从VisualPHPUnit运行它运行,但JSON没有正确导出。 Hier是我在检查器中看到的导出JSON:

{  
   "event":"test",
   "suite":"WebTest",
   "test":"WebTest::testTitle",
   "status":"pass",
   "time":2.4046840667725,
   "trace":[  

   ],
   "message":"",
   "output":""
}{  
   "suites":[  

   ],
   "stats":{  
      "suites":{  
         "succeeded":0,
         "skipped":0,
         "incomplete":0,
         "failed":0,
         "total":0,
         "percentSucceeded":0,
         "percentSkipped":0,
         "percentIncomplete":0,
         "percentFailed":0
      },
      "tests":{  
         "succeeded":0,
         "skipped":0,
         "incomplete":0,
         "failed":0,
         "total":0,
         "percentSucceeded":0,
         "percentSkipped":0,
         "percentIncomplete":0,
         "percentFailed":0
      }
   },
   "errors":[  

   ],
   "notifications":[  

   ]
}

这个问题是:多个JSON根元素

因此,php导出无法正常工作,或者是其他问题?

如果我用下一个代码转换所有错误,那么其他事情:

      error_reporting(E_ALL);
      ini_set('display_errors', 1);

我在JSON之间收到一些警告:

{ "event": "test", "suite": "WebTest", "test": "WebTest::testTitle", "status": "pass", "time": 2.4141600131989, "trace": [ ], "message": "", "output": ""}Notice: ob_end_clean(): failed to delete buffer. No buffer to delete in /var/www/app/lib/VPU.php on line 441
Warning: Cannot modify header information - headers already sent by (output started at /home/interweb/vendor/phpunit/phpunit/src/Util/Printer.php:172) in /var/www/nx/core/Response.php on line 145

Warning: Cannot modify header information - headers already sent by (output started at /home/interweb/vendor/phpunit/phpunit/src/Util/Printer.php:172) in /var/www/nx/core/Response.php on line 147
{"suites":[],"stats":{"suites":{"succeeded":0,"skipped":0,"incomplete":0,"failed":0,"total":0,"percentSucceeded":0,"percentSkipped":0,"percentIncomplete":0,"percentFailed":0},"tests":{"succeeded":0,"skipped":0,"incomplete":0,"failed":0,"total":0,"percentSucceeded":0,"percentSkipped":0,"percentIncomplete":0,"percentFailed":0}},"errors":[],"notifications":[]}

Hier是我的config / bootstrap.php

// The directory where PEAR is located
'pear_path'      => '/usr/share/php',

// The directories where the tests reside
'test_directories' => array(
    "/home/websites/root/application/classes/controller/admin/unit/test/classes/"
),

...

require_once '/home/inter/vendor/autoload.php';
require_once '/home/inter/vendor/phpunit/phpunit/src/Util/Log/JSON.php';

所以任何人都可以帮助我,在其他服务器上它工作正常,但使用一些较旧的PHPUnit。

2 个答案:

答案 0 :(得分:1)

是的,我也尝试了,并花了一整天时间来解决这个问题。 解决方案很简单:

在VPU.php中找到函数run_tests

更改行

$result->addListener(new \PHPUnit_Util_Log_JSON();

$result->addListener(new \PHPUnit_Util_Log_JSON("/tmp/res.json")); // or any other file you want to output to.

然后评论该行

$results = ob_get_contents();

并将其更改为:

    $results = file_get_contents("/tmp/res.json"); // or the file you set as output file in Listener

此外,您应该在同一文件中更改所有行:

ob_end_clean();

if (ob_get_length()) ob_end_clean();

这对我有用。

答案 1 :(得分:0)

感谢@yuriscom迈出了第一步。但是上面的解决方法XML文件仍然失败。所以我进一步挖掘了VPU和PHPUnit 4.2+。我找到的是CONST" PHPUNIT_TESTSUITE"需要为PHPUnit 4.2+设置才能正确地处理输出缓冲(参见PHPUnit_Framework_TestCase版本4.6中的第1882行)。

所以现在你唯一需要做的就是设置CONST而不是上面提到的所有更改。我是在VPU类的新添加构造函数中完成的(文件" VisualPHPUnit \ 2.2 \ app \ lib \ VPU.php"在顶部),如下所示:

class VPU {

    // added this function, not showing original annotations above
    public function __construct()
    {
        define('PHPUNIT_TESTSUITE', true);
    }

    ... // rest of class
}

现在,VPU使用menue和XML文件中的单个测试选择。希望这有助于(并且确实)每个人都有同样的问题。