From d599861b3cbb35dcc969958db6ab5ec4d29337e5 Mon Sep 17 00:00:00 2001 From: Steven Maguire Date: Wed, 30 Nov 2016 00:01:35 -0600 Subject: [PATCH] Add custom email subject support to scheduled tasks --- src/Illuminate/Console/Scheduling/Event.php | 24 +++++ tests/Console/Scheduling/EventTest.php | 114 ++++++++++++++------ 2 files changed, 104 insertions(+), 34 deletions(-) diff --git a/src/Illuminate/Console/Scheduling/Event.php b/src/Illuminate/Console/Scheduling/Event.php index 1805d3f2928e..7e858ef3b0fa 100644 --- a/src/Illuminate/Console/Scheduling/Event.php +++ b/src/Illuminate/Console/Scheduling/Event.php @@ -119,6 +119,13 @@ class Event */ public $description; + /** + * Customized email subject of the event. + * + * @var string + */ + public $emailSubject; + /** * Create a new event instance. * @@ -836,6 +843,19 @@ protected function emailOutput(Mailer $mailer, $addresses, $onlyIfOutputExists = }); } + /** + * Set the customized email subject of the event. + * + * @param string $subject + * @return $this + */ + public function emailSubject($subject) + { + $this->emailSubject = $subject; + + return $this; + } + /** * Get the e-mail subject line for output results. * @@ -843,6 +863,10 @@ protected function emailOutput(Mailer $mailer, $addresses, $onlyIfOutputExists = */ protected function getEmailSubject() { + if ($this->emailSubject) { + return $this->emailSubject; + } + if ($this->description) { return 'Scheduled Job Output ('.$this->description.')'; } diff --git a/tests/Console/Scheduling/EventTest.php b/tests/Console/Scheduling/EventTest.php index 2787db337e68..0d3bad776395 100644 --- a/tests/Console/Scheduling/EventTest.php +++ b/tests/Console/Scheduling/EventTest.php @@ -1,52 +1,98 @@ assertSame("php -i > {$quote}{$defaultOutput}{$quote} 2>&1 &", $event->buildCommand()); + function file_get_contents() + { + global $mockFileGetContents; + if (isset($mockFileGetContents) && ! is_null($mockFileGetContents)) { + return $mockFileGetContents; + } else { + return call_user_func_array('\file_get_contents', func_get_args()); + } } - public function testBuildCommandSendOutputTo() + class EventTest extends \PHPUnit_Framework_TestCase { - $quote = (DIRECTORY_SEPARATOR == '\\') ? '"' : "'"; + public function testBuildCommand() + { + $quote = (DIRECTORY_SEPARATOR == '\\') ? '"' : "'"; - $event = new Event('php -i'); + $event = new Event('php -i'); - $event->sendOutputTo('/dev/null'); - $this->assertSame("php -i > {$quote}/dev/null{$quote} 2>&1 &", $event->buildCommand()); + $defaultOutput = (DIRECTORY_SEPARATOR == '\\') ? 'NUL' : '/dev/null'; + $this->assertSame("php -i > {$quote}{$defaultOutput}{$quote} 2>&1 &", $event->buildCommand()); + } - $event = new Event('php -i'); + public function testBuildCommandSendOutputTo() + { + $quote = (DIRECTORY_SEPARATOR == '\\') ? '"' : "'"; - $event->sendOutputTo('/my folder/foo.log'); - $this->assertSame("php -i > {$quote}/my folder/foo.log{$quote} 2>&1 &", $event->buildCommand()); - } + $event = new Event('php -i'); - public function testBuildCommandAppendOutput() - { - $quote = (DIRECTORY_SEPARATOR == '\\') ? '"' : "'"; + $event->sendOutputTo('/dev/null'); + $this->assertSame("php -i > {$quote}/dev/null{$quote} 2>&1 &", $event->buildCommand()); - $event = new Event('php -i'); + $event = new Event('php -i'); - $event->appendOutputTo('/dev/null'); - $this->assertSame("php -i >> {$quote}/dev/null{$quote} 2>&1 &", $event->buildCommand()); - } + $event->sendOutputTo('/my folder/foo.log'); + $this->assertSame("php -i > {$quote}/my folder/foo.log{$quote} 2>&1 &", $event->buildCommand()); + } - /** - * @expectedException LogicException - */ - public function testEmailOutputToThrowsExceptionIfOutputFileWasNotSpecified() - { - $event = new Event('php -i'); - $event->emailOutputTo('foo@example.com'); + public function testBuildCommandAppendOutput() + { + $quote = (DIRECTORY_SEPARATOR == '\\') ? '"' : "'"; + + $event = new Event('php -i'); + + $event->appendOutputTo('/dev/null'); + $this->assertSame("php -i >> {$quote}/dev/null{$quote} 2>&1 &", $event->buildCommand()); + } + + /** + * @expectedException LogicException + */ + public function testEmailOutputToThrowsExceptionIfOutputFileWasNotSpecified() + { + $event = new Event('php -i'); + $event->emailOutputTo('foo@example.com'); + + $event->buildCommand(); + } + + public function testBuildEmailOutputToSubject() + { + global $mockFileGetContents; + $mockFileGetContents = 'test output'; + $address = 'foo@example.com'; + $container = new Container; + $resolveMailer = function ($subject = 'Scheduled Job Output') use ($address) { + return function () use ($address, $subject) { + $mailer = m::mock('Illuminate\Mail\Mailer[sendSwiftMessage]', [m::mock('Illuminate\Contracts\View\Factory'), m::mock('Swift_Mailer')])->shouldAllowMockingProtectedMethods(); + $mailer->shouldReceive('sendSwiftMessage')->with(m::on(function ($message) use ($address, $subject) { + $this->assertInstanceOf('Swift_Message', $message); + $this->assertSame([$address => null], $message->getTo()); + $this->assertContains($subject, $message->getSubject()); + + return true; + })); - $event->buildCommand(); + return $mailer; + }; + }; + $event = new Event(m::mock('Illuminate\Contracts\Cache\Repository'), 'php -i'); + $container->bind('Illuminate\Contracts\Mail\Mailer', $resolveMailer()); + $event->sendOutputTo('/my folder/foo.log')->emailOutputTo($address)->run($container); + $subject = 'custom subject'; + $container->bind('Illuminate\Contracts\Mail\Mailer', $resolveMailer($subject)); + $event->sendOutputTo('/my folder/foo.log')->emailSubject($subject)->emailOutputTo($address)->run($container); + } } }