Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Add custom email subject support to scheduled tasks #16598

Closed
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
24 changes: 24 additions & 0 deletions src/Illuminate/Console/Scheduling/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ class Event
*/
public $description;

/**
* Customized email subject of the event.
*
* @var string
*/
public $emailSubject;

/**
* Create a new event instance.
*
Expand Down Expand Up @@ -836,13 +843,30 @@ 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.
*
* @return string
*/
protected function getEmailSubject()
{
if ($this->emailSubject) {
return $this->emailSubject;
}

if ($this->description) {
return 'Scheduled Job Output ('.$this->description.')';
}
Expand Down
114 changes: 80 additions & 34 deletions tests/Console/Scheduling/EventTest.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,98 @@
<?php

use Illuminate\Console\Scheduling\Event;

class EventTest extends PHPUnit_Framework_TestCase
{
public function testBuildCommand()
{
$quote = (DIRECTORY_SEPARATOR == '\\') ? '"' : "'";
namespace {
$mockFileGetContents = null;
}

$event = new Event('php -i');
namespace Illuminate\Console\Scheduling {
use Mockery as m;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Container\Container;

$defaultOutput = (DIRECTORY_SEPARATOR == '\\') ? 'NUL' : '/dev/null';
$this->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);
}
}
}