Skip to content

Commit

Permalink
Added Unit Test
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrulhazim committed Apr 9, 2023
1 parent 8dcf677 commit 0e27b96
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 15 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="MAIL_MAILER" value="log"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
Expand Down
2 changes: 2 additions & 0 deletions src/Listeners/StoreMessageSending.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CleaniqueCoders\MailHistory\Exceptions\MailHistoryException;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Support\Str;

class StoreMessageSending
{
Expand All @@ -23,6 +24,7 @@ public function handle(MessageSending $event): void
MailHistoryException::throwIfHashContractMissing();

config('mailhistory.model')::create([
'uuid' => Str::uuid(),
'hash' => config('mailhistory.model')::generateHashValue(
$event->message->getHeaders()->toArray()
),
Expand Down
4 changes: 4 additions & 0 deletions src/MailHistoryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function configurePackage(Package $package): void

public function packageRegistered()
{
if (! config('mailhistory.enabled')) {
return;
}

foreach (config('mailhistory.events') as $event => $listeners) {
foreach (array_unique($listeners, SORT_REGULAR) as $listener) {
Event::listen($event, $listener);
Expand Down
2 changes: 1 addition & 1 deletion tests/Mail/WelcomeMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function envelope(): Envelope
*/
public function content(): Content
{
return new Content();
return new Content(markdown: 'mail.welcome-mail');
}

/**
Expand Down
64 changes: 51 additions & 13 deletions tests/MailHistoryTest.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,66 @@
<?php

use CleaniqueCoders\MailHistory\Tests\Mail\WelcomeMail;
use CleaniqueCoders\MailHistory\Contracts\HashContract;
use CleaniqueCoders\MailHistory\Exceptions\MailHistoryException;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Mail\Events\MessageSent;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Event;

beforeEach(function () {
config()->set('view.paths', [
dirname(__FILE__).'/resources/views',
]);

Artisan::call('vendor:publish', [
'--tag' => 'mailhistory-migrations',
'--force' => true,
]);
Artisan::call('migrate:fresh');
});

it('can store sent email in database', function ($email) {
Mail::fake();
// assert email sending / sent
it('has HashContract', function () {
$this->assertTrue(
file_exists(dirname(__FILE__).'/../src/Contracts/HashContract.php')
);

$this->assertTrue(
in_array(HashContract::class, class_implements(config('mailhistory.model')))
);

$this->assertTrue(
class_exists(MailHistoryException::class)
);

$this->assertTrue(
method_exists(
MailHistoryException::class,
'throwIfHashContractMissing'
)
);
});

it('has MessageSending and MessageSent event listened to if the package is enabled', function () {
$this->assertTrue(
Event::hasListeners(MessageSending::class),
);

$this->assertTrue(
Event::hasListeners(MessageSent::class),
);
});

it('does not has the MessageSending and MessageSent event listened to if the package is disabled', function () {

Mail::to($email)
->send(new WelcomeMail);
config([
'mailhistory.enabled' => false,
]);

Mail::assertSent(WelcomeMail::class);
$this->assertTrue(
Event::hasListeners(MessageSending::class),
);

// $this->assertDatabaseCount('mail_histories', 2);
})->with([
'[email protected]',
'[email protected]',
]);
$this->assertTrue(
Event::hasListeners(MessageSent::class),
);
});
10 changes: 10 additions & 0 deletions tests/resources/views/mail/welcome-mail.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<x-mail::message>
Hi, welcome to {{ config('app.name') }}!

<x-mail::button url="{{ url('/') }}">
{{ config('app.name') }}
</x-mail::button>

Thanks,<br>
{{ config('app.name') }}
</x-mail::message>

0 comments on commit 0e27b96

Please sign in to comment.