-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamResponseTest.php
More file actions
101 lines (86 loc) · 2.92 KB
/
StreamResponseTest.php
File metadata and controls
101 lines (86 loc) · 2.92 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
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
<?php
declare(strict_types=1);
namespace GetStream\Tests;
use GetStream\StreamResponse;
use PHPUnit\Framework\TestCase;
class StreamResponseTest extends TestCase
{
/**
* @test
*/
public function streamResponseConstruction(): void
{
// Arrange
$statusCode = 200;
$headers = ['content-type' => 'application/json', 'x-custom' => 'test'];
$data = ['id' => 123, 'message' => 'success'];
$rawBody = '{"id":123,"message":"success"}';
// Act
$response = new StreamResponse($statusCode, $headers, $data, $rawBody);
// Assert
self::assertSame($statusCode, $response->getStatusCode());
self::assertSame($headers, $response->getHeaders());
self::assertSame($data, $response->getData());
self::assertSame($rawBody, $response->getRawBody());
}
/**
* @test
*/
public function getHeader(): void
{
// Arrange
$headers = ['content-type' => 'application/json', 'x-custom' => 'test'];
$response = new StreamResponse(200, $headers, []);
// Act & Assert
self::assertSame('application/json', $response->getHeader('content-type'));
self::assertSame('test', $response->getHeader('x-custom'));
self::assertNull($response->getHeader('non-existent'));
}
/**
* @test
*/
public function isSuccessful(): void
{
// Test successful responses
self::assertTrue((new StreamResponse(200, [], []))->isSuccessful());
self::assertTrue((new StreamResponse(201, [], []))->isSuccessful());
self::assertTrue((new StreamResponse(299, [], []))->isSuccessful());
// Test non-successful responses
self::assertFalse((new StreamResponse(199, [], []))->isSuccessful());
self::assertFalse((new StreamResponse(300, [], []))->isSuccessful());
self::assertFalse((new StreamResponse(400, [], []))->isSuccessful());
self::assertFalse((new StreamResponse(404, [], []))->isSuccessful());
self::assertFalse((new StreamResponse(500, [], []))->isSuccessful());
}
/**
* @test
*/
public function toArray(): void
{
// Arrange
$statusCode = 201;
$headers = ['content-type' => 'application/json'];
$data = ['created' => true];
$response = new StreamResponse($statusCode, $headers, $data);
// Act
$array = $response->toArray();
// Assert
$expected = [
'status_code' => 201,
'headers' => ['content-type' => 'application/json'],
'data' => ['created' => true],
];
self::assertSame($expected, $array);
}
/**
* @test
*/
public function withNullRawBody(): void
{
// Arrange & Act
$response = new StreamResponse(204, [], null);
// Assert
self::assertNull($response->getRawBody());
self::assertNull($response->getData());
}
}