Skip to content

Commit

Permalink
more unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelcontento committed Jan 31, 2013
1 parent 4387bb7 commit a269810
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 64 deletions.
14 changes: 9 additions & 5 deletions src/Jobby/BackgroundJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ class BackgroundJob
/**
* @param string $job
* @param array $config
* @param Helper $helper
*/
public function __construct($job, array $config)
public function __construct($job, array $config, Helper $helper = null)
{
$this->job = $job;
$this->config = $config;
$this->helper = $helper;

$this->helper = new Helper();
if ($this->helper === null) {
$this->helper = new Helper();
}
$this->tmpDir = $this->helper->getTempDir();
}

Expand Down Expand Up @@ -180,11 +184,9 @@ private function runFunction()

if ($retval !== true) {
throw new Exception(
"Closure did not return true.\n" . var_export($retval)
"Closure did not return true.\n" . print_r($retval, true)
);
}

return $retval;
}

/**
Expand Down Expand Up @@ -217,6 +219,7 @@ private function runFile()

// run this file, if executed directly
// @see: http://stackoverflow.com/questions/2413991/php-equivalent-of-pythons-name-main
// @codeCoverageIgnoreStart
if (!debug_backtrace()) {
if (file_exists('vendor/autoload.php')) {
require('vendor/autoload.php');
Expand All @@ -233,3 +236,4 @@ private function runFile()
$job = new BackgroundJob($argv[1], $config);
$job->run();
}
// @codeCoverageIgnoreEnd
11 changes: 5 additions & 6 deletions src/Jobby/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function releaseLock($lockfile)
*/
public function getTempDir()
{
// @codeCoverageIgnoreStart
if (function_exists('sys_get_temp_dir')) {
$tmp = sys_get_temp_dir();
} else if (!empty($_SERVER['TMP'])) {
Expand All @@ -123,6 +124,7 @@ public function getTempDir()
} else {
$tmp = getcwd();
}
// @codeCoverageIgnoreEnd

return $tmp;
}
Expand All @@ -132,12 +134,7 @@ public function getTempDir()
*/
public function getHost()
{
$host = gethostname();
if ($host === false) {
$host = php_uname('n');
}

return $host;
return php_uname('n');
}

/**
Expand All @@ -158,7 +155,9 @@ public function getApplicationEnv()
public function getPlatform()
{
if (strncasecmp(PHP_OS, "Win", 3) == 0) {
// @codeCoverageIgnoreStart
return self::WINDOWS;
// @codeCoverageIgnoreEnd
} else {
return self::UNIX;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Jobby/Jobby.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private function runUnix($job, array $config)
exec("php $command 1> $output 2>&1 &");
}

// @codeCoverageIgnoreStart
/**
* @param string $job
* @param array $config
Expand All @@ -149,6 +150,7 @@ private function runWindows($job, array $config)
$command = $this->getExecutableCommand($job, $config);
pclose(popen("start \"blah\" /B \"php.exe\" $command", "r"));
}
// @codeCoverageIgnoreEnd

/**
* @param string $job
Expand Down
187 changes: 181 additions & 6 deletions tests/Jobby/BackgroundJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace Jobby\Tests;

use Jobby\BackgroundJob;
use Jobby\Helper;

/**
*
* @covers Jobby\BackgroundJob
*/
class BackgroundJobTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -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
);
}
}
20 changes: 20 additions & 0 deletions tests/Jobby/ExceptionTest.php
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);
}
}
Loading

0 comments on commit a269810

Please sign in to comment.