|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=0); |
| 4 | + |
| 5 | +namespace GetStream\Integration; |
| 6 | + |
| 7 | +use DateTime; |
| 8 | +use GetStream\StreamChat\Client; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class ReminderTest extends TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var Client |
| 15 | + */ |
| 16 | + protected $client; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var array |
| 20 | + */ |
| 21 | + protected $user; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \GetStream\StreamChat\Channel |
| 25 | + */ |
| 26 | + protected $channel; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + protected $messageId; |
| 32 | + |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + // Set the base URL environment variable if STREAM_HOST is provided |
| 36 | + $baseURL = getenv('STREAM_HOST'); |
| 37 | + if ($baseURL) { |
| 38 | + putenv("STREAM_BASE_CHAT_URL={$baseURL}"); |
| 39 | + } |
| 40 | + |
| 41 | + $this->client = new Client(getenv('STREAM_KEY'), getenv('STREAM_SECRET')); |
| 42 | + $this->user = $this->getUser(); |
| 43 | + $this->channel = $this->getChannel(); |
| 44 | + |
| 45 | + // Create a message to use for reminders |
| 46 | + $message = [ |
| 47 | + 'text' => 'This is a test message for reminders' |
| 48 | + ]; |
| 49 | + $response = $this->channel->sendMessage($message, $this->user['id']); |
| 50 | + $this->messageId = $response['message']['id']; |
| 51 | + } |
| 52 | + |
| 53 | + protected function tearDown(): void |
| 54 | + { |
| 55 | + try { |
| 56 | + $this->channel->delete(); |
| 57 | + $this->client->deleteUser($this->user['id'], ["user" => "hard", "messages" => "hard"]); |
| 58 | + } catch (\Exception $e) { |
| 59 | + // We don't care about cleanup errors |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private function getUser(): array |
| 64 | + { |
| 65 | + $userId = 'reminder-test-user-' . uniqid(); |
| 66 | + $user = [ |
| 67 | + 'id' => $userId, |
| 68 | + 'name' => 'Reminder Test User', |
| 69 | + ]; |
| 70 | + $this->client->upsertUser($user); |
| 71 | + return $user; |
| 72 | + } |
| 73 | + |
| 74 | + public function getChannel(): \GetStream\StreamChat\Channel |
| 75 | + { |
| 76 | + $channelId = 'reminder-test-channel-' . uniqid(); |
| 77 | + $channel = $this->client->Channel('messaging', $channelId); |
| 78 | + $channel->create($this->user['id']); |
| 79 | + return $channel; |
| 80 | + } |
| 81 | + |
| 82 | + public function testCreateReminder() |
| 83 | + { |
| 84 | + $remindAt = new DateTime('+1 day'); |
| 85 | + $response = $this->client->createReminder($this->messageId, $this->user['id'], $remindAt); |
| 86 | + |
| 87 | + $this->assertArrayHasKey('reminder', $response); |
| 88 | + $this->assertEquals($this->messageId, $response['reminder']['message_id']); |
| 89 | + $this->assertEquals($this->user['id'], $response['reminder']['user_id']); |
| 90 | + $this->assertNotEmpty($response['reminder']['remind_at']); |
| 91 | + } |
| 92 | + |
| 93 | + public function testCreateReminderWithoutRemindAt() |
| 94 | + { |
| 95 | + $response = $this->client->createReminder($this->messageId, $this->user['id']); |
| 96 | + |
| 97 | + $this->assertArrayHasKey('reminder', $response); |
| 98 | + $this->assertEquals($this->messageId, $response['reminder']['message_id']); |
| 99 | + $this->assertEquals($this->user['id'], $response['reminder']['user_id']); |
| 100 | + } |
| 101 | + |
| 102 | + public function testUpdateReminder() |
| 103 | + { |
| 104 | + // First create a reminder |
| 105 | + $this->client->createReminder($this->messageId, $this->user['id']); |
| 106 | + |
| 107 | + // Then update it |
| 108 | + $newRemindAt = new DateTime('+2 days'); |
| 109 | + $response = $this->client->updateReminder($this->messageId, $this->user['id'], $newRemindAt); |
| 110 | + |
| 111 | + $this->assertArrayHasKey('reminder', $response); |
| 112 | + $this->assertEquals($this->messageId, $response['reminder']['message_id']); |
| 113 | + $this->assertEquals($this->user['id'], $response['reminder']['user_id']); |
| 114 | + $this->assertNotEmpty($response['reminder']['remind_at']); |
| 115 | + } |
| 116 | + |
| 117 | + public function testDeleteReminder() |
| 118 | + { |
| 119 | + // First create a reminder |
| 120 | + $this->client->createReminder($this->messageId, $this->user['id']); |
| 121 | + |
| 122 | + // Then delete it |
| 123 | + $response = $this->client->deleteReminder($this->messageId, $this->user['id']); |
| 124 | + |
| 125 | + // The response is a StreamResponse object, so we'll just check that it exists |
| 126 | + $this->assertNotNull($response); |
| 127 | + $this->assertTrue(true); // If we got here, the test passed |
| 128 | + } |
| 129 | + |
| 130 | + public function testQueryReminders() |
| 131 | + { |
| 132 | + // Create a reminder |
| 133 | + $remindAt = new DateTime('+1 day'); |
| 134 | + $this->client->createReminder($this->messageId, $this->user['id'], $remindAt); |
| 135 | + |
| 136 | + // Query reminders |
| 137 | + $response = $this->client->queryReminders($this->user['id']); |
| 138 | + |
| 139 | + $this->assertArrayHasKey('reminders', $response); |
| 140 | + $this->assertGreaterThan(0, count($response['reminders'])); |
| 141 | + |
| 142 | + // Test with filter conditions |
| 143 | + $filterConditions = [ |
| 144 | + 'message_id' => $this->messageId |
| 145 | + ]; |
| 146 | + $response = $this->client->queryReminders($this->user['id'], $filterConditions); |
| 147 | + |
| 148 | + $this->assertArrayHasKey('reminders', $response); |
| 149 | + $this->assertGreaterThan(0, count($response['reminders'])); |
| 150 | + $this->assertEquals($this->messageId, $response['reminders'][0]['message_id']); |
| 151 | + } |
| 152 | +} |
0 commit comments