Skip to content

Commit 4f76fd9

Browse files
author
Rafael Marinho
committed
tests(cha-650): add unit tests
1 parent 69fa864 commit 4f76fd9

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
declare(strict_types=0);
4+
5+
namespace GetStream\Integration;
6+
7+
use GetStream\StreamChat\Client;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class SharedLocationsTest extends TestCase
11+
{
12+
/**
13+
* @var Client
14+
*/
15+
protected $client;
16+
17+
/**
18+
* @var array
19+
*/
20+
protected $user;
21+
22+
/**
23+
* @var \GetStream\StreamChat\Channel
24+
*/
25+
protected $channel;
26+
27+
/**
28+
* @var string
29+
*/
30+
protected $messageId;
31+
32+
protected function setUp(): void
33+
{
34+
// Set the base URL environment variable if STREAM_HOST is provided
35+
$baseURL = getenv('STREAM_HOST');
36+
if ($baseURL) {
37+
putenv("STREAM_BASE_CHAT_URL={$baseURL}");
38+
}
39+
40+
$this->client = new Client(getenv('STREAM_KEY'), getenv('STREAM_SECRET'));
41+
$this->user = $this->getUser();
42+
$this->channel = $this->getChannel();
43+
$this->channel->updatePartial([
44+
"config_overrides" => ["shared_locations" => true],
45+
]);
46+
47+
// Create a message to use for shared locations
48+
$message = [
49+
'text' => 'This is a test message for shared locations'
50+
];
51+
$response = $this->channel->sendMessage($message, $this->user['id']);
52+
$this->messageId = $response['message']['id'];
53+
}
54+
55+
protected function tearDown(): void
56+
{
57+
try {
58+
$this->channel->delete();
59+
$this->client->deleteUser($this->user['id'], ["user" => "hard", "messages" => "hard"]);
60+
} catch (\Exception $e) {
61+
// We don't care about cleanup errors
62+
}
63+
}
64+
65+
private function getUser(): array
66+
{
67+
$userId = 'shared-locations-test-user-' . uniqid();
68+
$user = [
69+
'id' => $userId,
70+
'name' => 'Shared Locations Test User',
71+
];
72+
$this->client->upsertUser($user);
73+
return $user;
74+
}
75+
76+
public function getChannel(): \GetStream\StreamChat\Channel
77+
{
78+
$channelId = 'shared-locations-test-channel-' . uniqid();
79+
$channel = $this->client->Channel('messaging', $channelId);
80+
$channel->create($this->user['id']);
81+
return $channel;
82+
}
83+
84+
public function testGetUserActiveLiveLocations()
85+
{
86+
$response = $this->client->getUserActiveLiveLocations($this->user['id']);
87+
88+
// The response should be a StreamResponse object
89+
$this->assertNotNull($response);
90+
// Initially, user should have no active live locations
91+
$this->assertArrayHasKey('live_locations', $response);
92+
$this->assertIsArray($response['live_locations']);
93+
}
94+
95+
public function testUpdateUserActiveLiveLocation()
96+
{
97+
$location = [
98+
'latitude' => 40.7128,
99+
'longitude' => -74.0060,
100+
'altitude' => 10.5,
101+
'accuracy' => 5.0,
102+
'speed' => 0.0,
103+
'heading' => 0.0,
104+
'timestamp' => time()
105+
];
106+
107+
$response = $this->client->updateUserActiveLiveLocation($this->user['id'], $this->messageId, $location);
108+
109+
$this->assertNotNull($response);
110+
$this->assertTrue(true); // If we got here, the test passed
111+
}
112+
113+
public function testUpdateUserActiveLiveLocationWithMinimalData()
114+
{
115+
$location = [
116+
'latitude' => 34.0522,
117+
'longitude' => -118.2437
118+
];
119+
120+
$response = $this->client->updateUserActiveLiveLocation($this->user['id'], $this->messageId, $location);
121+
122+
$this->assertNotNull($response);
123+
$this->assertTrue(true); // If we got here, the test passed
124+
}
125+
126+
public function testGetUserActiveLiveLocationsAfterUpdate()
127+
{
128+
// First update a location
129+
$location = [
130+
'latitude' => 51.5074,
131+
'longitude' => -0.1278,
132+
'accuracy' => 10.0
133+
];
134+
$this->client->updateUserActiveLiveLocation($this->user['id'], $this->messageId, $location);
135+
136+
// Then get the active live locations
137+
$response = $this->client->getUserActiveLiveLocations($this->user['id']);
138+
139+
$this->assertNotNull($response);
140+
$this->assertArrayHasKey('live_locations', $response);
141+
$this->assertIsArray($response['live_locations']);
142+
}
143+
144+
public function testUpdateUserActiveLiveLocationMultipleTimes()
145+
{
146+
$location1 = [
147+
'latitude' => 48.8566,
148+
'longitude' => 2.3522,
149+
'accuracy' => 5.0
150+
];
151+
152+
$location2 = [
153+
'latitude' => 48.8566,
154+
'longitude' => 2.3522,
155+
'accuracy' => 3.0,
156+
'speed' => 15.5
157+
];
158+
159+
// Update location twice
160+
$response1 = $this->client->updateUserActiveLiveLocation($this->user['id'], $this->messageId, $location1);
161+
$response2 = $this->client->updateUserActiveLiveLocation($this->user['id'], $this->messageId, $location2);
162+
163+
$this->assertNotNull($response1);
164+
$this->assertNotNull($response2);
165+
$this->assertTrue(true); // If we got here, the test passed
166+
}
167+
}

0 commit comments

Comments
 (0)