From a7163922acb61e7e1c686e314aa4ff37883cd11a Mon Sep 17 00:00:00 2001 From: Nasrul Hazim Bin Mohamad Date: Wed, 27 Nov 2024 09:53:25 +0800 Subject: [PATCH] Fixed unit tests --- phpunit.xml.dist | 77 +++++++++++++++-------------------- tests/MailHistoryTest.php | 59 +++++++++++---------------- tests/Pest.php | 4 +- tests/TestCase.php | 5 ++- workbench/app/Models/User.php | 1 - 5 files changed, 60 insertions(+), 86 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 439b8a7..94b2f6f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,47 +1,34 @@ - - - - tests - - - - - ./src - - - - - - - - - - - - - - - - - - - - - + + + + tests + + + + + + + + + + + + + + + + + + + + + + + + + + ./src + + diff --git a/tests/MailHistoryTest.php b/tests/MailHistoryTest.php index 0cfe259..99a0bb7 100644 --- a/tests/MailHistoryTest.php +++ b/tests/MailHistoryTest.php @@ -2,62 +2,51 @@ use Illuminate\Mail\Events\MessageSending; use Illuminate\Mail\Events\MessageSent; -use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Event; -use Illuminate\Support\Facades\DB; - -beforeEach(function () { - // Set view paths - config()->set('view.paths', [ - dirname(__FILE__).'/resources/views', - ]); - - // Commit any existing transactions - try { - DB::commit(); - } catch (\Exception $e) { - // Ignore any commit errors since there might not be an active transaction - } - - // Run migrations - Artisan::call('vendor:publish', [ - '--tag' => 'mailhistory-migrations', - '--force' => true, - ]); - - // Fresh migration without transaction wrapping - DB::unprepared('PRAGMA foreign_keys = OFF;'); - Artisan::call('migrate:fresh'); - DB::unprepared('PRAGMA foreign_keys = ON;'); -}); +use Illuminate\Support\Facades\Mail; it('has MessageSending and MessageSent event listened to if the package is enabled', function () { $this->assertTrue( - Event::hasListeners(MessageSending::class), + Event::hasListeners(MessageSending::class) ); $this->assertTrue( - Event::hasListeners(MessageSent::class), + Event::hasListeners(MessageSent::class) ); }); -it('does not has the MessageSending and MessageSent event listened to if the package is disabled', function () { +it('does not have the MessageSending and MessageSent event listened when disabled', function () { config([ 'mailhistory.enabled' => false, ]); - Event::fake(); - foreach (config('mailhistory.events') as $event => $listeners) { Event::flush($event); Event::forget($event); } $this->assertFalse( - Event::hasListeners(MessageSending::class), + Event::hasListeners(MessageSending::class) ); $this->assertFalse( - Event::hasListeners(MessageSent::class), + Event::hasListeners(MessageSent::class) ); -})->skip('Need to work on disabling the event listener at runtime.'); +}); + +it('stores mail history when sending mail', function () { + $mailable = new \Illuminate\Mail\Mailable; + $mailable->to('test@example.com') + ->from('from@example.com') + ->subject('Test Subject') + ->html('Test content'); + + Mail::send($mailable); + + $this->assertDatabaseHas('mail_histories', [ + 'status' => 'Sending', + 'body' => 'Test content', + 'content' => '{"text":null,"text-charset":null,"html":"Test content","html-charset":"utf-8"}', + 'meta' => '{"origin":"Mail"}', + ]); +}); diff --git a/tests/Pest.php b/tests/Pest.php index 40a26e9..2c665c7 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,9 +1,7 @@ in(__DIR__); diff --git a/tests/TestCase.php b/tests/TestCase.php index a1f168c..bf96982 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -27,10 +27,11 @@ protected function getPackageProviders($app) public function getEnvironmentSetUp($app) { config()->set('database.default', 'testing'); + config()->set('view.paths', [ + dirname(__FILE__).'/resources/views', + ]); - /* $migration = include __DIR__.'/../database/migrations/create_mailhistory_table.php.stub'; $migration->up(); - */ } } diff --git a/workbench/app/Models/User.php b/workbench/app/Models/User.php index 1405251..accfba6 100644 --- a/workbench/app/Models/User.php +++ b/workbench/app/Models/User.php @@ -6,7 +6,6 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; -use Laravel\Sanctum\HasApiTokens; class User extends Authenticatable {