Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 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\Component\HttpFoundation\Tests;
13
 
14
use Symfony\Component\HttpFoundation\JsonResponse;
15
 
16
class JsonResponseTest extends \PHPUnit_Framework_TestCase
17
{
18
    public function testConstructorEmptyCreatesJsonObject()
19
    {
20
        $response = new JsonResponse();
21
        $this->assertSame('{}', $response->getContent());
22
    }
23
 
24
    public function testConstructorWithArrayCreatesJsonArray()
25
    {
26
        $response = new JsonResponse(array(0, 1, 2, 3));
27
        $this->assertSame('[0,1,2,3]', $response->getContent());
28
    }
29
 
30
    public function testConstructorWithAssocArrayCreatesJsonObject()
31
    {
32
        $response = new JsonResponse(array('foo' => 'bar'));
33
        $this->assertSame('{"foo":"bar"}', $response->getContent());
34
    }
35
 
36
    public function testConstructorWithSimpleTypes()
37
    {
38
        $response = new JsonResponse('foo');
39
        $this->assertSame('"foo"', $response->getContent());
40
 
41
        $response = new JsonResponse(0);
42
        $this->assertSame('0', $response->getContent());
43
 
44
        $response = new JsonResponse(0.1);
45
        $this->assertSame('0.1', $response->getContent());
46
 
47
        $response = new JsonResponse(true);
48
        $this->assertSame('true', $response->getContent());
49
    }
50
 
51
    public function testConstructorWithCustomStatus()
52
    {
53
        $response = new JsonResponse(array(), 202);
54
        $this->assertSame(202, $response->getStatusCode());
55
    }
56
 
57
    public function testConstructorAddsContentTypeHeader()
58
    {
59
        $response = new JsonResponse();
60
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
61
    }
62
 
63
    public function testConstructorWithCustomHeaders()
64
    {
65
        $response = new JsonResponse(array(), 200, array('ETag' => 'foo'));
66
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
67
        $this->assertSame('foo', $response->headers->get('ETag'));
68
    }
69
 
70
    public function testConstructorWithCustomContentType()
71
    {
72
        $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
73
 
74
        $response = new JsonResponse(array(), 200, $headers);
75
        $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
76
    }
77
 
78
    public function testSetJson()
79
    {
80
        $response = new JsonResponse('1', 200, array(), true);
81
        $this->assertEquals('1', $response->getContent());
82
 
83
        $response = new JsonResponse('[1]', 200, array(), true);
84
        $this->assertEquals('[1]', $response->getContent());
85
 
86
        $response = new JsonResponse(null, 200, array());
87
        $response->setJson('true');
88
        $this->assertEquals('true', $response->getContent());
89
    }
90
 
91
    public function testCreate()
92
    {
93
        $response = JsonResponse::create(array('foo' => 'bar'), 204);
94
 
95
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
96
        $this->assertEquals('{"foo":"bar"}', $response->getContent());
97
        $this->assertEquals(204, $response->getStatusCode());
98
    }
99
 
100
    public function testStaticCreateEmptyJsonObject()
101
    {
102
        $response = JsonResponse::create();
103
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
104
        $this->assertSame('{}', $response->getContent());
105
    }
106
 
107
    public function testStaticCreateJsonArray()
108
    {
109
        $response = JsonResponse::create(array(0, 1, 2, 3));
110
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
111
        $this->assertSame('[0,1,2,3]', $response->getContent());
112
    }
113
 
114
    public function testStaticCreateJsonObject()
115
    {
116
        $response = JsonResponse::create(array('foo' => 'bar'));
117
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
118
        $this->assertSame('{"foo":"bar"}', $response->getContent());
119
    }
120
 
121
    public function testStaticCreateWithSimpleTypes()
122
    {
123
        $response = JsonResponse::create('foo');
124
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
125
        $this->assertSame('"foo"', $response->getContent());
126
 
127
        $response = JsonResponse::create(0);
128
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
129
        $this->assertSame('0', $response->getContent());
130
 
131
        $response = JsonResponse::create(0.1);
132
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
133
        $this->assertSame('0.1', $response->getContent());
134
 
135
        $response = JsonResponse::create(true);
136
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
137
        $this->assertSame('true', $response->getContent());
138
    }
139
 
140
    public function testStaticCreateWithCustomStatus()
141
    {
142
        $response = JsonResponse::create(array(), 202);
143
        $this->assertSame(202, $response->getStatusCode());
144
    }
145
 
146
    public function testStaticCreateAddsContentTypeHeader()
147
    {
148
        $response = JsonResponse::create();
149
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
150
    }
151
 
152
    public function testStaticCreateWithCustomHeaders()
153
    {
154
        $response = JsonResponse::create(array(), 200, array('ETag' => 'foo'));
155
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
156
        $this->assertSame('foo', $response->headers->get('ETag'));
157
    }
158
 
159
    public function testStaticCreateWithCustomContentType()
160
    {
161
        $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
162
 
163
        $response = JsonResponse::create(array(), 200, $headers);
164
        $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
165
    }
166
 
167
    public function testSetCallback()
168
    {
169
        $response = JsonResponse::create(array('foo' => 'bar'))->setCallback('callback');
170
 
171
        $this->assertEquals('/**/callback({"foo":"bar"});', $response->getContent());
172
        $this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
173
    }
174
 
175
    public function testJsonEncodeFlags()
176
    {
177
        $response = new JsonResponse('<>\'&"');
178
 
179
        $this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
180
    }
181
 
182
    public function testGetEncodingOptions()
183
    {
184
        $response = new JsonResponse();
185
 
186
        $this->assertEquals(JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT, $response->getEncodingOptions());
187
    }
188
 
189
    public function testSetEncodingOptions()
190
    {
191
        $response = new JsonResponse();
192
        $response->setData(array(array(1, 2, 3)));
193
 
194
        $this->assertEquals('[[1,2,3]]', $response->getContent());
195
 
196
        $response->setEncodingOptions(JSON_FORCE_OBJECT);
197
 
198
        $this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
199
    }
200
 
201
    /**
202
     * @expectedException \InvalidArgumentException
203
     */
204
    public function testSetCallbackInvalidIdentifier()
205
    {
206
        $response = new JsonResponse('foo');
207
        $response->setCallback('+invalid');
208
    }
209
 
210
    /**
211
     * @expectedException \InvalidArgumentException
212
     */
213
    public function testSetContent()
214
    {
215
        JsonResponse::create("\xB1\x31");
216
    }
217
 
218
    /**
219
     * @expectedException \Exception
220
     * @expectedExceptionMessage This error is expected
221
     */
222
    public function testSetContentJsonSerializeError()
223
    {
224
        $serializable = new JsonSerializableObject();
225
 
226
        JsonResponse::create($serializable);
227
    }
228
}
229
 
230
if (interface_exists('JsonSerializable')) {
231
    class JsonSerializableObject implements \JsonSerializable
232
    {
233
        public function jsonSerialize()
234
        {
235
            throw new \Exception('This error is expected');
236
        }
237
    }
238
}