From 6e170303b0dc0ba9eafc06706c396945b77bcbe3 Mon Sep 17 00:00:00 2001 From: vagrant Date: Sat, 23 Nov 2019 15:58:02 +0000 Subject: [PATCH] Add test-coverage, add more tests --- .gitignore | 2 + composer.json | 13 ++-- phpunit.xml | 47 +++++++------- src/MultiMailer.php | 29 ++++++--- tests/MultiMailTest.php | 33 ---------- tests/TestCase.php | 42 +++++++++++++ tests/Unit/Fixtures/view.blade.php | 1 + tests/Unit/MultiMailTest.php | 98 ++++++++++++++++++++++++++++++ tests/Unit/PendingMailTest.php | 33 ++++++++++ 9 files changed, 231 insertions(+), 67 deletions(-) delete mode 100644 tests/MultiMailTest.php create mode 100644 tests/TestCase.php create mode 100644 tests/Unit/Fixtures/view.blade.php create mode 100644 tests/Unit/MultiMailTest.php create mode 100644 tests/Unit/PendingMailTest.php diff --git a/.gitignore b/.gitignore index bd72dd3..13b3fba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ vendor .lock composer.lock +.phpunit.result.cache +tests/_report diff --git a/composer.json b/composer.json index 59ce6aa..870fcf1 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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": { @@ -40,5 +36,10 @@ "MultiMail": "IWasHereFirst2\\LaravelMultiMail\\Facades\\MultiMail" } } + }, + "scripts": { + "coverage-report": [ + "vendor/bin/phpunit -d xdebug.profiler_enable=On --coverage-html tests/_report/" + ] } } diff --git a/phpunit.xml b/phpunit.xml index 4b44041..c29661d 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,30 +1,35 @@ -?xml version="1.0" encoding="UTF-8"?> - - - + + - - tests + + ./tests/Unit - + + ./tests/Feature + + - - ./tests/ + + ./src - - + + + + + + - diff --git a/src/MultiMailer.php b/src/MultiMailer.php index aac777f..a2e2ee5 100644 --- a/src/MultiMailer.php +++ b/src/MultiMailer.php @@ -16,6 +16,8 @@ class MultiMailer protected $locale; + protected static $plugins; + /** * Send mail throug mail account form $mailer_name * @param MailableContract $mailable @@ -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 @@ -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; } diff --git a/tests/MultiMailTest.php b/tests/MultiMailTest.php deleted file mode 100644 index 97a436e..0000000 --- a/tests/MultiMailTest.php +++ /dev/null @@ -1,33 +0,0 @@ -cc($cc) - ->locale($locale) - ->from($from) - ->bcc($bcc); - - $pendingMail2 = MultiMail::locale($locale) - ->from($from) - ->to($to) - ->cc($cc) - ->bcc($bcc); - - $this->assertEquals($pendingMail,$pendingMail2); - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..0efd94e --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,42 @@ +set('database.default', 'testbench'); + $app['config']->set('database.connections.testbench', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + } +} diff --git a/tests/Unit/Fixtures/view.blade.php b/tests/Unit/Fixtures/view.blade.php new file mode 100644 index 0000000..71c40be --- /dev/null +++ b/tests/Unit/Fixtures/view.blade.php @@ -0,0 +1 @@ +{{__('nom')}} diff --git a/tests/Unit/MultiMailTest.php b/tests/Unit/MultiMailTest.php new file mode 100644 index 0000000..41784ec --- /dev/null +++ b/tests/Unit/MultiMailTest.php @@ -0,0 +1,98 @@ +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', + ['test@fake.de' => [ + '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()); + } +} diff --git a/tests/Unit/PendingMailTest.php b/tests/Unit/PendingMailTest.php new file mode 100644 index 0000000..d9b370a --- /dev/null +++ b/tests/Unit/PendingMailTest.php @@ -0,0 +1,33 @@ +cc($cc) + ->locale($locale) + ->from($from) + ->bcc($bcc); + + $pendingMail2 = MultiMail::locale($locale) + ->from($from) + ->to($to) + ->cc($cc) + ->bcc($bcc); + + $this->assertEquals($pendingMail, $pendingMail2); + } +}