forked from Icinga/icingaweb2-module-director
-
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.
Test: add test suite prototype and command
refs #12905
- Loading branch information
1 parent
468a271
commit 6c23a02
Showing
8 changed files
with
419 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace Icinga\Module\Director\Test; | ||
|
||
use Closure; | ||
|
||
class TestProcess | ||
{ | ||
protected $command; | ||
|
||
protected $identifier; | ||
|
||
protected $exitCode; | ||
|
||
protected $output; | ||
|
||
protected $onSuccess; | ||
|
||
protected $onFailure; | ||
|
||
protected $expectedExitCode = 0; | ||
|
||
public function __construct($command, $identifier = null) | ||
{ | ||
$this->command = $command; | ||
$this->identifier = $identifier; | ||
} | ||
|
||
public function getIdentifier() | ||
{ | ||
return $this->identifier; | ||
} | ||
|
||
public function expectExitCode($code) | ||
{ | ||
$this->expectedExitCode = $code; | ||
return $this; | ||
} | ||
|
||
public function onSuccess($func) | ||
{ | ||
$this->onSuccess = $this->makeClosure($func); | ||
return $this; | ||
} | ||
|
||
public function onFailure($func) | ||
{ | ||
$this->onSuccess = $this->makeClosure($func); | ||
return $this; | ||
} | ||
|
||
protected function makeClosure($func) | ||
{ | ||
if ($func instanceof Closure) { | ||
return $func; | ||
} | ||
|
||
if (is_array($func)) { | ||
return function ($process) use ($func) { | ||
return $func[0]->{$func[1]}($process); | ||
}; | ||
} | ||
} | ||
|
||
public function onFailureThrow($message, $class = 'Exception') | ||
{ | ||
return $this->onFailure(function () use ($message, $class) { | ||
throw new $class($message); | ||
}); | ||
} | ||
|
||
public function run() | ||
{ | ||
exec($this->command, $this->output, $this->exitCode); | ||
|
||
if ($this->succeeded()) { | ||
$this->triggerSuccess(); | ||
} else { | ||
$this->triggerFailure(); | ||
} | ||
} | ||
|
||
public function succeeded() | ||
{ | ||
return $this->exitCode === $this->expectedExitCode; | ||
} | ||
|
||
public function failed() | ||
{ | ||
return $this->exitCode !== $this->expectedExitCode; | ||
} | ||
|
||
protected function triggerSuccess() | ||
{ | ||
if (($func = $this->onSuccess) !== null) { | ||
$func($this); | ||
} | ||
} | ||
|
||
protected function triggerFailure() | ||
{ | ||
if (($func = $this->onFailure) !== null) { | ||
$func($this); | ||
} | ||
} | ||
|
||
public function getExitCode() | ||
{ | ||
return $this->exitCode; | ||
} | ||
|
||
public function getOutput() | ||
{ | ||
return implode("\n", $this->output) . "\n"; | ||
} | ||
} |
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,68 @@ | ||
<?php | ||
|
||
namespace Icinga\Module\Director\Test; | ||
|
||
use Icinga\Application\Icinga; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
|
||
abstract class TestSuite | ||
{ | ||
private $basedir; | ||
|
||
abstract public function run(); | ||
|
||
public static function newTempfile() | ||
{ | ||
return tempnam(sys_get_temp_dir(), 'DirectorTest-'); | ||
} | ||
|
||
public function process($command, $identifier = null) | ||
{ | ||
return new TestProcess($command, $identifier); | ||
} | ||
|
||
protected function filesByExtension($base, $extensions) | ||
{ | ||
$files = array(); | ||
|
||
if (! is_array($extensions)) { | ||
$extensions = array($extensions); | ||
} | ||
|
||
$basedir = $this->getBaseDir() . '/' . $base; | ||
$dir = new RecursiveDirectoryIterator($basedir); | ||
$iterator = new RecursiveIteratorIterator( | ||
$dir, | ||
RecursiveIteratorIterator::SELF_FIRST | ||
); | ||
|
||
foreach ($iterator as $file) { | ||
if (! $file->isFile()) { | ||
continue; | ||
} | ||
|
||
if (in_array($file->getExtension(), $extensions)) { | ||
$files[] = $file->getPathname(); | ||
} | ||
} | ||
|
||
return $files; | ||
} | ||
|
||
public function getBaseDir($file = null) | ||
{ | ||
if ($this->basedir === null) { | ||
$this->basedir = Icinga::app() | ||
->getModuleManager() | ||
->getModule('director') | ||
->getBaseDir(); | ||
} | ||
|
||
if ($file === null) { | ||
return $this->basedir; | ||
} else { | ||
return $this->basedir . '/' . $file; | ||
} | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
|
||
namespace Icinga\Module\Director\Test; | ||
|
||
use Icinga\Application\Logger; | ||
|
||
class TestSuiteLint extends TestSuite | ||
{ | ||
protected $checked; | ||
|
||
protected $failed; | ||
|
||
public function run() | ||
{ | ||
$this->checked = $this->failed = array(); | ||
|
||
foreach ($this->listFiles() as $file) { | ||
$checked[] = $file; | ||
$cmd = "php -l '$file'"; | ||
$this->result[$file] = $this | ||
->process($cmd, $file) | ||
->onFailure(array($this, 'failedCheck')) | ||
->run(); | ||
} | ||
} | ||
|
||
public function failedCheck($process) | ||
{ | ||
Logger::error($process->getOutput()); | ||
$this->failed[] = $process->getIdentifier(); | ||
} | ||
|
||
public function hasFailures() | ||
{ | ||
return ! empty($this->failed); | ||
} | ||
|
||
protected function listFiles() | ||
{ | ||
$basedir = $this->getBaseDir(); | ||
$files = array( | ||
$basedir . '/run.php', | ||
$basedir . '/configuration.php' | ||
); | ||
|
||
foreach ($this->filesByExtension('library/Director', 'php') as $file) { | ||
$files[] = $file; | ||
} | ||
|
||
foreach ($this->filesByExtension('application', array('php', 'phtml')) as $file) { | ||
$files[] = $file; | ||
} | ||
|
||
return $files; | ||
} | ||
} |
Oops, something went wrong.