From 17acf838b416c69a5b2ce402aa2c7ad2e4bffa2e Mon Sep 17 00:00:00 2001 From: Kevin Bui Date: Thu, 31 Oct 2024 01:07:08 +1100 Subject: [PATCH] [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. --- .../Foundation/Console/MailMakeCommand.php | 8 +++ .../Generators/MailMakeCommandTest.php | 51 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/Illuminate/Foundation/Console/MailMakeCommand.php b/src/Illuminate/Foundation/Console/MailMakeCommand.php index 2ec59a15d5a1..d6bf70f799ff 100644 --- a/src/Illuminate/Foundation/Console/MailMakeCommand.php +++ b/src/Illuminate/Foundation/Console/MailMakeCommand.php @@ -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')); @@ -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( diff --git a/tests/Integration/Generators/MailMakeCommandTest.php b/tests/Integration/Generators/MailMakeCommandTest.php index 53091340aa8d..1ca053cb8ff3 100644 --- a/tests/Integration/Generators/MailMakeCommandTest.php +++ b/tests/Integration/Generators/MailMakeCommandTest.php @@ -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), + 'My existing markdown' + ); + $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([ + '', + 'My existing markdown', + '', + ], $existingMarkdownPath); + } + public function testItCanGenerateMailFileWithViewOption() { $this->artisan('make:mail', ['name' => 'FooMail', '--view' => 'foo-mail']) @@ -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), + '
My existing template
' + ); + $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([ + '
My existing template
', + ], $existingViewPath); + } + public function testItCanGenerateMailFileWithTest() { $this->artisan('make:mail', ['name' => 'FooMail', '--test' => true])