-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientTest.php
More file actions
150 lines (125 loc) · 4.15 KB
/
ClientTest.php
File metadata and controls
150 lines (125 loc) · 4.15 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
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
<?php
declare(strict_types=1);
namespace GetStream\Tests;
use GetStream\Auth\JWTGenerator;
use GetStream\Client;
use GetStream\ClientBuilder;
use GetStream\Exceptions\StreamException;
use GetStream\Feed;
use GetStream\Http\HttpClientInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class ClientTest extends TestCase
{
private HttpClientInterface|MockObject $mockHttpClient;
protected function setUp(): void
{
$this->mockHttpClient = $this->createMock(HttpClientInterface::class);
}
/**
* @test
*/
public function clientConstruction(): void
{
// Load credentials from environment
$client = ClientBuilder::fromEnv()
->httpClient($this->mockHttpClient)
->build();
// Assert - verify environment credentials are loaded
self::assertNotEmpty($client->getApiKey());
self::assertNotEmpty($client->getApiSecret());
self::assertSame('https://chat.stream-io-api.com', $client->getBaseUrl());
self::assertSame($this->mockHttpClient, $client->getHttpClient());
self::assertInstanceOf(JWTGenerator::class, $client->getJWTGenerator());
}
/**
* @test
*/
public function clientConstructionWithDefaults(): void
{
// Load from environment with defaults
$client = ClientBuilder::fromEnv()->build();
// Assert
self::assertNotEmpty($client->getApiKey());
self::assertNotEmpty($client->getApiSecret());
self::assertSame('https://chat.stream-io-api.com', $client->getBaseUrl());
}
/**
* @test
*/
public function clientConstructionWithEmptyApiKey(): void
{
$this->expectException(StreamException::class);
$this->expectExceptionMessage('API key cannot be empty');
new Client('', 'test-api-secret');
}
/**
* @test
*/
public function clientConstructionWithEmptyApiSecret(): void
{
$this->expectException(StreamException::class);
$this->expectExceptionMessage('API secret cannot be empty');
new Client('test-api-key', '');
}
/**
* @test
*/
public function feedCreation(): void
{
// Arrange
$client = ClientBuilder::fromEnv()
->httpClient($this->mockHttpClient)
->build();
// Act
$feed = $client->feed('user', '123');
// Assert
self::assertInstanceOf(Feed::class, $feed);
self::assertSame('user', $feed->getFeedGroup());
self::assertSame('123', $feed->getFeedId());
}
/**
* @test
*/
public function createUserToken(): void
{
// Arrange
$client = ClientBuilder::fromEnv()
->httpClient($this->mockHttpClient)
->build();
// Act
$token = $client->createUserToken('user-123');
// Assert
self::assertIsString($token);
self::assertNotEmpty($token);
// Verify it's a valid JWT format (3 parts separated by dots)
$parts = explode('.', $token);
self::assertCount(3, $parts);
// Verify the payload contains the user_id
$payload = json_decode(base64_decode($parts[1], true), true);
self::assertSame('user-123', $payload['user_id']);
}
/**
* @test
*/
public function createUserTokenWithClaims(): void
{
// Arrange
$client = ClientBuilder::fromEnv()
->httpClient($this->mockHttpClient)
->build();
$claims = ['role' => 'admin', 'permissions' => ['read', 'write']];
// Act
$token = $client->createUserToken('user-123', $claims, 3600);
// Assert
self::assertIsString($token);
self::assertNotEmpty($token);
// Verify the payload contains the custom claims
$parts = explode('.', $token);
$payload = json_decode(base64_decode($parts[1], true), true);
self::assertSame('user-123', $payload['user_id']);
self::assertSame('admin', $payload['role']);
self::assertSame(['read', 'write'], $payload['permissions']);
self::assertArrayHasKey('exp', $payload); // Should have expiration
}
}