|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Tests\Component\HttpFoundation; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\Response; |
| 16 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 17 | + |
| 18 | +/** |
| 19 | + * @covers Symfony\Component\HttpFoundation\JsonResponse::__construct |
| 20 | + */ |
| 21 | +class JsonResponseTest extends \PHPUnit_Framework_TestCase |
| 22 | +{ |
| 23 | + public function testConstructorEmptyCreatesJsonObject() |
| 24 | + { |
| 25 | + $response = new JsonResponse(); |
| 26 | + $this->assertSame('{}', $response->getContent()); |
| 27 | + } |
| 28 | + |
| 29 | + public function testConstructorWithArrayCreatesJsonArray() |
| 30 | + { |
| 31 | + $response = new JsonResponse(array(0, 1, 2, 3)); |
| 32 | + $this->assertSame('[0,1,2,3]', $response->getContent()); |
| 33 | + } |
| 34 | + |
| 35 | + public function testConstructorWithAssocArrayCreatesJsonObject() |
| 36 | + { |
| 37 | + $response = new JsonResponse(array('foo' => 'bar')); |
| 38 | + $this->assertSame('{"foo":"bar"}', $response->getContent()); |
| 39 | + } |
| 40 | + |
| 41 | + public function testConstructorWithSimpleTypes() |
| 42 | + { |
| 43 | + $response = new JsonResponse('foo'); |
| 44 | + $this->assertSame('"foo"', $response->getContent()); |
| 45 | + |
| 46 | + $response = new JsonResponse(0); |
| 47 | + $this->assertSame('0', $response->getContent()); |
| 48 | + |
| 49 | + $response = new JsonResponse(0.1); |
| 50 | + $this->assertSame('0.1', $response->getContent()); |
| 51 | + |
| 52 | + $response = new JsonResponse(true); |
| 53 | + $this->assertSame('true', $response->getContent()); |
| 54 | + } |
| 55 | + |
| 56 | + public function testConstructorWithCustomStatus() |
| 57 | + { |
| 58 | + $response = new JsonResponse(array(), 202); |
| 59 | + $this->assertSame(202, $response->getStatusCode()); |
| 60 | + } |
| 61 | + |
| 62 | + public function testConstructorAddsContentTypeHeader() |
| 63 | + { |
| 64 | + $response = new JsonResponse(); |
| 65 | + $this->assertSame('application/json', $response->headers->get('Content-Type')); |
| 66 | + } |
| 67 | + |
| 68 | + public function testConstructorWithCustomHeaders() |
| 69 | + { |
| 70 | + $response = new JsonResponse(array(), 200, array('ETag' => 'foo')); |
| 71 | + $this->assertSame('application/json', $response->headers->get('Content-Type')); |
| 72 | + $this->assertSame('foo', $response->headers->get('ETag')); |
| 73 | + } |
| 74 | + |
| 75 | + public function testConstructorWithCustomContentType() |
| 76 | + { |
| 77 | + $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json'); |
| 78 | + |
| 79 | + $response = new JsonResponse(array(), 200, $headers); |
| 80 | + $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type')); |
| 81 | + } |
| 82 | +} |
0 commit comments