Files
zemailnator/tests/Unit/Models/MessageTest.php
2025-11-14 01:51:35 -08:00

72 lines
1.9 KiB
PHP

<?php
namespace Tests\Unit\Models;
use App\Models\Message;
use Carbon\Carbon;
use Tests\TestCase;
class MessageTest extends TestCase
{
/** @test */
public function it_can_create_a_message_with_factory()
{
$message = Message::factory()->create();
$this->assertInstanceOf(Message::class, $message);
$this->assertIsString($message->subject);
$this->assertIsString($message->from);
}
/** @test */
public function it_has_correct_fillable_attributes()
{
$messageData = [
'subject' => 'Test Message',
'from' => 'Test Sender <sender@example.com>',
'to' => 'recipient@example.com',
'body' => 'Test body content',
'attachments' => null,
'is_seen' => false,
];
$message = Message::create($messageData);
foreach ($messageData as $key => $value) {
$this->assertEquals($value, $message->$key);
}
}
/** @test */
public function it_stores_to_field_as_string()
{
$to = 'test1@example.com';
$message = Message::factory()->create(['to' => $to]);
$this->assertIsString($message->to);
$this->assertEquals($to, $message->to);
}
/** @test */
public function it_uses_created_at_as_timestamp()
{
$message = Message::factory()->create();
$this->assertInstanceOf(Carbon::class, $message->created_at);
$this->assertNotNull($message->created_at);
}
/** @test */
public function it_can_query_unseen_messages()
{
$unseenMessage = Message::factory()->create(['is_seen' => false]);
$seenMessage = Message::factory()->create(['is_seen' => true]);
$unseenMessages = Message::where('is_seen', false)->get();
$seenMessages = Message::where('is_seen', true)->get();
$this->assertCount(1, $unseenMessages);
$this->assertCount(1, $seenMessages);
}
}