3
3
namespace Tests ;
4
4
5
5
use Exception ;
6
- use PHPUnit \Framework \TestCase ;
7
6
use Tests \Exceptions \BarException ;
8
7
use Tests \Exceptions \FooException ;
9
8
10
- class ResponseUseDataTest extends TestCase
9
+ class ResponseUseDataTest extends BaseTestCase
11
10
{
12
11
public function testEmpty ()
13
12
{
14
- $ this ->assertJson (api_response (null )->getContent ());
15
- $ this ->assertJson (api_response (null , 300 )->getContent ());
16
- $ this ->assertJson (api_response (null , 400 )->getContent ());
17
- $ this ->assertJson (api_response (null , 500 )->getContent ());
18
-
19
- $ this ->assertJson (api_response ('' )->getContent ());
20
- $ this ->assertJson (api_response ('' , 300 )->getContent ());
21
- $ this ->assertJson (api_response ('' , 400 )->getContent ());
22
- $ this ->assertJson (api_response ('' , 500 )->getContent ());
23
-
24
- $ this ->assertEquals (json_encode (['data ' => null ]), api_response (null )->getContent ());
25
- $ this ->assertEquals (json_encode (['data ' => null ]), api_response (null , 300 )->getContent ());
26
- $ this ->assertEquals (json_encode (['error ' => ['type ' => Exception::class, 'data ' => null ]]), api_response (null , 400 )->getContent ());
27
- $ this ->assertEquals (json_encode (['error ' => ['type ' => Exception::class, 'data ' => null ]]), api_response (null , 500 )->getContent ());
28
-
29
- $ this ->assertEquals (json_encode (['data ' => null ]), api_response ('' )->getContent ());
30
- $ this ->assertEquals (json_encode (['data ' => null ]), api_response ('' , 300 )->getContent ());
31
- $ this ->assertEquals (json_encode (['error ' => ['type ' => Exception::class, 'data ' => null ]]), api_response ('' , 400 )->getContent ());
32
- $ this ->assertEquals (json_encode (['error ' => ['type ' => Exception::class, 'data ' => null ]]), api_response ('' , 500 )->getContent ());
13
+ $ this ->assertJson ($ this -> response (null )->getContent ());
14
+ $ this ->assertJson ($ this -> response (null , 300 )->getContent ());
15
+ $ this ->assertJson ($ this -> response (null , 400 )->getContent ());
16
+ $ this ->assertJson ($ this -> response (null , 500 )->getContent ());
17
+
18
+ $ this ->assertJson ($ this -> response ('' )->getContent ());
19
+ $ this ->assertJson ($ this -> response ('' , 300 )->getContent ());
20
+ $ this ->assertJson ($ this -> response ('' , 400 )->getContent ());
21
+ $ this ->assertJson ($ this -> response ('' , 500 )->getContent ());
22
+
23
+ $ this ->assertEquals (json_encode (['data ' => null ]), $ this -> response (null )->getContent ());
24
+ $ this ->assertEquals (json_encode (['data ' => null ]), $ this -> response (null , 300 )->getContent ());
25
+ $ this ->assertEquals (json_encode (['error ' => ['type ' => Exception::class, 'data ' => null ]]), $ this -> response (null , 400 )->getContent ());
26
+ $ this ->assertEquals (json_encode (['error ' => ['type ' => Exception::class, 'data ' => null ]]), $ this -> response (null , 500 )->getContent ());
27
+
28
+ $ this ->assertEquals (json_encode (['data ' => null ]), $ this -> response ('' )->getContent ());
29
+ $ this ->assertEquals (json_encode (['data ' => null ]), $ this -> response ('' , 300 )->getContent ());
30
+ $ this ->assertEquals (json_encode (['error ' => ['type ' => Exception::class, 'data ' => null ]]), $ this -> response ('' , 400 )->getContent ());
31
+ $ this ->assertEquals (json_encode (['error ' => ['type ' => Exception::class, 'data ' => null ]]), $ this -> response ('' , 500 )->getContent ());
33
32
}
34
33
35
34
public function testData ()
36
35
{
37
- $ this ->assertJson (api_response ('ok ' )->getContent ());
38
- $ this ->assertJson (api_response ('fail ' , 400 )->getContent ());
36
+ $ this ->assertJson ($ this -> response ('ok ' )->getContent ());
37
+ $ this ->assertJson ($ this -> response ('fail ' , 400 )->getContent ());
39
38
40
- $ this ->assertEquals (json_encode (['data ' => 'ok ' ]), api_response ('ok ' )->getContent ());
39
+ $ this ->assertEquals (json_encode (['data ' => 'ok ' ]), $ this -> response ('ok ' )->getContent ());
41
40
}
42
41
43
42
public function testStructure ()
44
43
{
45
- $ this ->assertJsonStringEqualsJsonString (json_encode (['data ' => 'ok ' ]), api_response ('ok ' )->getContent ());
46
- $ this ->assertJsonStringEqualsJsonString (json_encode (['error ' => ['type ' => Exception::class, 'data ' => 'fail ' ]]), api_response ('fail ' , 400 )->getContent ());
44
+ $ this ->assertJsonStringEqualsJsonString (json_encode (['data ' => 'ok ' ]), $ this -> response ('ok ' )->getContent ());
45
+ $ this ->assertJsonStringEqualsJsonString (json_encode (['error ' => ['type ' => Exception::class, 'data ' => 'fail ' ]]), $ this -> response ('fail ' , 400 )->getContent ());
47
46
48
- $ this ->assertJsonStringNotEqualsJsonString (json_encode (['data ' => 'ok ' ]), api_response ('fail ' , 400 )->getContent ());
47
+ $ this ->assertJsonStringNotEqualsJsonString (json_encode (['data ' => 'ok ' ]), $ this -> response ('fail ' , 400 )->getContent ());
49
48
}
50
49
51
50
public function testDataWith ()
52
51
{
53
- $ this ->assertJson (api_response ('ok ' , 200 , ['foo ' => 'bar ' ])->getContent ());
54
- $ this ->assertJson (api_response ('fail ' , 400 , ['foo ' => 'bar ' ])->getContent ());
52
+ $ this ->assertJson ($ this -> response ('ok ' , 200 , ['foo ' => 'bar ' ])->getContent ());
53
+ $ this ->assertJson ($ this -> response ('fail ' , 400 , ['foo ' => 'bar ' ])->getContent ());
55
54
56
- $ this ->assertEquals (json_encode (['data ' => 'ok ' , 'foo ' => 'bar ' ]), api_response ('ok ' , 200 , ['foo ' => 'bar ' ])->getContent ());
55
+ $ this ->assertEquals (json_encode (['data ' => 'ok ' , 'foo ' => 'bar ' ]), $ this -> response ('ok ' , 200 , ['foo ' => 'bar ' ])->getContent ());
57
56
58
57
$ this ->assertEquals (
59
58
json_encode ([
@@ -63,64 +62,64 @@ public function testDataWith()
63
62
],
64
63
'foo ' => 'bar ' ,
65
64
]),
66
- api_response ('ok ' , 400 , ['foo ' => 'bar ' ])->getContent ()
65
+ $ this -> response ('ok ' , 400 , ['foo ' => 'bar ' ])->getContent ()
67
66
);
68
67
69
68
$ this ->assertEquals (
70
69
json_encode (['data ' => [], 'foo ' => 'bar ' , 'baz ' => 'baq ' ]),
71
- api_response ([], 200 , ['foo ' => 'bar ' , 'baz ' => 'baq ' ])->getContent ()
70
+ $ this -> response ([], 200 , ['foo ' => 'bar ' , 'baz ' => 'baq ' ])->getContent ()
72
71
);
73
72
74
73
$ this ->assertEquals (
75
74
json_encode (['data ' => ['foo ' => 'foo ' , 'bar ' => 'bar ' ], 'foo ' => 'bar ' , 'baz ' => 'baq ' ]),
76
- api_response ([], 200 , ['data ' => ['foo ' => 'foo ' , 'bar ' => 'bar ' ], 'foo ' => 'bar ' , 'baz ' => 'baq ' ])->getContent ()
75
+ $ this -> response ([], 200 , ['data ' => ['foo ' => 'foo ' , 'bar ' => 'bar ' ], 'foo ' => 'bar ' , 'baz ' => 'baq ' ])->getContent ()
77
76
);
78
77
79
78
$ this ->assertEquals (
80
79
json_encode (['error ' => ['type ' => Exception::class, 'data ' => []], 'foo ' => 'bar ' , 'baz ' => 'baq ' ]),
81
- api_response ([], 400 , ['foo ' => 'bar ' , 'baz ' => 'baq ' ])->getContent ()
80
+ $ this -> response ([], 400 , ['foo ' => 'bar ' , 'baz ' => 'baq ' ])->getContent ()
82
81
);
83
82
84
83
$ this ->assertEquals (
85
84
json_encode (['error ' => ['type ' => Exception::class, 'data ' => []], 'data ' => ['foo ' => 'foo ' , 'bar ' => 'bar ' ], 'foo ' => 'bar ' , 'baz ' => 'baq ' ]),
86
- api_response ([], 400 , ['data ' => ['foo ' => 'foo ' , 'bar ' => 'bar ' ], 'foo ' => 'bar ' , 'baz ' => 'baq ' ])->getContent ()
85
+ $ this -> response ([], 400 , ['data ' => ['foo ' => 'foo ' , 'bar ' => 'bar ' ], 'foo ' => 'bar ' , 'baz ' => 'baq ' ])->getContent ()
87
86
);
88
87
}
89
88
90
89
public function testNumber ()
91
90
{
92
- $ this ->assertEquals (json_encode (['data ' => 304 ]), api_response (304 )->getContent ());
93
- $ this ->assertEquals (json_encode (['error ' => ['type ' => Exception::class, 'data ' => 304 ]]), api_response (304 , 400 )->getContent ());
91
+ $ this ->assertEquals (json_encode (['data ' => 304 ]), $ this -> response (304 )->getContent ());
92
+ $ this ->assertEquals (json_encode (['error ' => ['type ' => Exception::class, 'data ' => 304 ]]), $ this -> response (304 , 400 )->getContent ());
94
93
}
95
94
96
95
public function testStatusCode ()
97
96
{
98
- $ this ->assertEquals (200 , api_response ('ok ' )->getStatusCode ());
99
- $ this ->assertEquals (301 , api_response ('ok ' , 301 )->getStatusCode ());
100
- $ this ->assertEquals (401 , api_response ('ok ' , 401 )->getStatusCode ());
101
- $ this ->assertEquals (500 , api_response ('ok ' , 500 )->getStatusCode ());
97
+ $ this ->assertEquals (200 , $ this -> response ('ok ' )->getStatusCode ());
98
+ $ this ->assertEquals (301 , $ this -> response ('ok ' , 301 )->getStatusCode ());
99
+ $ this ->assertEquals (401 , $ this -> response ('ok ' , 401 )->getStatusCode ());
100
+ $ this ->assertEquals (500 , $ this -> response ('ok ' , 500 )->getStatusCode ());
102
101
}
103
102
104
103
public function testKeyCollisionTesting ()
105
104
{
106
- $ this ->assertEquals (json_encode (['data ' => 'example ' , 'foo ' => 'bar ' ]), api_response (['data ' => 'example ' , 'foo ' => 'bar ' ])->getContent ());
107
- $ this ->assertEquals (json_encode (['data ' => ['example ' , 'foo ' => 'bar ' ]]), api_response (['data ' => ['example ' , 'foo ' => 'bar ' ]])->getContent ());
105
+ $ this ->assertEquals (json_encode (['data ' => 'example ' , 'foo ' => 'bar ' ]), $ this -> response (['data ' => 'example ' , 'foo ' => 'bar ' ])->getContent ());
106
+ $ this ->assertEquals (json_encode (['data ' => ['example ' , 'foo ' => 'bar ' ]]), $ this -> response (['data ' => ['example ' , 'foo ' => 'bar ' ]])->getContent ());
108
107
109
108
$ this ->assertEquals (
110
109
json_encode (['error ' => ['type ' => Exception::class, 'data ' => 'example ' ], 'foo ' => 'bar ' ]),
111
- api_response (['data ' => 'example ' , 'foo ' => 'bar ' ], 400 )->getContent ()
110
+ $ this -> response (['data ' => 'example ' , 'foo ' => 'bar ' ], 400 )->getContent ()
112
111
);
113
112
114
113
$ this ->assertEquals (
115
114
json_encode (['error ' => ['type ' => Exception::class, 'data ' => ['foo ' => 'bar ' , 'baz ' => 'baq ' ]]]),
116
- api_response (['data ' => ['foo ' => 'bar ' , 'baz ' => 'baq ' ]], 400 )->getContent ()
115
+ $ this -> response (['data ' => ['foo ' => 'bar ' , 'baz ' => 'baq ' ]], 400 )->getContent ()
117
116
);
118
117
}
119
118
120
119
public function testWithExceptionHandler ()
121
120
{
122
121
$ e = new FooException ('Foo ' );
123
- $ r = api_response ($ e );
122
+ $ r = $ this -> response ($ e );
124
123
125
124
$ this ->assertEquals (
126
125
json_encode (['error ' => ['type ' => 'FooException ' , 'data ' => 'Foo ' ]]),
@@ -133,7 +132,7 @@ public function testWithExceptionHandler()
133
132
public function testExceptionHandlerWithAdditionalData ()
134
133
{
135
134
$ e = new FooException ('Foo ' );
136
- $ r = api_response ($ e , 200 , ['foo ' => 'Bar ' ]);
135
+ $ r = $ this -> response ($ e , 200 , ['foo ' => 'Bar ' ]);
137
136
138
137
$ this ->assertEquals (
139
138
json_encode (['error ' => ['type ' => 'FooException ' , 'data ' => 'Foo ' ], 'foo ' => 'Bar ' ]),
@@ -146,7 +145,7 @@ public function testExceptionHandlerWithAdditionalData()
146
145
public function testExceptionHandlerWithReplaceStatusCode ()
147
146
{
148
147
$ e = new FooException ('Foo ' );
149
- $ r = api_response ($ e , 408 );
148
+ $ r = $ this -> response ($ e , 408 );
150
149
151
150
$ this ->assertEquals (
152
151
json_encode (['error ' => ['type ' => 'FooException ' , 'data ' => 'Foo ' ]]),
@@ -159,7 +158,7 @@ public function testExceptionHandlerWithReplaceStatusCode()
159
158
public function testExceptionHandlerWithDefaultStatusCode ()
160
159
{
161
160
$ e = new BarException ('Bar ' );
162
- $ r = api_response ($ e );
161
+ $ r = $ this -> response ($ e );
163
162
164
163
$ this ->assertEquals (
165
164
json_encode (['error ' => ['type ' => 'BarException ' , 'data ' => 'Bar ' ]]),
@@ -172,7 +171,7 @@ public function testExceptionHandlerWithDefaultStatusCode()
172
171
public function testExceptionHandlerWithStatusCode ()
173
172
{
174
173
$ e = new BarException ('Bar ' );
175
- $ r = api_response ($ e , 406 );
174
+ $ r = $ this -> response ($ e , 406 );
176
175
177
176
$ this ->assertEquals (
178
177
json_encode (['error ' => ['type ' => 'BarException ' , 'data ' => 'Bar ' ]]),
0 commit comments