-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseTest.php
More file actions
73 lines (57 loc) · 2.5 KB
/
Copy pathResponseTest.php
File metadata and controls
73 lines (57 loc) · 2.5 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
<?php
declare(strict_types=1);
use Infra\Http\Response;
describe('Constructor', function () {
it('should create a response with default values', function () {
$response = new Response;
expect($response->getStatus())->toBe(200)
->and($response->getHeaders())->toBe([])
->and($response->getBody())->toBe('');
});
it('should create a response with custom values', function () {
$response = new Response(
status: 404,
body: 'Not Found',
headers: ['X-Test' => 'true']
);
expect($response->getStatus())->toBe(404)
->and($response->getHeaders())->toBe(['X-Test' => 'true'])
->and($response->getBody())->toBe('Not Found');
});
});
describe('JSON Factory', function () {
it('should create a JSON response with a 200 status by default', function () {
$data = ['user' => 'John Doe', 'id' => 123];
$response = Response::json($data);
expect($response->getStatus())->toBe(200)
->and($response->getHeaders())->toBe(['Content-Type' => 'application/json'])
->and($response->getBody())->toBe(json_encode($data));
});
it('should create a JSON response with a custom status', function () {
$data = ['error' => 'Invalid input'];
$response = Response::json($data, 422);
expect($response->getStatus())->toBe(422)
->and($response->getHeaders())->toBe(['Content-Type' => 'application/json'])
->and($response->getBody())->toBe(json_encode($data));
});
it('should handle an empty array for a JSON response', function () {
$response = Response::json([]);
expect($response->getBody())->toBe('[]');
});
});
describe('HTML Factory', function () {
it('should create an HTML response with a 200 status by default', function () {
$html = '<h1>Hello, World!</h1>';
$response = Response::html($html);
expect($response->getStatus())->toBe(200)
->and($response->getHeaders())->toBe(['Content-Type' => 'text/html; charset=UTF-8'])
->and($response->getBody())->toBe($html);
});
it('should create an HTML response with a custom status', function () {
$html = '<h1>Unauthorized</h1>';
$response = Response::html($html, 401);
expect($response->getStatus())->toBe(401)
->and($response->getHeaders())->toBe(['Content-Type' => 'text/html; charset=UTF-8'])
->and($response->getBody())->toBe($html);
});
});