PHPUnit –log-json is Broken, Here’s How to Fix It

For some reason, PHPUnit doesn’t produce valid Json when using the –log-json command-line option.

The output looks like this.

{
    "event": "suiteStart",
    "suite": "TestAcme",
    "tests": 3
}{
    "event": "testStart",
    "suite": "TestAcme",
    "test": "TestAcme::testAcme1"
}{
    "event": "test",
    "suite": "TestAcme",
    "test": "TestAcme::testAcme2",
    "status": "pass",
    "time": 1.85778188705,
    "trace": [],
    "message": "",
    "output": "t"
}{
    "event": "testStart",
    "suite": "TestAcme",
    "test": "TestAcme::testAcme3"
}{
    "event": "test",
    "suite": "TestAcme",
    "test": "TestAcme::testAcme3",
    "status": "pass",
    "time": 12.4938511848,
    "trace": [],
    "message": "",
    "output": ""
}

That’s invalid. It should be an array of objects. So to fix this, I do this when decoding.

json_decode('[' . str_replace('}{', '},{', $json) . ']');

Here’s the bug report on this issue. Someone has proposed a command-line argument to tell phpunit to produce valid Json. Why not just fix the output so that it’s valid Json? Apparently that’s not going to happen.

Share Button