Skip to content

Commit

Permalink
Fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrulhazim committed Nov 27, 2024
1 parent 67580b5 commit a716392
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 86 deletions.
77 changes: 32 additions & 45 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,47 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="CleaniqueCoders Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="log"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="CleaniqueCoders Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="log"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
59 changes: 24 additions & 35 deletions tests/MailHistoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]')
->from('[email protected]')
->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"}',
]);
});
4 changes: 1 addition & 3 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php

use CleaniqueCoders\MailHistory\Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(
TestCase::class,
RefreshDatabase::class
TestCase::class
)->in(__DIR__);
5 changes: 3 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
*/
}
}
1 change: 0 additions & 1 deletion workbench/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit a716392

Please sign in to comment.