Skip to content
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
8 changes: 8 additions & 0 deletions src/Illuminate/Foundation/Console/MailMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ protected function writeMarkdownTemplate()
str_replace('.', '/', $this->getView()).'.blade.php'
);

if ($this->files->exists($path)) {
return $this->components->error(sprintf('%s [%s] already exists.', 'Markdown view', $path));
}

$this->files->ensureDirectoryExists(dirname($path));

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

if ($this->files->exists($path)) {
return $this->components->error(sprintf('%s [%s] already exists.', 'View', $path));
}

$this->files->ensureDirectoryExists(dirname($path));

$stub = str_replace(
Expand Down
51 changes: 51 additions & 0 deletions tests/Integration/Generators/MailMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ public function testItCanGenerateMailFileWithMarkdownOption()
], 'resources/views/foo-mail.blade.php');
}

public function testErrorsWillBeDisplayedWhenMarkdownsAlreadyExist()
{
$existingMarkdownPath = 'resources/views/existing-markdown.blade.php';
$this->app['files']
->put(
$this->app->basePath($existingMarkdownPath),
'<x-mail::message>My existing markdown</x-mail::message>'
);
$this->artisan('make:mail', ['name' => 'FooMail', '--markdown' => 'existing-markdown'])
->expectsOutputToContain('already exists.')
->assertExitCode(0);

$this->assertFileContains([
'namespace App\Mail;',
'use Illuminate\Mail\Mailable;',
'class FooMail extends Mailable',
'return new Content(',
"markdown: 'existing-markdown',",
], 'app/Mail/FooMail.php');
$this->assertFileContains([
'<x-mail::message>',
'My existing markdown',
'</x-mail::message>',
], $existingMarkdownPath);
}

public function testItCanGenerateMailFileWithViewOption()
{
$this->artisan('make:mail', ['name' => 'FooMail', '--view' => 'foo-mail'])
Expand All @@ -63,6 +89,31 @@ public function testItCanGenerateMailFileWithViewOption()
$this->assertFilenameExists('resources/views/foo-mail.blade.php');
}

public function testErrorsWillBeDisplayedWhenViewsAlreadyExist()
{
$existingViewPath = 'resources/views/existing-template.blade.php';
$this->app['files']
->put(
$this->app->basePath($existingViewPath),
'<div>My existing template</div>'
);
$this->artisan('make:mail', ['name' => 'FooMail', '--view' => 'existing-template'])
->expectsOutputToContain('already exists.')
->assertExitCode(0);

$this->assertFileContains([
'namespace App\Mail;',
'use Illuminate\Mail\Mailable;',
'class FooMail extends Mailable',
'return new Content(',
"view: 'existing-template',",
], 'app/Mail/FooMail.php');
$this->assertFilenameExists($existingViewPath);
$this->assertFileContains([
'<div>My existing template</div>',
], $existingViewPath);
}

public function testItCanGenerateMailFileWithTest()
{
$this->artisan('make:mail', ['name' => 'FooMail', '--test' => true])
Expand Down