Skip to content

Commit

Permalink
Add custom email subject support to scheduled tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenmaguire committed Nov 30, 2016
1 parent 2f19ec5 commit d368b3f
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 34 deletions.
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
112 changes: 78 additions & 34 deletions tests/Console/Scheduling/EventTest.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,96 @@
<?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();
}

$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;
}));
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);
}
}
}

0 comments on commit d368b3f

Please sign in to comment.