Skip to content

Commit

Permalink
Add test-coverage, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vagrant committed Nov 23, 2019
1 parent a25a75e commit 6e17030
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 67 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
vendor
.lock
composer.lock
.phpunit.result.cache
tests/_report
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
"description": "A package to send mails easily from multiple mail accounts with Laravel",
"keywords": ["multiple providers", "laravel", "emails"],
"license": "MIT",
"homepage": "",
"support": {
"issues": "",
"source": ""
},
"homepage": "https://github.com/iwasherefirst2/Laravel-MultiMail",
"authors": [
{
"name": "Dr. Adam Nielsen",
Expand All @@ -18,7 +14,7 @@
"laravel/framework": "^5.6.0|^6.0.0"
},
"require-dev": {
"phpunit/phpunit": "~5.7.14|~6.1|~7.0|~8.0"
"orchestra/testbench": "^4.0"
},
"autoload": {
"psr-4": {
Expand All @@ -40,5 +36,10 @@
"MultiMail": "IWasHereFirst2\\LaravelMultiMail\\Facades\\MultiMail"
}
}
},
"scripts": {
"coverage-report": [
"vendor/bin/phpunit -d xdebug.profiler_enable=On --coverage-html tests/_report/"
]
}
}
47 changes: 26 additions & 21 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap = "vendor/autoload.php"
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false">

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="MultiMail Test Suite">
<directory>tests</directory>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>

<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./tests/</directory>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>

<php>
<env name="APP_ENV" value="testing"/>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
</php>

</phpunit>
29 changes: 22 additions & 7 deletions src/MultiMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class MultiMailer

protected $locale;

protected static $plugins;

/**
* Send mail throug mail account form $mailer_name
* @param MailableContract $mailable
Expand All @@ -28,11 +30,16 @@ public static function sendMail(MailableContract $mailable, $mailer_name)
if (empty($mailer_name)) {
return \Mail::send($mailable);
}

$mailer = static::getMailer($mailer_name);

$mailable->send($mailer);
}

public static function registerPlugin($plugin)
{
static::$plugins[] =$plugin;
}

public static function queueMail(MailableContract $mailable, $mailer_name)
{
// no mailer given, use default mailer
Expand Down Expand Up @@ -201,24 +208,32 @@ protected static function getLogTransport()
* @param array
* @return Swift_Mailer
*/
protected static function getSwiftMailer($config, $timeout = null, $frequency = null)
public static function getSwiftMailer($config, $timeout = null, $frequency = null)
{
$provider = static::getProvider($config['provider'] ?? null);

if (isset($provider['driver']) && $provider['driver'] == 'log') {
$transport = static::getLogTransport();

return new Swift_Mailer($transport);
$swift_mailer = new Swift_Mailer($transport);
}
else{
$transport = static::getSMTPTransport($config, $provider);

$transport = static::getSMTPTransport($config, $provider);
$swift_mailer = new Swift_Mailer($transport);

$swift_mailer = new Swift_Mailer($transport);
if (!empty($frequency) && !empty($timeout)) {
$swift_mailer->registerPlugin(new \Swift_Plugins_AntiFloodPlugin($frequency, $timeout));
}
}

if (!empty($frequency) && !empty($timeout)) {
$swift_mailer->registerPlugin(new \Swift_Plugins_AntiFloodPlugin($frequency, $timeout));
if(!empty(self::$plugins)){
foreach (self::$plugins as $plugin) {
$swift_mailer->registerPlugin($plugin);
}
}


return $swift_mailer;
}

Expand Down
33 changes: 0 additions & 33 deletions tests/MultiMailTest.php

This file was deleted.

42 changes: 42 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace IWasHereFirst2\LaravelMultiMail\Tests;

use IWasHereFirst2\LaravelMultiMail\MultiMailServiceProvider;

class TestCase extends \Orchestra\Testbench\TestCase
{
public function setUp(): void
{
parent::setUp();
}

/**
* add the package provider
*
* @param $app
* @return array
*/
protected function getPackageProviders($app)
{
return [MultiMailServiceProvider::class];
}

/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{

// Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}
}
1 change: 1 addition & 0 deletions tests/Unit/Fixtures/view.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{__('nom')}}
98 changes: 98 additions & 0 deletions tests/Unit/MultiMailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace IWasHereFirst2\LaravelMultiMail\Tests\Unit;

use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\View;
use IWasHereFirst2\LaravelMultiMail\Facades\MultiMail;
use IWasHereFirst2\LaravelMultiMail\Tests\TestCase;
use Swift_Events_EventListener;
use Swift_Message;

class MultiMailTest extends TestCase
{
const FROM = '[email protected]';

protected $emails;

public function setUp(): void
{
parent::setUp();

MultiMail::registerPlugin(new TestingMailEventListener($this));
}

/** @test */
public function check_if_mail_is_sendable()
{
$to = '[email protected]';
$cc = '[email protected]';
$locale = 'de';
$from = static::FROM;
$bcc = ['[email protected]', '[email protected]'];
$pendingMail = MultiMail::to($to)
->cc($cc)
->locale($locale)
->from($from)
->bcc($bcc)
->send(new TestMail());

$this->assertEquals(1, count($this->emails));
}

/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{

// Setup default database to use sqlite :memory:
$app['config']->set('multimail.emails',
['[email protected]' => [
'pass' => 'fakepass',
'username' => 'fakeusername',
'from' => 'Who knows',
]]);
$app['config']->set('multimail.provider.default', [
'driver' => 'log',
]);

View::addLocation(__DIR__ . '/Fixtures');
}

public function addEmail(Swift_Message $email)
{
$this->emails[] = $email;
}
}

class TestMail extends Mailable
{
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('view');
}
}

class TestingMailEventListener implements Swift_Events_EventListener
{
protected $test;

public function __construct($test)
{
$this->test = $test;
}

public function beforeSendPerformed($event)
{
$this->test->addEmail($event->getMessage());
}
}
33 changes: 33 additions & 0 deletions tests/Unit/PendingMailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace IWasHereFirst2\LaravelMultiMail\Tests\Unit;

use IWasHereFirst2\LaravelMultiMail\Facades\MultiMail;
use IWasHereFirst2\LaravelMultiMail\Tests\TestCase;

class PendingMailTest extends TestCase
{
/** @test */
public function check_if_object_chaining_works()
{
$to = '[email protected]';
$cc = '[email protected]';
$locale = 'de';
$from = '[email protected]';
$bcc = ['[email protected]', '[email protected]'];

$pendingMail = MultiMail::to($to)
->cc($cc)
->locale($locale)
->from($from)
->bcc($bcc);

$pendingMail2 = MultiMail::locale($locale)
->from($from)
->to($to)
->cc($cc)
->bcc($bcc);

$this->assertEquals($pendingMail, $pendingMail2);
}
}

0 comments on commit 6e17030

Please sign in to comment.