Asked  7 Months ago    Answers:  5   Viewed   39 times

I've an issue with my JSON. It works returns correctly in PHP 5.3 (so I can't use json_last_error()), and it returns successfully when I copy string explicitly into json_decode (json_decode('{...}'). It only returns null in when I pass the result as a variable and I'm using php 5.2, which is what I need it for.

The output comes from JSON logging in PHPUnit:

[
    {
        "event": "suiteStart",
        "suite": "",
        "tests": 2
    },
    {
        "event": "suiteStart",
        "suite": "TagTestCase",
        "tests": 2
    },
    {
        "event": "test",
        "suite": "TagTestCase",
        "test": "TagTestCase::test_it",
        "status": "fail",
        "time": 0.00248718261719,
        "trace": [
            {
                "file": "/UnitTest/PHPUnit.php",
                "line": 98,
                "function": "run",
                "class": "PHPUnit_Framework_TestSuite",
                "type": "->",
                "args": [
                    {

                    }
                ]
            },
            {
                "file": "/UnitTest/PHPUnit.php",
                "line": 116,
                "function": "run",
                "class": "PHPUnit",
                "type": "->",
                "args": [

                ]
            },
            {
                "file": "/UnitTest/PHPUnit.php",
                "line": 212,
                "function": "__tostring",
                "class": "PHPUnit",
                "type": "->",
                "args": [

                ]
            }
        ],
        "message": "false assertionzzzzz.nFailed asserting that <boolean:false> is true."
    },
    {
        "event": "test",
        "suite": "TagTestCase",
        "test": "TagTestCase::test_two",
        "status": "pass",
        "time": 0.00182914733887,
        "trace": [

        ],
        "message": ""
    }
]

EDIT: These are the paths, I've been exploring - maybe you are a better explorer.. Three possible paths that could help:

  • What is different about json_decode() in php 5.2 then 5.3? what did they change?
  • Someone else using JSON from PHPUnit, and how they parse it.
  • What changes when you have it in a variable vs. printing it to screen and copying it into json_decode()

Any help would be greatly(!) appreciated.

Thanks! Matt

 Answers

19

What a HORRENDOUS debug session.. well there's good news.. I figured it out..

I started looking at it using AJAX and logging it with Firebug... and it turns out json_decode (or eval by the way) cannot handle &quot;, which is what PHPUnit sends back (Come on Sebastian!), so to fix it:

$json = str_replace('&quot;', '"', $json);

Now I thought they were the same.. maybe someone can enlighten me..

Wednesday, March 31, 2021
 
WooDzu
answered 7 Months ago
81

You need to make sure that the newly installed php command is executed, not the default one.

Add the folder where the correct php binary resides as the first item to the $PATH environment variable.

Saturday, May 29, 2021
 
oroshnivskyy
answered 5 Months ago
80

The compiler first tries to evaluate the right-hand expression:

GetBoolValue() ? 10 : null

The 10 is an int literal (not int?) and null is, well, null. There's no implicit conversion between those two hence the error message.

If you change the right-hand expression to one of the following then it compiles because there is an implicit conversion between int? and null (#1) and between int and int? (#2, #3).

GetBoolValue() ? (int?)10 : null    // #1
GetBoolValue() ? 10 : (int?)null    // #2
GetBoolValue() ? 10 : default(int?) // #3
Tuesday, June 1, 2021
 
rasmusx
answered 5 Months ago
91

You can work around this known bug in OpenJDK with this:

Map<Integer, Boolean> collect = list.stream()
        .collect(HashMap::new, (m,v)->m.put(v.getId(), v.getAnswer()), HashMap::putAll);

It is not that much pretty, but it works. Result:

1: true
2: true
3: null

(this tutorial helped me the most.)

EDIT:

Unlike Collectors.toMap, this will silently replace values if you have the same key multiple times, as @mmdemirbas pointed out in the comments. If you don't want this, look at the link in the comment.

Tuesday, June 1, 2021
 
Evernoob
answered 5 Months ago
59

I ran into the same issue and found the following solution in the documentation: http://symfony.com/doc/current/book/testing.html#your-first-functional-test

To run your functional tests, the WebTestCase class bootstraps the kernel of your application. In most cases, this happens automatically. However, if your kernel is in a non-standard directory, you'll need to modify your phpunit.xml.dist file to set the KERNEL_DIR environment variable to the directory of your kernel:

<phpunit>
    <!-- ... -->
    <php>
        <server name="KERNEL_DIR" value="/path/to/your/app/" />
    </php>
    <!-- ... -->
</phpunit>

So check your phpunit.xml.dist configuration file and try to add the absolute path to your app-directory.

Hope it helps.

Friday, July 30, 2021
 
Bálint Molnár
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :