[Galactica] Sleep task tweaks

This commit is contained in:
Andrés Montañez 2022-04-10 18:13:59 -03:00
parent 2e04ed9fe1
commit 728f70fe6d
No known key found for this signature in database
GPG Key ID: 97E9F675F4C03DE2
3 changed files with 29 additions and 19 deletions

View File

@ -21,42 +21,34 @@ use Mage\Task\AbstractTask;
*/
class SleepTask extends AbstractTask
{
/**
* @return string
*/
public function getName()
public function getName(): string
{
return 'sleep';
}
/**
* @return string
*/
public function getDescription()
public function getDescription(): string
{
$options = $this->getOptions();
return sprintf('[Sleep] Sleeping for %s second(s)', (int) $options['seconds']);
return sprintf('[Sleep] Sleeping for %d second(s)', $options['seconds']);
}
/**
* @return bool
*
* @throws ErrorException
*/
public function execute()
public function execute(): bool
{
$options = $this->getOptions();
sleep((int) $options['seconds']);
sleep(intval($options['seconds']));
return true;
}
/**
* @return array
* @return array<string, string|int>
*/
protected function getOptions()
protected function getOptions(): array
{
$options = array_merge(
['seconds' => 1],

View File

@ -12,7 +12,6 @@ namespace Mage\Tests\Task\BuiltIn;
use Mage\Task\Exception\ErrorException;
use Mage\Task\BuiltIn\ExecTask;
use Exception;
use Mage\Tests\Runtime\RuntimeMockup;
use PHPUnit\Framework\TestCase;
@ -118,7 +117,7 @@ class ExecTest extends TestCase
try {
$task->execute();
$this->assertTrue(false, 'Task did not failed');
} catch (Exception $exception) {
} catch (\Exception $exception) {
$this->assertTrue($exception instanceof ErrorException);
$this->assertEquals('Parameter "cmd" is not defined', $exception->getMessage());
}

View File

@ -12,11 +12,11 @@ namespace Mage\Tests\Task\BuiltIn;
use Mage\Task\BuiltIn\SleepTask;
use Mage\Tests\Runtime\RuntimeMockup;
use PHPUnit_Framework_TestCase as TestCase;
use PHPUnit\Framework\TestCase;
class SleepTaskTest extends TestCase
{
public function testCommand()
public function testTaskWithDefault()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
@ -28,4 +28,23 @@ class SleepTaskTest extends TestCase
$this->assertSame('[Sleep] Sleeping for 1 second(s)', $task->getDescription());
$task->execute();
}
public function testTaskWithValue()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new SleepTask();
$task->setOptions(['seconds' => 2]);
$task->setRuntime($runtime);
$this->assertSame('[Sleep] Sleeping for 2 second(s)', $task->getDescription());
$startedAt = microtime(true);
$task->execute();
$finishedAt = microtime(true);
$this->assertGreaterThanOrEqual(2, $finishedAt - $startedAt);
}
}