about summary refs log tree commit diff stats
path: root/tests/ResponseTest.php
blob: 133b077e8fa3ef3c5cb7195b89238e3e900ce0a1 (plain) (blame)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
use PHPUnit\Framework\TestCase;

final class ResponseTest extends TestCase
{
	private function createResponse(): Response
	{
		return new Response();
	}

	// ── Default state ────────────────────────────────────────────────────────

	public function test_default_status_is_200(): void
	{
		$response = $this->createResponse();
		$clone = $response->withStatus(200);
		$this->assertSame(200, (function () { return $this->status; })->call($clone));
	}

	public function test_default_body_is_empty(): void
	{
		$response = $this->createResponse();
		$this->assertEquals('', $response->getBody());
	}

	// ── withStatus() ─────────────────────────────────────────────────────────

	public function test_with_status_returns_clone(): void
	{
		$response = $this->createResponse();
		$newResponse = $response->withStatus(404);
		$this->assertNotSame($response, $newResponse);
	}

	public function test_with_status_sets_code(): void
	{
		$response = $this->createResponse();
		$newResponse = $response->withStatus(404);
		$ref = new ReflectionClass($newResponse);
		$prop = $ref->getProperty('status');
		$this->assertEquals(404, $prop->getValue($newResponse));
	}

	public function test_with_status_does_not_mutate_original(): void
	{
		$response = $this->createResponse();
		$response->withStatus(404);
		$ref = new ReflectionClass($response);
		$prop = $ref->getProperty('status');
		$this->assertEquals(200, $prop->getValue($response));
	}

	public function test_with_status_various_codes(): void
	{
		foreach ([200, 201, 301, 400, 401, 403, 404, 500, 503] as $code) {
			$newResponse = $this->createResponse()->withStatus($code);
			$ref = new ReflectionClass($newResponse);
			$prop = $ref->getProperty('status');
			$this->assertEquals($code, $prop->getValue($newResponse));
		}
	}

	// ── withHeader() ─────────────────────────────────────────────────────────

	public function test_with_header_returns_clone(): void
	{
		$response = $this->createResponse();
		$newResponse = $response->withHeader('Content-Type', 'text/html');
		$this->assertNotSame($response, $newResponse);
	}

	public function test_with_header_sets_value(): void
	{
		$response = $this->createResponse();
		$newResponse = $response->withHeader('Content-Type', 'application/json');
		$ref = new ReflectionClass($newResponse);
		$prop = $ref->getProperty('headers');
		$this->assertEquals(['Content-Type' => 'application/json'], $prop->getValue($newResponse));
	}

	public function test_with_header_does_not_mutate_original(): void
	{
		$response = $this->createResponse();
		$response->withHeader('X-Foo', 'bar');
		$ref = new ReflectionClass($response);
		$prop = $ref->getProperty('headers');
		$this->assertEquals([], $prop->getValue($response));
	}

	public function test_with_header_overwrites_existing(): void
	{
		$response = $this->createResponse();
		$r1 = $response->withHeader('Content-Type', 'text/html');
		$r2 = $r1->withHeader('Content-Type', 'application/json');
		$ref = new ReflectionClass($r2);
		$prop = $ref->getProperty('headers');
		$this->assertEquals(['Content-Type' => 'application/json'], $prop->getValue($r2));
	}

	public function test_with_header_multiple_headers(): void
	{
		$response = $this->createResponse();
		$newResponse = $response
			->withHeader('Content-Type', 'text/html')
			->withHeader('X-Custom', 'value');
		$ref = new ReflectionClass($newResponse);
		$prop = $ref->getProperty('headers');
		$headers = $prop->getValue($newResponse);
		$this->assertEquals('text/html', $headers['Content-Type']);
		$this->assertEquals('value', $headers['X-Custom']);
	}

	// ── withBody() ───────────────────────────────────────────────────────────

	public function test_with_body_returns_clone(): void
	{
		$response = $this->createResponse();
		$newResponse = $response->withBody('hello');
		$this->assertNotSame($response, $newResponse);
	}

	public function test_with_body_sets_content(): void
	{
		$response = $this->createResponse();
		$newResponse = $response->withBody('hello world');
		$this->assertEquals('hello world', $newResponse->getBody());
	}

	public function test_with_body_does_not_mutate_original(): void
	{
		$response = $this->createResponse();
		$response->withBody('changed');
		$this->assertEquals('', $response->getBody());
	}

	public function test_body_json_content(): void
	{
		$json = json_encode(['status' => 'ok']);
		$response = $this->createResponse()->withBody($json);
		$this->assertEquals($json, $response->getBody());
	}

	// ── send() ───────────────────────────────────────────────────────────────

	public function test_send_does_not_throw(): void
	{
		$response = $this->createResponse()->withBody('test');
		// send() calls header() and http_response_code() which throw in CLI
		// PHPUnit suppresses these in some versions, but we catch them
		$this->expectNotToPerformAssertions();
		$response->send();
	}
}