forked from jobbyphp/jobby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4387bb7
commit a269810
Showing
7 changed files
with
246 additions
and
64 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
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 |
---|---|---|
|
@@ -3,9 +3,10 @@ | |
namespace Jobby\Tests; | ||
|
||
use Jobby\BackgroundJob; | ||
use Jobby\Helper; | ||
|
||
/** | ||
* | ||
* @covers Jobby\BackgroundJob | ||
*/ | ||
class BackgroundJobTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
@@ -40,16 +41,190 @@ public function tearDown() | |
} | ||
|
||
/** | ||
* | ||
* @param array $config | ||
* @param Helper $helper | ||
*/ | ||
private function runJob(array $config, Helper $helper = null) | ||
{ | ||
$config = $this->getJobConfig($config); | ||
|
||
$job = new BackgroundJob("name", $config, $helper); | ||
$job->run(); | ||
} | ||
|
||
/** | ||
* @param array $config | ||
* @return array | ||
*/ | ||
private function getJobConfig(array $config) | ||
{ | ||
$helper = new Helper(); | ||
|
||
if ($config['command'] instanceof \Closure) { | ||
$config['command'] = $helper->closureToString($config['command']); | ||
} | ||
|
||
return array_merge( | ||
array( | ||
"enabled" => 1, | ||
"runOnHost" => $helper->getHost(), | ||
"dateFormat" => "Y-m-d H:i:s", | ||
"schedule" => "* * * * *", | ||
"output" => $this->logFile | ||
), | ||
$config | ||
); | ||
} | ||
|
||
/** | ||
* @covers Jobby\BackgroundJob::run | ||
*/ | ||
public function testShouldNotRunIfNotEnabled() | ||
{ | ||
$job = new BackgroundJob("name", array( | ||
"enabled" => false, | ||
"output" => $this->logFile | ||
$this->runJob(array( | ||
"command" => function() { echo "test"; return true; }, | ||
"enabled" => false | ||
)); | ||
|
||
$this->assertEquals("", $this->getLogContent()); | ||
} | ||
|
||
/** | ||
* @covers Jobby\BackgroundJob::run | ||
*/ | ||
public function testShouldNotRunIfNotDue() | ||
{ | ||
$this->runJob(array( | ||
"command" => function() { echo "test"; return true; }, | ||
"schedule" => "0 0 1 1 *" | ||
)); | ||
|
||
$this->assertEquals("", $this->getLogContent()); | ||
} | ||
|
||
/** | ||
* @covers Jobby\BackgroundJob::run | ||
*/ | ||
public function testShouldNotRunOnWrongHost() | ||
{ | ||
$this->runJob(array( | ||
"command" => function() { echo "test"; return true; }, | ||
"runOnHost" => "something that does not match" | ||
)); | ||
$job->run(); | ||
|
||
$this->assertEquals("", $this->getLogContent()); | ||
} | ||
|
||
/** | ||
* @covers Jobby\BackgroundJob::run | ||
*/ | ||
public function testShouldRunAsCurrentUser() | ||
{ | ||
$this->runJob(array( | ||
"command" => function() { echo posix_getuid(); return true; } | ||
)); | ||
|
||
$this->assertEquals(posix_getuid(), $this->getLogContent()); | ||
} | ||
|
||
/** | ||
* @covers Jobby\BackgroundJob::runFile | ||
*/ | ||
public function testInvalidCommand() | ||
{ | ||
$this->runJob(array("command" => "invalid-command")); | ||
|
||
$this->assertContains("invalid-command", $this->getLogContent()); | ||
$this->assertContains("not found", $this->getLogContent()); | ||
$this->assertContains( | ||
"ERROR: Job exited with status '127'", | ||
$this->getLogContent() | ||
); | ||
} | ||
|
||
/** | ||
* @covers Jobby\BackgroundJob::runFunction | ||
*/ | ||
public function testClosureNotReturnTrue() | ||
{ | ||
$this->runJob(array("command" => function() { return false; })); | ||
|
||
$this->assertContains( | ||
'ERROR: Closure did not return true.', | ||
$this->getLogContent() | ||
); | ||
} | ||
|
||
/** | ||
* @covers Jobby\BackgroundJob::getLogFile | ||
*/ | ||
public function testHideStdOutByDefault() | ||
{ | ||
ob_start(); | ||
$this->runJob(array( | ||
"command" => function() { echo "foo bar"; }, | ||
"output" => null | ||
)); | ||
$content = ob_get_contents(); | ||
ob_end_clean(); | ||
|
||
$this->assertEquals("", $content); | ||
} | ||
|
||
/** | ||
* @covers Jobby\BackgroundJob::getLogFile | ||
*/ | ||
public function testShouldCreateLogFolder() | ||
{ | ||
$logfile = dirname($this->logFile) . "/foo/bar.log"; | ||
$this->runJob(array( | ||
"command" => function() { echo "foo bar"; }, | ||
"output" => $logfile | ||
)); | ||
|
||
$dirExists = file_exists(dirname($logfile)); | ||
$isDir = is_dir(dirname($logfile)); | ||
|
||
unlink($logfile); | ||
rmdir(dirname($logfile)); | ||
|
||
$this->assertTrue($dirExists); | ||
$this->assertTrue($isDir); | ||
} | ||
|
||
/** | ||
* @covers Jobby\BackgroundJob::mail | ||
*/ | ||
public function testNotSendMailOnMissingRecipients() | ||
{ | ||
$helper = $this->getMock("Jobby\Helper", array("sendMail")); | ||
$helper->expects($this->never()) | ||
->method("sendMail"); | ||
|
||
$this->runJob( | ||
array( | ||
"command" => function() { return false; }, | ||
"recipients" => "" | ||
), | ||
$helper | ||
); | ||
} | ||
|
||
/** | ||
* @covers Jobby\BackgroundJob::mail | ||
*/ | ||
public function testMailShoudTriggerHelper() | ||
{ | ||
$helper = $this->getMock("Jobby\Helper", array("sendMail")); | ||
$helper->expects($this->once()) | ||
->method("sendMail"); | ||
|
||
$this->runJob( | ||
array( | ||
"command" => function() { return false; }, | ||
"recipients" => "[email protected]" | ||
), | ||
$helper | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Jobby\Tests; | ||
|
||
use Jobby\Exception; | ||
|
||
/** | ||
* @covers Jobby\Exception | ||
*/ | ||
class ExceptionTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* | ||
*/ | ||
public function testInheritsBaseException() | ||
{ | ||
$e = new Exception(); | ||
$this->assertTrue($e instanceof \Exception); | ||
} | ||
} |
Oops, something went wrong.