Passed
Push — 1.x ( 05110a...ecbb08 )
by Kevin
02:11
created

php$1 ➔ can_override_json_and_ajax_headers()   A

Complexity

Conditions 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 16
rs 9.7333
1
<?php
2
3
namespace Zenstruck\Browser\Tests;
4
5
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Zenstruck\Browser\HttpOptions;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
final class HttpOptionsTest extends TestCase
12
{
13
    /**
14
     * @test
15
     */
16
    public function defaults(): void
17
    {
18
        $options = new HttpOptions();
19
20
        $this->assertEmpty($options->query());
21
        $this->assertEmpty($options->files());
22
        $this->assertEmpty($options->server());
23
        $this->assertNull($options->body());
24
    }
25
26
    /**
27
     * @test
28
     */
29
    public function can_configure_with_constructor_array(): void
30
    {
31
        $options = new HttpOptions([
32
            'headers' => ['header' => 'header value'],
33
            'query' => $expectedQuery = ['param' => 'param value'],
34
            'files' => $expectedFiles = ['file' => 'file value'],
35
            'server' => ['server' => 'server value'],
36
            'body' => $expectedBody = 'body value',
37
            'json' => null,
38
            'ajax' => false,
39
        ]);
40
41
        $this->assertSame($expectedQuery, $options->query());
42
        $this->assertSame($expectedFiles, $options->files());
43
        $this->assertSame(['server' => 'server value', 'HTTP_HEADER' => 'header value'], $options->server());
44
        $this->assertSame($expectedBody, $options->body());
45
    }
46
47
    /**
48
     * @test
49
     */
50
    public function can_configure_via_withers(): void
51
    {
52
        $options = (new HttpOptions())
53
            ->withBody($expectedBody = 'body value')
54
            ->withHeaders(['header1' => 'header1 value'])
55
            ->withHeader('header2', 'header2 value')
56
            ->withFiles($expectedFiles = ['file' => 'file value'])
57
            ->withServer(['server' => 'server value'])
58
            ->withQuery($expectedQuery = ['param' => 'param value'])
59
        ;
60
61
        $this->assertSame($expectedQuery, $options->query());
62
        $this->assertSame($expectedFiles, $options->files());
63
        $this->assertSame($expectedBody, $options->body());
64
        $this->assertSame(
65
            [
66
                'server' => 'server value',
67
                'HTTP_HEADER1' => 'header1 value',
68
                'HTTP_HEADER2' => 'header2 value',
69
            ],
70
            $options->server()
71
        );
72
    }
73
74
    /**
75
     * @test
76
     */
77
    public function can_configure_json_and_ajax_with_constructor_array(): void
78
    {
79
        $options = new HttpOptions([
80
            'headers' => ['header' => 'header value'],
81
            'server' => ['server' => 'server value'],
82
            'body' => 'not used',
83
            'json' => $json = ['json' => 'body'],
84
            'ajax' => true,
85
        ]);
86
87
        $expectedServer = [
88
            'server' => 'server value',
89
            'HTTP_HEADER' => 'header value',
90
            'HTTP_ACCEPT' => 'application/json',
91
            'CONTENT_TYPE' => 'application/json',
92
            'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest',
93
        ];
94
95
        $this->assertSame(\json_encode($json), $options->body());
96
        $this->assertSame($expectedServer, $options->server());
97
    }
98
99
    /**
100
     * @test
101
     */
102
    public function ajax_constructor(): void
103
    {
104
        $options = HttpOptions::ajax();
105
106
        $this->assertNull($options->body());
107
        $this->assertSame(
108
            [
109
                'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest',
110
            ],
111
            $options->server()
112
        );
113
    }
114
115
    /**
116
     * @test
117
     */
118
    public function json_constructor_with_value(): void
119
    {
120
        $options = HttpOptions::json('value');
121
122
        $this->assertSame('"value"', $options->body());
123
        $this->assertSame(
124
            [
125
                'HTTP_ACCEPT' => 'application/json',
126
                'CONTENT_TYPE' => 'application/json',
127
            ],
128
            $options->server()
129
        );
130
    }
131
132
    /**
133
     * @test
134
     */
135
    public function json_constructor_with_no_value(): void
136
    {
137
        $options = HttpOptions::json();
138
139
        $this->assertNull($options->body());
140
        $this->assertSame(
141
            [
142
                'HTTP_ACCEPT' => 'application/json',
143
                'CONTENT_TYPE' => 'application/json',
144
            ],
145
            $options->server()
146
        );
147
    }
148
149
    /**
150
     * @test
151
     */
152
    public function json_ajax_constructor_with_value(): void
153
    {
154
        $options = HttpOptions::jsonAjax('value');
155
156
        $this->assertSame('"value"', $options->body());
157
        $this->assertSame(
158
            [
159
                'HTTP_ACCEPT' => 'application/json',
160
                'CONTENT_TYPE' => 'application/json',
161
                'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest',
162
            ],
163
            $options->server()
164
        );
165
    }
166
167
    /**
168
     * @test
169
     */
170
    public function json_ajax_constructor_with_no_value(): void
171
    {
172
        $options = HttpOptions::jsonAjax();
173
174
        $this->assertNull($options->body());
175
        $this->assertSame(
176
            [
177
                'HTTP_ACCEPT' => 'application/json',
178
                'CONTENT_TYPE' => 'application/json',
179
                'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest',
180
            ],
181
            $options->server()
182
        );
183
    }
184
185
    /**
186
     * @test
187
     */
188
    public function create_with_self(): void
189
    {
190
        $options = new class() extends HttpOptions {};
191
192
        $this->assertSame($options, HttpOptions::create($options));
193
    }
194
195
    /**
196
     * @test
197
     */
198
    public function can_merge_with_array(): void
199
    {
200
        $options = HttpOptions::create([
201
            'headers' => ['header1' => 'header1 value'],
202
            'query' => $expectedQuery = ['param' => 'param value'],
203
            'files' => $expectedFiles = ['file' => 'file value'],
204
            'server' => ['server' => 'server value'],
205
            'body' => null,
206
            'json' => null,
207
            'ajax' => true,
208
        ]);
209
        $options = $options->merge([
210
            'headers' => ['header2' => 'header2 value'],
211
            'json' => $json = ['json' => 'body'],
212
        ]);
213
214
        $this->assertSame($expectedQuery, $options->query());
215
        $this->assertSame($expectedFiles, $options->files());
216
        $this->assertSame(\json_encode($json), $options->body());
217
        $this->assertSame(
218
            [
219
                'server' => 'server value',
220
                'HTTP_HEADER1' => 'header1 value',
221
                'HTTP_HEADER2' => 'header2 value',
222
                'HTTP_ACCEPT' => 'application/json',
223
                'CONTENT_TYPE' => 'application/json',
224
                'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest',
225
            ],
226
            $options->server()
227
        );
228
    }
229
230
    /**
231
     * @test
232
     */
233
    public function can_merge_with_http_options_object(): void
234
    {
235
        $options = HttpOptions::create([
236
            'headers' => ['header1' => 'header1 value'],
237
            'query' => $expectedQuery = ['param' => 'param value'],
238
            'files' => $expectedFiles = ['file' => 'file value'],
239
            'server' => ['server' => 'server value'],
240
            'body' => null,
241
            'json' => null,
242
            'ajax' => true,
243
        ]);
244
        $options = $options->merge(new class(['headers' => ['header2' => 'header2 value']]) extends HttpOptions {});
245
246
        $this->assertSame($expectedQuery, $options->query());
247
        $this->assertSame($expectedFiles, $options->files());
248
        $this->assertNull($options->body());
249
        $this->assertSame(
250
            [
251
                'server' => 'server value',
252
                'HTTP_HEADER1' => 'header1 value',
253
                'HTTP_HEADER2' => 'header2 value',
254
                'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest',
255
            ],
256
            $options->server()
257
        );
258
    }
259
260
    /**
261
     * @test
262
     */
263
    public function can_override_json_and_ajax_headers(): void
264
    {
265
        $options = HttpOptions::jsonAjax()
266
            ->withHeader('Accept', 'application/ld+json')
267
            ->withHeader('Content-Type', 'application/ld+json')
268
            ->withHeader('X-Requested-With', 'something')
269
        ;
270
271
        $this->assertNull($options->body());
272
        $this->assertSame(
273
            [
274
                'HTTP_ACCEPT' => 'application/ld+json',
275
                'CONTENT_TYPE' => 'application/ld+json',
276
                'HTTP_X_REQUESTED_WITH' => 'something',
277
            ],
278
            $options->server()
279
        );
280
    }
281
}
282