fix: resolve PSR-4 autoloading and test failures

- Add proper Tests\ namespace to all test classes in tests/Feature and tests/Unit
  - Split RemainingModelsTest.php into separate files (PSR-4 compliance)
  - Create missing factories: MetaFactory, RemoteEmailFactory
  - Add HasFactory trait to RemoteEmail model
  - Add missing ReflectionClass imports to test files
  - Fix mass assignment issues in Meta and RemoteEmail models
  - Override database connection for RemoteEmail in testing environment
  - Fix DateTime comparison precision issues in tests
This commit is contained in:
idevakk
2025-11-13 09:49:21 -08:00
parent 68ef391c5d
commit 3706072ce5
38 changed files with 863 additions and 680 deletions

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Feature;
// Simple test to check if Laravel application is working
use Tests\TestCase;

View File

@@ -1,7 +1,10 @@
<?php
namespace Tests\Feature\Controllers;
use App\Http\Controllers\AppController;
use Illuminate\Support\Facades\Config;
use ReflectionClass;
use Tests\TestCase;
class AppControllerTest extends TestCase

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Feature\Controllers;
use App\Http\Controllers\WebhookController;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Tests\Concerns\LoadsApplicationData;

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Feature\Filament;
use App\Filament\Resources\BlogResource;
use App\Filament\Resources\BlogResource\Pages\CreateBlog;
use App\Filament\Resources\PlanResource;

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Feature\Filament;
use App\Filament\Resources\UserResource;
use App\Filament\Resources\UserResource\Pages\CreateUser;
use App\Filament\Resources\UserResource\Pages\EditUser;

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Feature\Livewire\Auth;
use App\Livewire\Auth\Login;
use App\Models\User;
use Illuminate\Auth\Events\Lockout;

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Feature\Livewire\Auth;
use App\Livewire\Auth\Register;
use App\Models\User;
use Illuminate\Auth\Events\Registered;

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Feature\Livewire;
use App\Models\Plan;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Feature\Livewire;
use App\Models\Email;
use App\Models\ZEmail;
use Illuminate\Support\Facades\Cookie;

View File

@@ -1,7 +1,10 @@
<?php
namespace Tests\Unit;
use App\ColorPicker;
use App\Models\Email;
use ReflectionClass;
use Tests\TestCase;
// Create a test class that uses the trait

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Unit;
test('that true is true', function () {
expect(true)->toBeTrue();
});

View File

@@ -0,0 +1,75 @@
<?php
namespace Tests\Unit\Models;
use App\Models\ActivationKey;
use App\Models\User;
use Tests\TestCase;
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);
}
}

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Unit\Models;
use App\Models\Blog;
use App\Models\Category;
use App\Models\User;

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Unit\Models;
use App\Models\Blog;
use App\Models\Category;
use Tests\TestCase;

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Unit\Models;
use App\Models\Email;
use App\Models\RemoteEmail;
use Carbon\Carbon;

View File

@@ -0,0 +1,75 @@
<?php
namespace Tests\Unit\Models;
use App\Models\Log;
use App\Models\User;
use Tests\TestCase;
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);
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Tests\Unit\Models;
use App\Models\Menu;
use Tests\TestCase;
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);
}
}

View File

@@ -0,0 +1,71 @@
<?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);
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Tests\Unit\Models;
use App\Models\Meta;
use Tests\TestCase;
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',
];
$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);
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace Tests\Unit\Models;
use App\Models\Page;
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);
}
}

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Unit\Models;
use App\Models\Plan;
use Tests\TestCase;

View File

@@ -0,0 +1,79 @@
<?php
namespace Tests\Unit\Models;
use App\Models\PremiumEmail;
use App\Models\User;
use Carbon\Carbon;
use Tests\TestCase;
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);
}
}

View File

@@ -1,625 +0,0 @@
<?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);
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Tests\Unit\Models;
use App\Models\RemoteEmail;
use Carbon\Carbon;
use Tests\TestCase;
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()
{
$timestamp = now();
$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' => $timestamp,
];
$remoteEmail = RemoteEmail::create($remoteEmailData);
foreach ($remoteEmailData as $key => $value) {
if ($key === 'timestamp') {
$this->assertInstanceOf(Carbon::class, $remoteEmail->$key);
$this->assertEquals($timestamp->format('Y-m-d H:i:s'), $remoteEmail->$key->format('Y-m-d H:i:s'));
} else {
$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->format('Y-m-d H:i:s'), $remoteEmail->timestamp->format('Y-m-d H:i:s'));
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace Tests\Unit\Models;
use App\Models\Setting;
use Tests\TestCase;
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);
}
}

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Unit\Models;
use App\Models\Ticket;
use App\Models\TicketResponse;
use App\Models\User;

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Unit\Models;
use App\Models\Ticket;
use App\Models\TicketResponse;
use App\Models\User;

View File

@@ -0,0 +1,67 @@
<?php
namespace Tests\Unit\Models;
use App\Models\UsageLog;
use App\Models\User;
use Tests\TestCase;
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);
}
}

View File

@@ -1,5 +1,7 @@
<?php
namespace Tests\Unit\Models;
use App\Models\Log;
use App\Models\Ticket;
use App\Models\UsageLog;

View File

@@ -1,10 +1,13 @@
<?php
namespace Tests\Unit\Models;
use App\Models\Email;
use App\Models\Message;
use App\Models\ZEmail;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Cookie;
use ReflectionClass;
use Tests\TestCase;
class ZEmailTest extends TestCase

View File

@@ -1,6 +1,9 @@
<?php
namespace Tests\Unit;
use App\Http\Controllers\WebhookController;
use App\NotifyMe;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
@@ -9,7 +12,7 @@ use Tests\TestCase;
// Create a test class that uses the trait
class TestNotifier
{
use App\NotifyMe;
use NotifyMe;
}
class NotifyMeTest extends TestCase