Permalink
Browse files

Adds global `from` support (#42)

  • Loading branch information...
1 parent 96d54cf commit 4a341566a41042da9e83397929c55cdc0caa27e6 @gpopoteur gpopoteur committed with besologic Sep 13, 2016
Showing with 165 additions and 49 deletions.
  1. +5 −0 composer.json
  2. +35 −4 src/MailThief.php
  3. +1 −22 tests/InteractsWithMailTest.php
  4. +69 −23 tests/MailThiefTest.php
  5. +55 −0 tests/TestCase.php
View
@@ -21,5 +21,10 @@
"psr-4": {
"MailThief\\": "src/"
}
+ },
+ "autoload-dev": {
+ "classmap": [
+ "tests/TestCase.php"
+ ]
}
}
View
@@ -8,6 +8,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Mail;
+use Illuminate\Contracts\Config\Repository as ConfigRepository;
use InvalidArgumentException;
use MailThief\Support\MailThiefCollection;
@@ -17,9 +18,10 @@ class MailThief implements Mailer, MailQueue
public $messages;
public $later;
- public function __construct(Factory $views)
+ public function __construct(Factory $views, ConfigRepository $config)
{
$this->views = $views;
+ $this->config = $config;
$this->messages = new MailThiefCollection;
$this->later = new MailThiefCollection;
}
@@ -31,22 +33,35 @@ public static function instance()
public function hijack()
{
+ $this->swapMail();
+ $this->loadGlobalFrom();
+ }
+
+ protected function swapMail()
+ {
Mail::swap($this);
app()->instance(Mailer::class, $this);
}
+ protected function loadGlobalFrom()
+ {
+ if ($this->config->has('mail.from.address')) {
+ $this->alwaysFrom($this->config->get('mail.from.address'), $this->config->get('mail.from.name'));
+ }
+ }
+
public function raw($text, $callback)
{
$message = Message::fromRaw($text);
- $callback($message);
+ $this->prepareMessage($message, $callback);
$this->messages[] = $message;
}
public function send($view, array $data = [], $callback = null)
{
$callback = $callback ?: null;
$message = Message::fromView($this->renderViews($view, $data), $data);
- $callback($message);
+ $this->prepareMessage($message, $callback);
$this->messages[] = $message;
}
@@ -113,7 +128,7 @@ public function later($delay, $view, array $data, $callback, $queue = null)
{
$message = Message::fromView($view, $data);
$message->delay = $delay;
- $callback($message);
+ $this->prepareMessage($message, $callback);
$this->later[] = $message;
}
@@ -133,4 +148,20 @@ public function lastMessage()
{
return $this->messages->last();
}
+
+ public function prepareMessage($message, $callback)
+ {
+ if (! empty($this->from['address'])) {
+ $message->from($this->from['address'], $this->from['name']);
+ }
+
+ $callback($message);
+
+ return $message;
+ }
+
+ public function alwaysFrom($address, $name = null)
+ {
+ $this->from = ['address' => $address, 'name' => $name];
+ }
}
@@ -1,33 +1,12 @@
<?php
-use Illuminate\Contracts\View\Factory;
use MailThief\MailThief;
use MailThief\Testing\InteractsWithMail;
-class InteractsWithMailTest extends PHPUnit_Framework_TestCase
+class InteractsWithMailTest extends TestCase
{
use InteractsWithMail;
- private function getViewFactory()
- {
- $factory = Mockery::mock(Factory::class);
- $factory->shouldReceive('make')->andReturnUsing(function ($template, $data) {
- return new class {
- public function render()
- {
- return 'stubbed rendered view';
- }
- };
- });
-
- return $factory;
- }
-
- private function getMailThief()
- {
- return new MailThief($this->getViewFactory());
- }
-
private function getMockMailThief()
{
return Mockery::mock(MailThief::class);
@@ -1,29 +1,7 @@
<?php
-use Illuminate\Contracts\View\Factory;
-use MailThief\MailThief;
-
-class MailThiefTest extends PHPUnit_Framework_TestCase
+class MailThiefTest extends TestCase
{
- private function getViewFactory()
- {
- $factory = Mockery::mock(Factory::class);
- $factory->shouldReceive('make')->andReturnUsing(function ($template, $data) {
- return new class {
- public function render()
- {
- return 'stubbed rendered view';
- }
- };
- });
- return $factory;
- }
-
- private function getMailThief()
- {
- return new MailThief($this->getViewFactory());
- }
-
public function test_send_to_one_recipient()
{
$mailer = $this->getMailThief();
@@ -121,6 +99,33 @@ public function test_from_overrides_previous_from()
$this->assertEquals(['jane@example.com'], $mailer->lastMessage()->from->all());
}
+ public function test_global_from_is_respected()
+ {
+ $mailer = $this->getMailThief();
+
+ $mailer->alwaysFrom('john@example.com');
+
+ $mailer->send('example-view', [], function ($m) {
+ $m->to('joe@example.com');
+ });
+
+ $this->assertEquals(['john@example.com'], $mailer->lastMessage()->from->all());
+ }
+
+ public function test_global_from_gets_overwritten_if_specified()
+ {
+ $mailer = $this->getMailThief();
+
+ $mailer->alwaysFrom('john@example.com');
+
+ $mailer->send('example-view', [], function ($m) {
+ $m->to('joe@example.com');
+ $m->from(['jane@example.com']);
+ });
+
+ $this->assertEquals(['jane@example.com'], $mailer->lastMessage()->from->all());
+ }
+
public function test_sender_returns_array_of_senders()
{
$mailer = $this->getMailThief();
@@ -218,6 +223,19 @@ public function test_queue_on_is_sent_immediately()
$this->assertTrue($mailer->hasMessageFor('john@example2.com'));
}
+ public function test_global_from_is_respected_when_email_is_queued()
+ {
+ $mailer = $this->getMailThief();
+
+ $mailer->alwaysFrom('john@example.com');
+
+ $mailer->queue('example-view', [], function ($m) {
+ $m->to('joe@example.com');
+ });
+
+ $this->assertEquals(['john@example.com'], $mailer->lastMessage()->from->all());
+ }
+
public function test_later_messages_are_marked_with_delay()
{
$mailer = $this->getMailThief();
@@ -251,6 +269,19 @@ public function test_later_messages_are_not_included_when_checking_to_see_if_an_
$this->assertFalse($mailer->hasMessageFor('john@example.com'));
}
+ public function test_global_from_is_respected_when_email_set_to_later_with_a_delay()
+ {
+ $mailer = $this->getMailThief();
+
+ $mailer->alwaysFrom('john@example.com');
+
+ $mailer->later(10, 'example-view', [], function ($m) {
+ $m->to('joe@example.com');
+ });
+
+ $this->assertEquals(['john@example.com'], $mailer->later->first()->from->all());
+ }
+
public function test_can_retrieve_last_sent_message()
{
$mailer = $this->getMailThief();
@@ -357,4 +388,19 @@ public function test_invalid_message_method_in_mailthief_causes_exception()
$m->foo('bar');
});
}
+
+ public function test_it_reads_values_from_the_config_helper_function()
+ {
+ $mailer = \Mockery::mock('MailThief\MailThief[swapMail]', [$this->getViewFactory(), $this->getConfigFactory()])
+ ->shouldAllowMockingProtectedMethods();
+ $mailer->shouldReceive('swapMail')->once()->andReturn(null);
+
+ $mailer->hijack();
+
+ $mailer->send('example-view', [], function ($m) {
+ $m->to('joe@example.com');
+ });
+
+ $this->assertEquals(['foo@bar.tld' => 'First Last'], $mailer->lastMessage()->from->all());
+ }
}
View
@@ -0,0 +1,55 @@
+<?php
+
+use MailThief\MailThief;
+use Illuminate\Contracts\View\Factory;
+use Illuminate\Contracts\Config\Repository as ConfigRepository;
+
+abstract class TestCase extends PHPUnit_Framework_TestCase {
+
+ protected function getViewFactory()
+ {
+ $factory = Mockery::mock(Factory::class);
+ $factory->shouldReceive('make')->andReturnUsing(function ($template, $data) {
+ return new class {
+ public function render()
+ {
+ return 'stubbed rendered view';
+ }
+ };
+ });
+ return $factory;
+ }
+
+ protected function getConfigFactory()
+ {
+ $configKeys = [
+ 'mail.from.name' => 'First Last',
+ 'mail.from.address' => 'foo@bar.tld',
+ ];
+
+ $config = Mockery::mock(ConfigRepository::class);
+ $config->shouldReceive('has')->andReturnUsing(function ($key) use ($configKeys) {
+ if( isset($configKeys[$key]) ){
+ return true;
+ }
+
+ return false;
+ });
+
+ $config->shouldReceive('get')->andReturnUsing(function ($key) use ($configKeys) {
+ if( isset($configKeys[$key]) ){
+ return $configKeys[$key];
+ }
+
+ return null;
+ });
+
+ return $config;
+ }
+
+ protected function getMailThief()
+ {
+ return new MailThief($this->getViewFactory(), $this->getConfigFactory());
+ }
+
+}

0 comments on commit 4a34156

Please sign in to comment.