Skip to content

Commit

Permalink
Fixed bug introduced by earlier commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AEM5299 committed Mar 2, 2023
1 parent 18204dc commit f42a877
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 92 deletions.
3 changes: 1 addition & 2 deletions src/Illuminate/Mail/MailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ protected function registerMarkdownRenderer()
$this->app->singleton(Markdown::class, function ($app) {
$config = $app->make('config');

return new Markdown($app->make('view'), [
'theme' => $config->get('mail.markdown.theme', 'default'),
return new Markdown($app->make('view'), $app->make('config'), [
'paths' => $config->get('mail.markdown.paths', []),
]);
});
Expand Down
4 changes: 1 addition & 3 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,10 @@ protected function buildMarkdownView()
{
$markdown = Container::getInstance()->make(Markdown::class);

$markdown->theme($this->theme ?? 'default');

$data = $this->buildViewData();

return [
'html' => $markdown->render($this->markdown, $data),
'html' => $markdown->render($this->markdown, $data, theme: $this->theme),
'text' => $this->buildMarkdownText($markdown, $data),
];
}
Expand Down
45 changes: 12 additions & 33 deletions src/Illuminate/Mail/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Mail;

use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use League\CommonMark\Environment\Environment;
Expand All @@ -21,11 +22,10 @@ class Markdown
protected $view;

/**
* The current theme being used when generating emails.
*
* @var string
* @var \Illuminate\Contracts\Config\Repository
*/
protected $theme = 'default';
protected $config;

/**
* The registered component paths.
Expand All @@ -41,10 +41,10 @@ class Markdown
* @param array $options
* @return void
*/
public function __construct(ViewFactory $view, array $options = [])
public function __construct(ViewFactory $view, ConfigRepository $config, array $options = [])
{
$this->view = $view;
$this->theme = $options['theme'] ?? 'default';
$this->config = $config;
$this->loadComponentsFrom($options['paths'] ?? []);
}

Expand All @@ -56,20 +56,22 @@ public function __construct(ViewFactory $view, array $options = [])
* @param \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles|null $inliner
* @return \Illuminate\Support\HtmlString
*/
public function render($view, array $data = [], $inliner = null)
public function render($view, array $data = [], $inliner = null, ?string $theme = null)
{
$this->view->flushFinderCache();

$theme = $theme ?? $this->config->get('mail.markdown.theme', 'default');

$contents = $this->view->replaceNamespace(
'mail', $this->htmlComponentPaths()
)->make($view, $data)->render();

if ($this->view->exists($customTheme = Str::start($this->theme, 'mail.'))) {
if ($this->view->exists($customTheme = Str::start($theme, 'mail.'))) {
$theme = $customTheme;
} else {
$theme = str_contains($this->theme, '::')
? $this->theme
: 'mail::themes.'.$this->theme;
$theme = str_contains($theme, '::')
? $theme
: 'mail::themes.'.$theme;
}

return new HtmlString(($inliner ?: new CssToInlineStyles)->convert(
Expand Down Expand Up @@ -163,27 +165,4 @@ public function loadComponentsFrom(array $paths = [])
{
$this->componentPaths = $paths;
}

/**
* Set the default theme to be used.
*
* @param string $theme
* @return $this
*/
public function theme($theme)
{
$this->theme = $theme;

return $this;
}

/**
* Get the theme currently being used by the renderer.
*
* @return string
*/
public function getTheme()
{
return $this->theme;
}
}
45 changes: 0 additions & 45 deletions tests/Mail/MailMailableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,51 +439,6 @@ public function testMailableSetsFromCorrectly()
}
}

public function testMailableSetsMarkdownThemeCorrectly()
{
$viewFactory = m::mock(Factory::class);
$viewFactory->shouldReceive('flushFinderCache');
$viewFactory->shouldReceive('replaceNamespace')->andReturnSelf();
$viewFactory->shouldReceive('make')->andReturnSelf();
$viewFactory->shouldReceive('render')->andReturn('<html></html>', 'body {}');
$viewFactory->shouldReceive('exists')->andReturn(true);

Container::getInstance()->instance(Factory::class, $viewFactory);
Container::getInstance()->singleton(Markdown::class);
Container::getInstance()->instance('mailer', new class
{
public function render()
{
//
}
});

(new class() extends Mailable
{
public $theme = 'custom-theme';

public function content()
{
return new Content(
markdown: 'mail.markdown',
);
}
})->render();

$this->assertEquals('custom-theme', Container::getInstance()->make(Markdown::class)->getTheme());

(new class() extends Mailable
{
public function content()
{
return new Content(
markdown: 'mail.markdown',
);
}
})->render();

$this->assertEquals('default', Container::getInstance()->make(Markdown::class)->getTheme());
}

public function testMailableSetsSubjectCorrectly()
{
Expand Down
35 changes: 26 additions & 9 deletions tests/Mail/MailMarkdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@

namespace Illuminate\Tests\Mail;

use Illuminate\Config\Repository;
use Illuminate\Mail\Markdown;
use Illuminate\View\Factory;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class MailMarkdownTest extends TestCase
{
/**
* @var \Illuminate\Config\Repository
*/
protected $configRepository;

protected function setUp(): void
{
$this->configRepository = new Repository([
'mail' => [
'markdown' => [
'theme' => 'default'
]
]
]);

parent::setUp();
}

protected function tearDown(): void
{
m::close();
Expand All @@ -17,7 +36,7 @@ protected function tearDown(): void
public function testRenderFunctionReturnsHtml()
{
$viewFactory = m::mock(Factory::class);
$markdown = new Markdown($viewFactory);
$markdown = new Markdown($viewFactory, $this->configRepository);
$viewFactory->shouldReceive('flushFinderCache')->once();
$viewFactory->shouldReceive('replaceNamespace')->once()->with('mail', $markdown->htmlComponentPaths())->andReturnSelf();
$viewFactory->shouldReceive('exists')->with('mail.default')->andReturn(false);
Expand All @@ -33,41 +52,39 @@ public function testRenderFunctionReturnsHtml()
public function testRenderFunctionReturnsHtmlWithCustomTheme()
{
$viewFactory = m::mock(Factory::class);
$markdown = new Markdown($viewFactory);
$markdown->theme('yaz');
$markdown = new Markdown($viewFactory, $this->configRepository);
$viewFactory->shouldReceive('flushFinderCache')->once();
$viewFactory->shouldReceive('replaceNamespace')->once()->with('mail', $markdown->htmlComponentPaths())->andReturnSelf();
$viewFactory->shouldReceive('exists')->with('mail.yaz')->andReturn(true);
$viewFactory->shouldReceive('make')->with('view', [])->andReturnSelf();
$viewFactory->shouldReceive('make')->with('mail.yaz', [])->andReturnSelf();
$viewFactory->shouldReceive('render')->twice()->andReturn('<html></html>', 'body {}');

$result = $markdown->render('view', []);
$result = $markdown->render('view', [], theme: 'yaz');

$this->assertStringContainsString('<html></html>', $result);
}

public function testRenderFunctionReturnsHtmlWithCustomThemeWithMailPrefix()
{
$viewFactory = m::mock(Factory::class);
$markdown = new Markdown($viewFactory);
$markdown->theme('mail.yaz');
$markdown = new Markdown($viewFactory, $this->configRepository);
$viewFactory->shouldReceive('flushFinderCache')->once();
$viewFactory->shouldReceive('replaceNamespace')->once()->with('mail', $markdown->htmlComponentPaths())->andReturnSelf();
$viewFactory->shouldReceive('exists')->with('mail.yaz')->andReturn(true);
$viewFactory->shouldReceive('make')->with('view', [])->andReturnSelf();
$viewFactory->shouldReceive('make')->with('mail.yaz', [])->andReturnSelf();
$viewFactory->shouldReceive('render')->twice()->andReturn('<html></html>', 'body {}');

$result = $markdown->render('view', []);
$result = $markdown->render('view', [], theme: 'mail.yaz');

$this->assertStringContainsString('<html></html>', $result);
}

public function testRenderTextReturnsText()
{
$viewFactory = m::mock(Factory::class);
$markdown = new Markdown($viewFactory);
$markdown = new Markdown($viewFactory, $this->configRepository);
$viewFactory->shouldReceive('flushFinderCache')->once();
$viewFactory->shouldReceive('replaceNamespace')->once()->with('mail', $markdown->textComponentPaths())->andReturnSelf();
$viewFactory->shouldReceive('make')->with('view', [])->andReturnSelf();
Expand All @@ -81,7 +98,7 @@ public function testRenderTextReturnsText()
public function testParseReturnsParsedMarkdown()
{
$viewFactory = m::mock(Factory::class);
$markdown = new Markdown($viewFactory);
$markdown = new Markdown($viewFactory, $this->configRepository);

$result = $markdown->parse('# Something')->toHtml();

Expand Down

0 comments on commit f42a877

Please sign in to comment.