ChatControllerTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 ChatControllerTest extends TestCase
  9. {
  10. use WithFaker;
  11. public function test_can_create_chat()
  12. {
  13. $data = [
  14. 'title' => $this->faker->sentence,
  15. 'user_id' => "ba5463f3-72d1-4410-858e-eadd10884713"
  16. ];
  17. $response = $this->postJson('/api/v2/chats', $data);
  18. $response->assertStatus(200)
  19. ->assertJsonStructure([
  20. 'data' => [
  21. 'id',
  22. 'title',
  23. 'user_id',
  24. 'created_at',
  25. 'updated_at'
  26. ]
  27. ]);
  28. $this->assertDatabaseHas('chats', [
  29. 'title' => $data['title']
  30. ]);
  31. }
  32. public function test_can_list_chats()
  33. {
  34. Chat::factory()->count(1)->create();
  35. $response = $this->getJson('/api/v2/chats?user_id=ba5463f3-72d1-4410-858e-eadd10884713');
  36. $response->assertStatus(200)
  37. ->assertJsonStructure([
  38. 'data' => [
  39. 'rows' => [
  40. '*' => [
  41. 'id',
  42. 'title',
  43. 'created_at',
  44. 'updated_at'
  45. ]
  46. ]
  47. ]
  48. ]);
  49. }
  50. public function test_can_show_chat()
  51. {
  52. $chat = Chat::factory()->create();
  53. $response = $this->getJson("/api/v2/chats/{$chat->uid}");
  54. $response->assertStatus(200)
  55. ->assertJson([
  56. 'data' => [
  57. 'id' => $chat->uid,
  58. 'title' => $chat->title
  59. ]
  60. ]);
  61. }
  62. public function test_can_delete_chat()
  63. {
  64. $chat = Chat::factory()->create();
  65. $response = $this->deleteJson("/api/chats/{$chat->uid}");
  66. $response->assertStatus(200);
  67. $this->assertDatabaseMissing('chats', [
  68. 'id' => $chat->id
  69. ]);
  70. }
  71. }