2
0

ChatMessageControllerTest.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\Chat;
  4. use App\Models\ChatMessage;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. use Illuminate\Foundation\Testing\WithFaker;
  7. use Tests\TestCase;
  8. class ChatMessageControllerTest extends TestCase
  9. {
  10. use WithFaker;
  11. protected $chat;
  12. protected function setUp(): void
  13. {
  14. parent::setUp();
  15. $this->chat = Chat::factory()->create();
  16. }
  17. public function test_can_create_chat_message()
  18. {
  19. $data = [
  20. 'chat_id' => $this->chat->uid,
  21. 'messages' => [
  22. [
  23. 'role' => 'user',
  24. 'content' => $this->faker->sentence
  25. ]
  26. ]
  27. ];
  28. $response = $this->postJson("/api/v2/chat-messages", $data);
  29. $response->assertStatus(200);
  30. $this->assertDatabaseHas('chat_messages', [
  31. 'chat_id' => $this->chat->uid,
  32. 'role' => 'user',
  33. 'content' => $data['messages'][0]['content']
  34. ]);
  35. }
  36. public function test_can_list_chat_messages()
  37. {
  38. ChatMessage::factory()->count(3)->create([
  39. 'chat_id' => $this->chat->id
  40. ]);
  41. $response = $this->getJson("/api/v2/chat-messages?chat={$this->chat->id}");
  42. $response->assertStatus(200);
  43. }
  44. }