Skip to content

Commit 17acf83

Browse files
authored
[11.x] Pick up existing views and markdowns when creating mails (#53308)
* pick up the existing mail template when creating new mails. * Existing markdowns will be picked up when creating emails. * display error messages when specified markdowns or views already exist. * update the test cases for the make:mail command when markdowns and views already exist. * rename a test case for the make:mail command.
1 parent bdedd46 commit 17acf83

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/Illuminate/Foundation/Console/MailMakeCommand.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ protected function writeMarkdownTemplate()
7070
str_replace('.', '/', $this->getView()).'.blade.php'
7171
);
7272

73+
if ($this->files->exists($path)) {
74+
return $this->components->error(sprintf('%s [%s] already exists.', 'Markdown view', $path));
75+
}
76+
7377
$this->files->ensureDirectoryExists(dirname($path));
7478

7579
$this->files->put($path, file_get_contents(__DIR__.'/stubs/markdown.stub'));
@@ -88,6 +92,10 @@ protected function writeView()
8892
str_replace('.', '/', $this->getView()).'.blade.php'
8993
);
9094

95+
if ($this->files->exists($path)) {
96+
return $this->components->error(sprintf('%s [%s] already exists.', 'View', $path));
97+
}
98+
9199
$this->files->ensureDirectoryExists(dirname($path));
92100

93101
$stub = str_replace(

tests/Integration/Generators/MailMakeCommandTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,32 @@ public function testItCanGenerateMailFileWithMarkdownOption()
4747
], 'resources/views/foo-mail.blade.php');
4848
}
4949

50+
public function testErrorsWillBeDisplayedWhenMarkdownsAlreadyExist()
51+
{
52+
$existingMarkdownPath = 'resources/views/existing-markdown.blade.php';
53+
$this->app['files']
54+
->put(
55+
$this->app->basePath($existingMarkdownPath),
56+
'<x-mail::message>My existing markdown</x-mail::message>'
57+
);
58+
$this->artisan('make:mail', ['name' => 'FooMail', '--markdown' => 'existing-markdown'])
59+
->expectsOutputToContain('already exists.')
60+
->assertExitCode(0);
61+
62+
$this->assertFileContains([
63+
'namespace App\Mail;',
64+
'use Illuminate\Mail\Mailable;',
65+
'class FooMail extends Mailable',
66+
'return new Content(',
67+
"markdown: 'existing-markdown',",
68+
], 'app/Mail/FooMail.php');
69+
$this->assertFileContains([
70+
'<x-mail::message>',
71+
'My existing markdown',
72+
'</x-mail::message>',
73+
], $existingMarkdownPath);
74+
}
75+
5076
public function testItCanGenerateMailFileWithViewOption()
5177
{
5278
$this->artisan('make:mail', ['name' => 'FooMail', '--view' => 'foo-mail'])
@@ -63,6 +89,31 @@ public function testItCanGenerateMailFileWithViewOption()
6389
$this->assertFilenameExists('resources/views/foo-mail.blade.php');
6490
}
6591

92+
public function testErrorsWillBeDisplayedWhenViewsAlreadyExist()
93+
{
94+
$existingViewPath = 'resources/views/existing-template.blade.php';
95+
$this->app['files']
96+
->put(
97+
$this->app->basePath($existingViewPath),
98+
'<div>My existing template</div>'
99+
);
100+
$this->artisan('make:mail', ['name' => 'FooMail', '--view' => 'existing-template'])
101+
->expectsOutputToContain('already exists.')
102+
->assertExitCode(0);
103+
104+
$this->assertFileContains([
105+
'namespace App\Mail;',
106+
'use Illuminate\Mail\Mailable;',
107+
'class FooMail extends Mailable',
108+
'return new Content(',
109+
"view: 'existing-template',",
110+
], 'app/Mail/FooMail.php');
111+
$this->assertFilenameExists($existingViewPath);
112+
$this->assertFileContains([
113+
'<div>My existing template</div>',
114+
], $existingViewPath);
115+
}
116+
66117
public function testItCanGenerateMailFileWithTest()
67118
{
68119
$this->artisan('make:mail', ['name' => 'FooMail', '--test' => true])

0 commit comments

Comments
 (0)