JsonResponseTest

class JsonResponseTest extends TestCase

Methods

public testConstructorEmptyCreatesJsonObject() No description
public testConstructorWithArrayCreatesJsonArray() No description
public testConstructorWithAssocArrayCreatesJsonObject() No description
public testConstructorWithSimpleTypes() No description
public testConstructorWithCustomStatus() No description
public testConstructorAddsContentTypeHeader() No description
public testConstructorWithCustomHeaders() No description
public testConstructorWithCustomContentType() No description
public testSetJson() No description
public testCreate() No description
public testStaticCreateEmptyJsonObject() No description
public testStaticCreateJsonArray() No description
public testStaticCreateJsonObject() No description
public testStaticCreateWithSimpleTypes() No description
public testStaticCreateWithCustomStatus() No description
public testStaticCreateAddsContentTypeHeader() No description
public testStaticCreateWithCustomHeaders() No description
public testStaticCreateWithCustomContentType() No description
public testSetCallback() No description
public testJsonEncodeFlags() No description
public testGetEncodingOptions() No description
public testSetEncodingOptions() No description
public testItAcceptsJsonAsString() No description
public testSetCallbackInvalidIdentifier() No description
public testSetContent() No description
public testSetContentJsonSerializeError() No description
public testSetComplexCallback() No description

Details

at line 19

testConstructorEmptyCreatesJsonObject()

public testConstructorEmptyCreatesJsonObject()
at line 25

testConstructorWithArrayCreatesJsonArray()

public testConstructorWithArrayCreatesJsonArray()
at line 31

testConstructorWithAssocArrayCreatesJsonObject()

public testConstructorWithAssocArrayCreatesJsonObject()
at line 37

testConstructorWithSimpleTypes()

public testConstructorWithSimpleTypes()
at line 52

testConstructorWithCustomStatus()

public testConstructorWithCustomStatus()
at line 58

testConstructorAddsContentTypeHeader()

public testConstructorAddsContentTypeHeader()
at line 64

testConstructorWithCustomHeaders()

public testConstructorWithCustomHeaders()
at line 71

testConstructorWithCustomContentType()

public testConstructorWithCustomContentType()
at line 79

testSetJson()

public testSetJson()
at line 92

testCreate()

public testCreate()
at line 101

testStaticCreateEmptyJsonObject()

public testStaticCreateEmptyJsonObject()
at line 108

testStaticCreateJsonArray()

public testStaticCreateJsonArray()
at line 115

testStaticCreateJsonObject()

public testStaticCreateJsonObject()
at line 122

testStaticCreateWithSimpleTypes()

public testStaticCreateWithSimpleTypes()
at line 141

testStaticCreateWithCustomStatus()

public testStaticCreateWithCustomStatus()
at line 147

testStaticCreateAddsContentTypeHeader()

public testStaticCreateAddsContentTypeHeader()
at line 153

testStaticCreateWithCustomHeaders()

public testStaticCreateWithCustomHeaders()
at line 160

testStaticCreateWithCustomContentType()

public testStaticCreateWithCustomContentType()
at line 168

testSetCallback()

public testSetCallback()
at line 176

testJsonEncodeFlags()

public testJsonEncodeFlags()
at line 183

testGetEncodingOptions()

public testGetEncodingOptions()
at line 190

testSetEncodingOptions()

public testSetEncodingOptions()
at line 202

testItAcceptsJsonAsString()

public testItAcceptsJsonAsString()
at line 211

testSetCallbackInvalidIdentifier()

public testSetCallbackInvalidIdentifier()
at line 220

testSetContent()

public testSetContent()
at line 229

testSetContentJsonSerializeError()

public testSetContentJsonSerializeError()
at line 240

testSetComplexCallback()

public testSetComplexCallback()

Source code

<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\HttpFoundation\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\JsonResponse;

class JsonResponseTest extends TestCase
{
    public function testConstructorEmptyCreatesJsonObject()
    {
        $response = new JsonResponse();
        $this->assertSame('{}', $response->getContent());
    }

    public function testConstructorWithArrayCreatesJsonArray()
    {
        $response = new JsonResponse(array(0, 1, 2, 3));
        $this->assertSame('[0,1,2,3]', $response->getContent());
    }

    public function testConstructorWithAssocArrayCreatesJsonObject()
    {
        $response = new JsonResponse(array('foo' => 'bar'));
        $this->assertSame('{"foo":"bar"}', $response->getContent());
    }

    public function testConstructorWithSimpleTypes()
    {
        $response = new JsonResponse('foo');
        $this->assertSame('"foo"', $response->getContent());

        $response = new JsonResponse(0);
        $this->assertSame('0', $response->getContent());

        $response = new JsonResponse(0.1);
        $this->assertSame('0.1', $response->getContent());

        $response = new JsonResponse(true);
        $this->assertSame('true', $response->getContent());
    }

    public function testConstructorWithCustomStatus()
    {
        $response = new JsonResponse(array(), 202);
        $this->assertSame(202, $response->getStatusCode());
    }

    public function testConstructorAddsContentTypeHeader()
    {
        $response = new JsonResponse();
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
    }

    public function testConstructorWithCustomHeaders()
    {
        $response = new JsonResponse(array(), 200, array('ETag' => 'foo'));
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
        $this->assertSame('foo', $response->headers->get('ETag'));
    }

    public function testConstructorWithCustomContentType()
    {
        $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');

        $response = new JsonResponse(array(), 200, $headers);
        $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
    }

    public function testSetJson()
    {
        $response = new JsonResponse('1', 200, array(), true);
        $this->assertEquals('1', $response->getContent());

        $response = new JsonResponse('[1]', 200, array(), true);
        $this->assertEquals('[1]', $response->getContent());

        $response = new JsonResponse(null, 200, array());
        $response->setJson('true');
        $this->assertEquals('true', $response->getContent());
    }

    public function testCreate()
    {
        $response = JsonResponse::create(array('foo' => 'bar'), 204);

        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
        $this->assertEquals('{"foo":"bar"}', $response->getContent());
        $this->assertEquals(204, $response->getStatusCode());
    }

    public function testStaticCreateEmptyJsonObject()
    {
        $response = JsonResponse::create();
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
        $this->assertSame('{}', $response->getContent());
    }

    public function testStaticCreateJsonArray()
    {
        $response = JsonResponse::create(array(0, 1, 2, 3));
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
        $this->assertSame('[0,1,2,3]', $response->getContent());
    }

    public function testStaticCreateJsonObject()
    {
        $response = JsonResponse::create(array('foo' => 'bar'));
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
        $this->assertSame('{"foo":"bar"}', $response->getContent());
    }

    public function testStaticCreateWithSimpleTypes()
    {
        $response = JsonResponse::create('foo');
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
        $this->assertSame('"foo"', $response->getContent());

        $response = JsonResponse::create(0);
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
        $this->assertSame('0', $response->getContent());

        $response = JsonResponse::create(0.1);
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
        $this->assertSame('0.1', $response->getContent());

        $response = JsonResponse::create(true);
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
        $this->assertSame('true', $response->getContent());
    }

    public function testStaticCreateWithCustomStatus()
    {
        $response = JsonResponse::create(array(), 202);
        $this->assertSame(202, $response->getStatusCode());
    }

    public function testStaticCreateAddsContentTypeHeader()
    {
        $response = JsonResponse::create();
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
    }

    public function testStaticCreateWithCustomHeaders()
    {
        $response = JsonResponse::create(array(), 200, array('ETag' => 'foo'));
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
        $this->assertSame('foo', $response->headers->get('ETag'));
    }

    public function testStaticCreateWithCustomContentType()
    {
        $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');

        $response = JsonResponse::create(array(), 200, $headers);
        $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
    }

    public function testSetCallback()
    {
        $response = JsonResponse::create(array('foo' => 'bar'))->setCallback('callback');

        $this->assertEquals('/**/callback({"foo":"bar"});', $response->getContent());
        $this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
    }

    public function testJsonEncodeFlags()
    {
        $response = new JsonResponse('<>\'&"');

        $this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
    }

    public function testGetEncodingOptions()
    {
        $response = new JsonResponse();

        $this->assertEquals(JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT, $response->getEncodingOptions());
    }

    public function testSetEncodingOptions()
    {
        $response = new JsonResponse();
        $response->setData(array(array(1, 2, 3)));

        $this->assertEquals('[[1,2,3]]', $response->getContent());

        $response->setEncodingOptions(JSON_FORCE_OBJECT);

        $this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
    }

    public function testItAcceptsJsonAsString()
    {
        $response = JsonResponse::fromJsonString('{"foo":"bar"}');
        $this->assertSame('{"foo":"bar"}', $response->getContent());
    }

    /**
     * @expectedException \InvalidArgumentException
     */
    public function testSetCallbackInvalidIdentifier()
    {
        $response = new JsonResponse('foo');
        $response->setCallback('+invalid');
    }

    /**
     * @expectedException \InvalidArgumentException
     */
    public function testSetContent()
    {
        JsonResponse::create("\xB1\x31");
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage This error is expected
     */
    public function testSetContentJsonSerializeError()
    {
        if (!interface_exists('JsonSerializable', false)) {
            $this->markTestSkipped('JsonSerializable is required.');
        }

        $serializable = new JsonSerializableObject();

        JsonResponse::create($serializable);
    }

    public function testSetComplexCallback()
    {
        $response = JsonResponse::create(array('foo' => 'bar'));
        $response->setCallback('ಠ_ಠ["foo"].bar[0]');

        $this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
    }
}

if (interface_exists('JsonSerializable', false)) {
    class JsonSerializableObject implements \JsonSerializable
    {
        public function jsonSerialize()
        {
            throw new \Exception('This error is expected');
        }
    }
}