Question

I have some acceptance tests hitting my REST API that are all basically the same pattern:

<?php
$I = new WebGuy($scenario);
$I->wantTo('Do something');
$I->haveHttpHeader('Accept', 'application/hal+json');
$I->sendGET('contact/1/website');
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(array(...

The problem in some of my tests occur with seeResponseContainsJson. Given a JSON response:

{
    _links: {
        self: {
            href: "http://myurl/api/v1/contact/1/website"
        }
        describedBy: {
            href: "http://myurl/api/v1/documentation/collection"
        }
    }
    _embedded: {
        items: [1]
            0:  {
                id: 1
                label: "Personal Site"
                url: "http://someurl.com"
                _links: {
                    self: {
                        href: "http://myurl/api/v1/contact/1/website/1"
                    }
                }
            }
    }
}

When trying to test the format of the response with seeResponseContainsJson, I have the following:

$I->seeResponseContainsJson(array(
    '_links' => array(
        'self' => array(
            'href' => $baseUrl . 'contact/1/website'
        ),
        'describedBy' => array(
            'href' => $baseUrl . 'documentation/collection' 
        ),      
    ), 
    '_embedded' => array(

    ),
));

The issue is that this always fails. However, if I remove the check for _embedded:

$I->seeResponseContainsJson(array(
    '_links' => array(
        'self' => array(
            'href' => $baseUrl . 'contact/1/website'
        ),
        'describedBy' => array(
            'href' => $baseUrl . 'documentation/collection' 
        ),      
    ), 
));

Everything works fine. As far as I can tell the responses are the same and there doesn't seem to be any evidence of why _embedded would cause the JSON parsing to blow up as it does. Any ideas?

Was it helpful?

Solution

It seems like seeResponseContainsJson does not handle empty arrays. If I further implement what I assert the response to be until there are no empty arrays:

$I->seeResponseContainsJson(array(
    '_links' => array(
        'self' => array(
            'href' => $baseUrl . 'contact/1/website'
        ),
        'describedBy' => array(
            'href' => $baseUrl . 'documentation/collection' 
        ),      
    ), 
    '_embedded' => array(
        'items' => array(
            array(
                'id' => 1,
            ),
        ),
    ),
));

My tests are now passing. This is sufficient for now, but perhaps handling empty arrays is a future feature request?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top