[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 class SleepTask extends AbstractTask
{ {
/** public function getName(): string
* @return string
*/
public function getName()
{ {
return 'sleep'; return 'sleep';
} }
/** public function getDescription(): string
* @return string
*/
public function getDescription()
{ {
$options = $this->getOptions(); $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 * @throws ErrorException
*/ */
public function execute() public function execute(): bool
{ {
$options = $this->getOptions(); $options = $this->getOptions();
sleep((int) $options['seconds']); sleep(intval($options['seconds']));
return true; return true;
} }
/** /**
* @return array * @return array<string, string|int>
*/ */
protected function getOptions() protected function getOptions(): array
{ {
$options = array_merge( $options = array_merge(
['seconds' => 1], ['seconds' => 1],

View file

@ -12,7 +12,6 @@ namespace Mage\Tests\Task\BuiltIn;
use Mage\Task\Exception\ErrorException; use Mage\Task\Exception\ErrorException;
use Mage\Task\BuiltIn\ExecTask; use Mage\Task\BuiltIn\ExecTask;
use Exception;
use Mage\Tests\Runtime\RuntimeMockup; use Mage\Tests\Runtime\RuntimeMockup;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -118,7 +117,7 @@ class ExecTest extends TestCase
try { try {
$task->execute(); $task->execute();
$this->assertTrue(false, 'Task did not failed'); $this->assertTrue(false, 'Task did not failed');
} catch (Exception $exception) { } catch (\Exception $exception) {
$this->assertTrue($exception instanceof ErrorException); $this->assertTrue($exception instanceof ErrorException);
$this->assertEquals('Parameter "cmd" is not defined', $exception->getMessage()); $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\Task\BuiltIn\SleepTask;
use Mage\Tests\Runtime\RuntimeMockup; use Mage\Tests\Runtime\RuntimeMockup;
use PHPUnit_Framework_TestCase as TestCase; use PHPUnit\Framework\TestCase;
class SleepTaskTest extends TestCase class SleepTaskTest extends TestCase
{ {
public function testCommand() public function testTaskWithDefault()
{ {
$runtime = new RuntimeMockup(); $runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]); $runtime->setConfiguration(['environments' => ['test' => []]]);
@ -28,4 +28,23 @@ class SleepTaskTest extends TestCase
$this->assertSame('[Sleep] Sleeping for 1 second(s)', $task->getDescription()); $this->assertSame('[Sleep] Sleeping for 1 second(s)', $task->getDescription());
$task->execute(); $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);
}
} }