-
Notifications
You must be signed in to change notification settings - Fork 11.3k
/
Copy pathHttpJsonResponseTest.php
135 lines (112 loc) · 3.84 KB
/
HttpJsonResponseTest.php
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
<?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\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use stdClass;
class HttpJsonResponseTest extends TestCase
{
#[DataProvider('setAndRetrieveDataProvider')]
public function testSetAndRetrieveData($data)
{
$response = new JsonResponse($data);
$this->assertInstanceOf(stdClass::class, $response->getData());
$this->assertSame('bar', $response->getData()->foo);
}
public static function setAndRetrieveDataProvider()
{
return [
'Jsonable data' => [new JsonResponseTestJsonableObject],
'JsonSerializable data' => [new JsonResponseTestJsonSerializeObject],
'Arrayable data' => [new JsonResponseTestArrayableObject],
'Array data' => [['foo' => 'bar']],
'stdClass data' => [(object) ['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]);
}
#[DataProvider('jsonErrorDataProvider')]
public function testGracefullyHandledSomeJsonErrorsWithPartialOutputOnError($data)
{
new JsonResponse(['data' => $data], 200, [], JSON_PARTIAL_OUTPUT_ON_ERROR);
}
public static 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],
];
}
public function testFromJsonString()
{
$json_string = '{"foo":"bar"}';
$response = JsonResponse::fromJsonString($json_string);
$this->assertSame('bar', $response->getData()->foo);
}
}
class JsonResponseTestJsonableObject implements Jsonable
{
public function toJson($options = 0)
{
return '{"foo":"bar"}';
}
}
class JsonResponseTestJsonSerializeObject implements JsonSerializable
{
public function jsonSerialize(): array
{
return ['foo' => 'bar'];
}
}
class JsonResponseTestArrayableObject implements Arrayable
{
public function toArray()
{
return ['foo' => 'bar'];
}
}