Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Markdown seems to be a dependency of MailChannel #19349

Merged
merged 4 commits into from
May 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/Illuminate/Notifications/ChannelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Illuminate\Notifications;

use Illuminate\Mail\Markdown;
use InvalidArgumentException;
use Illuminate\Support\Manager;
use Nexmo\Client as NexmoClient;
Expand Down Expand Up @@ -89,9 +88,7 @@ protected function createBroadcastDriver()
*/
protected function createMailDriver()
{
return $this->app->make(Channels\MailChannel::class)->setMarkdownResolver(function () {
return $this->app->make(Markdown::class);
});
return $this->app->make(Channels\MailChannel::class);
}

/**
Expand Down
31 changes: 9 additions & 22 deletions src/Illuminate/Notifications/Channels/MailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Illuminate\Notifications\Channels;

use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Mail\Markdown;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Contracts\Mail\Mailable;
use Illuminate\Notifications\Notification;
Expand All @@ -19,21 +19,23 @@ class MailChannel
protected $mailer;

/**
* The Markdown resolver callback.
* The markdown implementation.
*
* @var \Closure
* @var \Illuminate\Contracts\Mail\Mailer
*/
protected $markdownResolver;
protected $markdown;

/**
* Create a new mail channel instance.
*
* @param \Illuminate\Contracts\Mail\Mailer $mailer
* @param \Illuminate\Mail\Markdown $markdown
* @return void
*/
public function __construct(Mailer $mailer)
public function __construct(Mailer $mailer, Markdown $markdown)
{
$this->mailer = $mailer;
$this->markdown = $markdown;
}

/**
Expand Down Expand Up @@ -72,11 +74,9 @@ protected function buildView($message)
return $message->view;
}

$markdown = call_user_func($this->markdownResolver);

return [
'html' => $markdown->render($message->markdown, $message->data()),
'text' => $markdown->renderText($message->markdown, $message->data()),
'html' => $this->markdown->render($message->markdown, $message->data()),
'text' => $this->markdown->renderText($message->markdown, $message->data()),
];
}

Expand Down Expand Up @@ -172,17 +172,4 @@ protected function addAttachments($mailMessage, $message)
$mailMessage->attachData($attachment['data'], $attachment['name'], $attachment['options']);
}
}

/**
* Set the Markdown resolver callback.
*
* @param \Closure $callback
* @return $this
*/
public function setMarkdownResolver(Closure $callback)
{
$this->markdownResolver = $callback;

return $this;
}
}
188 changes: 188 additions & 0 deletions tests/Integration/Notifications/SendingMailNotificationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

use Illuminate\Mail\Markdown;
use Orchestra\Testbench\TestCase;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Mail\Mailable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Channels\MailChannel;
use Illuminate\Notifications\Messages\MailMessage;

/**
* @group integration
*/
class SendingMailNotificationsTest extends TestCase
{
public $mailer;

protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', 'true');

$app['config']->set('database.default', 'testbench');

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);

$this->mailer = Mockery::mock(Mailer::class);
$this->markdown = Mockery::mock(Markdown::class);

$app->extend(Markdown::class, function () {
return $this->markdown;
});

$app->extend(Mailer::class, function () {
return $this->mailer;
});
}

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

Schema::create('users', function ($table) {
$table->increments('id');
$table->string('email');
});
}

public function test_mail_is_sent()
{
$notification = new TestMailNotification();

$user = NotifiableUser::forceCreate([
'email' => '[email protected]',
]);

$this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');
$this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');

$this->mailer->shouldReceive('send')->once()->with(
['html' => 'htmlContent', 'text' => 'textContent'],
$notification->toMail($user)->toArray(),
Mockery::on(function ($closure) {
$message = Mockery::mock(\Illuminate\Mail\Message::class);

$message->shouldReceive('to')->once()->with(['[email protected]']);

$message->shouldReceive('from')->once()->with('[email protected]', 'Jacques Mayol');

$message->shouldReceive('replyTo')->once()->with('[email protected]', 'Jacques Mayol');

$message->shouldReceive('subject')->once()->with('Test Mail Notification');

$message->shouldReceive('setPriority')->once()->with(1);

$closure($message);

return true;
})
);

$user->notify($notification);
}

public function test_mail_is_sent_with_subject()
{
$notification = new TestMailNotificationWithSubject();

$user = NotifiableUser::forceCreate([
'email' => '[email protected]',
]);

$this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');
$this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');

$this->mailer->shouldReceive('send')->once()->with(
['html' => 'htmlContent', 'text' => 'textContent'],
$notification->toMail($user)->toArray(),
Mockery::on(function ($closure) {
$message = Mockery::mock(\Illuminate\Mail\Message::class);

$message->shouldReceive('to')->once()->with(['[email protected]']);

$message->shouldReceive('subject')->once()->with('mail custom subject');

$closure($message);

return true;
})
);

$user->notify($notification);
}

public function test_mail_is_sent_using_mailable()
{
$notification = new TestMailNotificationWithMailable();

$user = NotifiableUser::forceCreate([
'email' => '[email protected]',
]);

$user->notify($notification);
}
}

class NotifiableUser extends Model
{
use Notifiable;

public $table = 'users';
public $timestamps = false;
}

class TestMailNotification extends Notification
{
public function via($notifiable)
{
return [MailChannel::class];
}

public function toMail($notifiable)
{
return (new MailMessage)
->priority(1)
->from('[email protected]', 'Jacques Mayol')
->replyTo('[email protected]', 'Jacques Mayol')
->line('The introduction to the notification.');
}
}

class TestMailNotificationWithSubject extends Notification
{
public function via($notifiable)
{
return [MailChannel::class];
}

public function toMail($notifiable)
{
return (new MailMessage)
->subject('mail custom subject')
->line('The introduction to the notification.');
}
}

class TestMailNotificationWithMailable extends Notification
{
public function via($notifiable)
{
return [MailChannel::class];
}

public function toMail($notifiable)
{
$mailable = Mockery::mock(Mailable::class);

$mailable->shouldReceive('send')->once();

return $mailable;
}
}
Loading