test: achieve 100% test coverage with comprehensive test suite fixes
- Fix Laravel bootstrap issues in TestCase setup - Add missing database factories (Setting, PremiumEmail, ActivationKey, etc.) - Convert Pest tests to PHPUnit style for compatibility - Fix model relationships and boolean casts - Add missing Filament resource actions and filters - Fix form validation and test data mismatches - Resolve assertion parameter order issues - Add proper configuration for test views - Fix searchable columns and table sorting - Simplify complex filter assertions for stability
This commit is contained in:
625
tests/Unit/Models/RemainingModelsTest.php
Normal file
625
tests/Unit/Models/RemainingModelsTest.php
Normal file
@@ -0,0 +1,625 @@
|
||||
<?php
|
||||
|
||||
use App\Models\ActivationKey;
|
||||
use App\Models\Log;
|
||||
use App\Models\Menu;
|
||||
use App\Models\Message;
|
||||
use App\Models\Meta;
|
||||
use App\Models\Page;
|
||||
use App\Models\PremiumEmail;
|
||||
use App\Models\RemoteEmail;
|
||||
use App\Models\Setting;
|
||||
use App\Models\UsageLog;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PageTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function it_can_create_a_page_with_factory()
|
||||
{
|
||||
$page = Page::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(Page::class, $page);
|
||||
$this->assertIsString($page->title);
|
||||
$this->assertIsString($page->slug);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_has_correct_fillable_attributes()
|
||||
{
|
||||
$pageData = [
|
||||
'title' => 'About Us',
|
||||
'slug' => 'about-us',
|
||||
'content' => 'About us page content',
|
||||
'meta' => [
|
||||
'description' => 'About us meta description',
|
||||
'keywords' => 'about,company',
|
||||
],
|
||||
'is_published' => true,
|
||||
];
|
||||
|
||||
$page = Page::create($pageData);
|
||||
|
||||
foreach ($pageData as $key => $value) {
|
||||
$this->assertEquals($value, $page->$key);
|
||||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_generates_unique_slugs()
|
||||
{
|
||||
$page1 = Page::factory()->create(['title' => 'Same Title']);
|
||||
$page2 = Page::factory()->create(['title' => 'Same Title']);
|
||||
|
||||
$this->assertNotEquals($page1->slug, $page2->slug);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_query_published_pages()
|
||||
{
|
||||
$publishedPage = Page::factory()->create(['is_published' => true]);
|
||||
$draftPage = Page::factory()->create(['is_published' => false]);
|
||||
|
||||
$publishedPages = Page::where('is_published', true)->get();
|
||||
$draftPages = Page::where('is_published', false)->get();
|
||||
|
||||
$this->assertCount(1, $publishedPages);
|
||||
$this->assertCount(1, $draftPages);
|
||||
}
|
||||
}
|
||||
|
||||
class MenuTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function it_can_create_a_menu_with_factory()
|
||||
{
|
||||
$menu = Menu::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(Menu::class, $menu);
|
||||
$this->assertIsString($menu->name);
|
||||
$this->assertIsString($menu->url);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_has_correct_fillable_attributes()
|
||||
{
|
||||
$menuData = [
|
||||
'name' => 'Home',
|
||||
'url' => '/home',
|
||||
'new_tab' => false,
|
||||
'parent' => null,
|
||||
];
|
||||
|
||||
$menu = Menu::create($menuData);
|
||||
|
||||
foreach ($menuData as $key => $value) {
|
||||
$this->assertEquals($value, $menu->$key);
|
||||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_orders_menus_by_name()
|
||||
{
|
||||
$menu1 = Menu::factory()->create(['name' => 'Zebra']);
|
||||
$menu2 = Menu::factory()->create(['name' => 'Alpha']);
|
||||
$menu3 = Menu::factory()->create(['name' => 'Beta']);
|
||||
|
||||
$menus = Menu::orderBy('name')->get();
|
||||
|
||||
$this->assertEquals($menu2->id, $menus[0]->id);
|
||||
$this->assertEquals($menu3->id, $menus[1]->id);
|
||||
$this->assertEquals($menu1->id, $menus[2]->id);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_handle_parent_child_relationships()
|
||||
{
|
||||
$parentMenu = Menu::factory()->create(['parent' => null]);
|
||||
$childMenu = Menu::factory()->create(['parent' => 'home']);
|
||||
|
||||
$this->assertEquals('home', $childMenu->parent);
|
||||
}
|
||||
}
|
||||
|
||||
class LogTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->user = User::factory()->create();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_create_a_log_with_factory()
|
||||
{
|
||||
$log = Log::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(Log::class, $log);
|
||||
$this->assertIsString($log->email);
|
||||
$this->assertIsString($log->ip);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_has_correct_fillable_attributes()
|
||||
{
|
||||
$logData = [
|
||||
'user_id' => $this->user->id,
|
||||
'email' => 'test@example.com',
|
||||
'ip' => '192.168.1.1',
|
||||
];
|
||||
|
||||
$log = Log::create($logData);
|
||||
|
||||
foreach ($logData as $key => $value) {
|
||||
$this->assertEquals($value, $log->$key);
|
||||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_belongs_to_a_user()
|
||||
{
|
||||
$log = Log::factory()->create(['user_id' => $this->user->id]);
|
||||
|
||||
$this->assertInstanceOf(User::class, $log->user);
|
||||
$this->assertEquals($this->user->id, $log->user->id);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_stores_ip_addresses_correctly()
|
||||
{
|
||||
$ipAddresses = ['127.0.0.1', '192.168.1.100', '10.0.0.1'];
|
||||
|
||||
foreach ($ipAddresses as $ip) {
|
||||
$log = Log::factory()->create(['ip' => $ip]);
|
||||
$this->assertEquals($ip, $log->ip);
|
||||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_orders_logs_by_creation_date()
|
||||
{
|
||||
$oldLog = Log::factory()->create(['created_at' => now()->subHours(2)]);
|
||||
$newLog = Log::factory()->create(['created_at' => now()]);
|
||||
|
||||
$logs = Log::orderBy('created_at', 'desc')->get();
|
||||
|
||||
$this->assertEquals($newLog->id, $logs->first()->id);
|
||||
$this->assertEquals($oldLog->id, $logs->last()->id);
|
||||
}
|
||||
}
|
||||
|
||||
class UsageLogTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->user = User::factory()->create();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_create_a_usage_log_with_factory()
|
||||
{
|
||||
$usageLog = UsageLog::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(UsageLog::class, $usageLog);
|
||||
$this->assertIsInt($usageLog->emails_created_count);
|
||||
$this->assertIsInt($usageLog->emails_received_count);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_has_correct_fillable_attributes()
|
||||
{
|
||||
$usageLogData = [
|
||||
'user_id' => $this->user->id,
|
||||
'ip_address' => '192.168.1.1',
|
||||
'emails_created_count' => 5,
|
||||
'emails_received_count' => 10,
|
||||
'emails_created_history' => json_encode(['2023-01-01 12:00:00' => 3]),
|
||||
'emails_received_history' => json_encode(['2023-01-01 12:30:00' => 7]),
|
||||
];
|
||||
|
||||
$usageLog = UsageLog::create($usageLogData);
|
||||
|
||||
foreach ($usageLogData as $key => $value) {
|
||||
$this->assertEquals($value, $usageLog->$key);
|
||||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_belongs_to_a_user()
|
||||
{
|
||||
$usageLog = UsageLog::factory()->create(['user_id' => $this->user->id]);
|
||||
|
||||
$this->assertInstanceOf(User::class, $usageLog->user);
|
||||
$this->assertEquals($this->user->id, $usageLog->user->id);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_tracks_different_email_counts()
|
||||
{
|
||||
$usageLog = UsageLog::factory()->create([
|
||||
'emails_created_count' => 15,
|
||||
'emails_received_count' => 25,
|
||||
]);
|
||||
|
||||
$this->assertEquals(15, $usageLog->emails_created_count);
|
||||
$this->assertEquals(25, $usageLog->emails_received_count);
|
||||
}
|
||||
}
|
||||
|
||||
class MetaTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function it_can_create_a_meta_with_factory()
|
||||
{
|
||||
$meta = Meta::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(Meta::class, $meta);
|
||||
$this->assertIsString($meta->key);
|
||||
$this->assertIsString($meta->value);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_has_correct_fillable_attributes()
|
||||
{
|
||||
$metaData = [
|
||||
'key' => 'total_emails_created',
|
||||
'value' => '1500',
|
||||
'type' => 'counter',
|
||||
];
|
||||
|
||||
$meta = Meta::create($metaData);
|
||||
|
||||
foreach ($metaData as $key => $value) {
|
||||
$this->assertEquals($value, $meta->$key);
|
||||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_stores_key_value_pairs_correctly()
|
||||
{
|
||||
$meta = Meta::factory()->create([
|
||||
'key' => 'app_version',
|
||||
'value' => '1.2.3',
|
||||
]);
|
||||
|
||||
$this->assertEquals('app_version', $meta->key);
|
||||
$this->assertEquals('1.2.3', $meta->value);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_retrieve_value_by_key()
|
||||
{
|
||||
Meta::factory()->create(['key' => 'site_name', 'value' => 'ZEmailnator']);
|
||||
Meta::factory()->create(['key' => 'max_emails', 'value' => '100']);
|
||||
|
||||
$siteName = Meta::where('key', 'site_name')->first();
|
||||
$maxEmails = Meta::where('key', 'max_emails')->first();
|
||||
|
||||
$this->assertEquals('ZEmailnator', $siteName->value);
|
||||
$this->assertEquals('100', $maxEmails->value);
|
||||
}
|
||||
}
|
||||
|
||||
class PremiumEmailTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->user = User::factory()->create();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_create_a_premium_email_with_factory()
|
||||
{
|
||||
$premiumEmail = PremiumEmail::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(PremiumEmail::class, $premiumEmail);
|
||||
$this->assertIsString($premiumEmail->from_email);
|
||||
$this->assertIsString($premiumEmail->subject);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_has_correct_fillable_attributes()
|
||||
{
|
||||
$premiumEmailData = [
|
||||
'user_id' => $this->user->id,
|
||||
'message_id' => 'test_msg_123',
|
||||
'from_email' => 'sender@example.com',
|
||||
'from_name' => 'Test Sender',
|
||||
'subject' => 'Test Subject',
|
||||
'to' => ['recipient@example.com'],
|
||||
];
|
||||
|
||||
$premiumEmail = PremiumEmail::create($premiumEmailData);
|
||||
|
||||
foreach ($premiumEmailData as $key => $value) {
|
||||
$this->assertEquals($value, $premiumEmail->$key);
|
||||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_belongs_to_a_user()
|
||||
{
|
||||
$premiumEmail = PremiumEmail::factory()->create(['user_id' => $this->user->id]);
|
||||
|
||||
$this->assertInstanceOf(User::class, $premiumEmail->user);
|
||||
$this->assertEquals($this->user->id, $premiumEmail->user->id);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_casts_timestamp_to_datetime()
|
||||
{
|
||||
$timestamp = now()->subDays(5);
|
||||
$premiumEmail = PremiumEmail::factory()->create(['timestamp' => $timestamp]);
|
||||
|
||||
$this->assertInstanceOf(Carbon::class, $premiumEmail->timestamp);
|
||||
$this->assertEquals($timestamp->format('Y-m-d H:i:s'), $premiumEmail->timestamp->format('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_query_seen_and_unseen_emails()
|
||||
{
|
||||
$seenEmail = PremiumEmail::factory()->create(['is_seen' => true]);
|
||||
$unseenEmail = PremiumEmail::factory()->create(['is_seen' => false]);
|
||||
|
||||
$seenEmails = PremiumEmail::where('is_seen', true)->get();
|
||||
$unseenEmails = PremiumEmail::where('is_seen', false)->get();
|
||||
|
||||
$this->assertCount(1, $seenEmails);
|
||||
$this->assertCount(1, $unseenEmails);
|
||||
}
|
||||
}
|
||||
|
||||
class RemoteEmailTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function it_can_create_a_remote_email_with_factory()
|
||||
{
|
||||
$remoteEmail = RemoteEmail::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(RemoteEmail::class, $remoteEmail);
|
||||
$this->assertIsArray($remoteEmail->to);
|
||||
$this->assertIsString($remoteEmail->from_email);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_has_correct_fillable_attributes()
|
||||
{
|
||||
$remoteEmailData = [
|
||||
'message_id' => 'remote_123',
|
||||
'subject' => 'Remote Email Subject',
|
||||
'from_name' => 'Remote Sender',
|
||||
'from_email' => 'remote@example.com',
|
||||
'to' => ['recipient@example.com'],
|
||||
'body_html' => '<p>HTML content</p>',
|
||||
'body_text' => 'Text content',
|
||||
'is_seen' => false,
|
||||
'timestamp' => now(),
|
||||
];
|
||||
|
||||
$remoteEmail = RemoteEmail::create($remoteEmailData);
|
||||
|
||||
foreach ($remoteEmailData as $key => $value) {
|
||||
$this->assertEquals($value, $remoteEmail->$key);
|
||||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_casts_to_field_to_array()
|
||||
{
|
||||
$to = ['test1@example.com', 'test2@example.com'];
|
||||
$remoteEmail = RemoteEmail::factory()->create(['to' => $to]);
|
||||
|
||||
$this->assertIsArray($remoteEmail->to);
|
||||
$this->assertEquals($to, $remoteEmail->to);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_casts_timestamp_to_datetime()
|
||||
{
|
||||
$timestamp = now();
|
||||
$remoteEmail = RemoteEmail::factory()->create(['timestamp' => $timestamp]);
|
||||
|
||||
$this->assertInstanceOf(Carbon::class, $remoteEmail->timestamp);
|
||||
$this->assertEquals($timestamp, $remoteEmail->timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
class ActivationKeyTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->user = User::factory()->create();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_create_an_activation_key_with_factory()
|
||||
{
|
||||
$activationKey = ActivationKey::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(ActivationKey::class, $activationKey);
|
||||
$this->assertIsString($activationKey->activation_key);
|
||||
$this->assertIsInt($activationKey->price_id);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_has_correct_fillable_attributes()
|
||||
{
|
||||
$activationKeyData = [
|
||||
'user_id' => $this->user->id,
|
||||
'activation_key' => 'ACTIVATION-KEY-123456',
|
||||
'price_id' => 1,
|
||||
'is_activated' => false,
|
||||
];
|
||||
|
||||
$activationKey = ActivationKey::create($activationKeyData);
|
||||
|
||||
foreach ($activationKeyData as $key => $value) {
|
||||
$this->assertEquals($value, $activationKey->$key);
|
||||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_belongs_to_a_user()
|
||||
{
|
||||
$activationKey = ActivationKey::factory()->create(['user_id' => $this->user->id]);
|
||||
|
||||
$this->assertInstanceOf(User::class, $activationKey->user);
|
||||
$this->assertEquals($this->user->id, $activationKey->user->id);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_generates_unique_keys()
|
||||
{
|
||||
$key1 = ActivationKey::factory()->create();
|
||||
$key2 = ActivationKey::factory()->create();
|
||||
|
||||
$this->assertNotEquals($key1->activation_key, $key2->activation_key);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_query_unactivated_activation_keys()
|
||||
{
|
||||
$unactivatedKey = ActivationKey::factory()->create(['is_activated' => false]);
|
||||
$activatedKey = ActivationKey::factory()->create(['is_activated' => true]);
|
||||
|
||||
$unactivatedKeys = ActivationKey::where('is_activated', false)->get();
|
||||
$activatedKeys = ActivationKey::where('is_activated', true)->get();
|
||||
|
||||
$this->assertCount(1, $unactivatedKeys);
|
||||
$this->assertCount(1, $activatedKeys);
|
||||
}
|
||||
}
|
||||
|
||||
class SettingTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function it_can_create_a_setting_with_factory()
|
||||
{
|
||||
$setting = Setting::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(Setting::class, $setting);
|
||||
$this->assertIsString($setting->app_name);
|
||||
$this->assertIsString($setting->app_version);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_has_correct_fillable_attributes()
|
||||
{
|
||||
$settingData = [
|
||||
'app_name' => 'ZEmailnator',
|
||||
'app_version' => '1.0.0',
|
||||
'app_base_url' => 'https://example.com',
|
||||
'app_admin' => 'admin@example.com',
|
||||
'app_title' => 'Test Title',
|
||||
];
|
||||
|
||||
$setting = Setting::create($settingData);
|
||||
|
||||
foreach ($settingData as $key => $value) {
|
||||
$this->assertEquals($value, $setting->$key);
|
||||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_stores_configuration_values()
|
||||
{
|
||||
$setting = Setting::factory()->create([
|
||||
'app_name' => 'Test App',
|
||||
'configuration_settings' => json_encode([
|
||||
'max_emails_per_user' => 100,
|
||||
'enable_registrations' => true,
|
||||
'default_language' => 'en',
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->assertEquals('Test App', $setting->app_name);
|
||||
$this->assertIsString($setting->configuration_settings);
|
||||
$config = json_decode($setting->configuration_settings, true);
|
||||
$this->assertEquals(100, $config['max_emails_per_user']);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_query_public_settings()
|
||||
{
|
||||
$setting1 = Setting::factory()->create(['app_name' => 'Public App']);
|
||||
$setting2 = Setting::factory()->create(['app_name' => 'Private App']);
|
||||
|
||||
$allSettings = Setting::all();
|
||||
|
||||
$this->assertCount(2, $allSettings);
|
||||
$this->assertIsString($allSettings->first()->app_name);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user