forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpJsonResponseTest.php
More file actions
138 lines (117 loc) · 3.66 KB
/
HttpJsonResponseTest.php
File metadata and controls
138 lines (117 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace Illuminate\Tests\Http;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Http\JsonResponse;
use InvalidArgumentException;
use JsonSerializable;
use PHPUnit\Framework\TestCase;
use stdClass;
class HttpJsonResponseTest extends TestCase
{
/**
* @dataProvider setAndRetrieveDataProvider
*
* @param mixed $data
*/
public function testSetAndRetrieveData($data): void
{
$response = new JsonResponse($data);
$this->assertInstanceOf(stdClass::class, $response->getData());
$this->assertSame('bar', $response->getData()->foo);
}
public function setAndRetrieveDataProvider(): array
{
return [
'Jsonable data' => [new JsonResponseTestJsonableObject],
'JsonSerializable data' => [new JsonResponseTestJsonSerializeObject],
'Arrayable data' => [new JsonResponseTestArrayableObject],
'Array data' => [['foo' => 'bar']],
];
}
public function testGetOriginalContent()
{
$response = new JsonResponse(new JsonResponseTestArrayableObject);
$this->assertInstanceOf(JsonResponseTestArrayableObject::class, $response->getOriginalContent());
$response = new JsonResponse;
$response->setData(new JsonResponseTestArrayableObject);
$this->assertInstanceOf(JsonResponseTestArrayableObject::class, $response->getOriginalContent());
}
public function testSetAndRetrieveOptions()
{
$response = new JsonResponse(['foo' => 'bar']);
$response->setEncodingOptions(JSON_PRETTY_PRINT);
$this->assertSame(JSON_PRETTY_PRINT, $response->getEncodingOptions());
}
public function testSetAndRetrieveDefaultOptions()
{
$response = new JsonResponse(['foo' => 'bar']);
$this->assertSame(0, $response->getEncodingOptions());
}
public function testSetAndRetrieveStatusCode()
{
$response = new JsonResponse(['foo' => 'bar'], 404);
$this->assertSame(404, $response->getStatusCode());
$response = new JsonResponse(['foo' => 'bar']);
$response->setStatusCode(404);
$this->assertSame(404, $response->getStatusCode());
}
/**
* @dataProvider jsonErrorDataProvider
*/
public function testInvalidArgumentExceptionOnJsonError($data)
{
$this->expectException(InvalidArgumentException::class);
new JsonResponse(['data' => $data]);
}
/**
* @param mixed $data
*
* @dataProvider jsonErrorDataProvider
*/
public function testGracefullyHandledSomeJsonErrorsWithPartialOutputOnError($data)
{
new JsonResponse(['data' => $data], 200, [], JSON_PARTIAL_OUTPUT_ON_ERROR);
}
/**
* @return array
*/
public function jsonErrorDataProvider()
{
// Resources can't be encoded
$resource = tmpfile();
// Recursion can't be encoded
$recursiveObject = new stdClass;
$objectB = new stdClass;
$recursiveObject->b = $objectB;
$objectB->a = $recursiveObject;
// NAN or INF can't be encoded
$nan = NAN;
return [
[$resource],
[$recursiveObject],
[$nan],
];
}
}
class JsonResponseTestJsonableObject implements Jsonable
{
public function toJson($options = 0)
{
return '{"foo":"bar"}';
}
}
class JsonResponseTestJsonSerializeObject implements JsonSerializable
{
public function jsonSerialize()
{
return ['foo' => 'bar'];
}
}
class JsonResponseTestArrayableObject implements Arrayable
{
public function toArray()
{
return ['foo' => 'bar'];
}
}