-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom email subject support to scheduled tasks
- Loading branch information
1 parent
2f19ec5
commit d368b3f
Showing
2 changed files
with
102 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |