From 728f70fe6d2b7b7e579ee4081dae7be4d9d303ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sun, 10 Apr 2022 18:13:59 -0300 Subject: [PATCH] [Galactica] Sleep task tweaks --- src/Task/BuiltIn/SleepTask.php | 22 +++++++--------------- tests/Task/BuiltIn/ExecTaskTest.php | 3 +-- tests/Task/BuiltIn/SleepTaskTest.php | 23 +++++++++++++++++++++-- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/Task/BuiltIn/SleepTask.php b/src/Task/BuiltIn/SleepTask.php index 24c6051..07dfda8 100644 --- a/src/Task/BuiltIn/SleepTask.php +++ b/src/Task/BuiltIn/SleepTask.php @@ -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 */ - protected function getOptions() + protected function getOptions(): array { $options = array_merge( ['seconds' => 1], diff --git a/tests/Task/BuiltIn/ExecTaskTest.php b/tests/Task/BuiltIn/ExecTaskTest.php index a17e78a..930a985 100755 --- a/tests/Task/BuiltIn/ExecTaskTest.php +++ b/tests/Task/BuiltIn/ExecTaskTest.php @@ -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()); } diff --git a/tests/Task/BuiltIn/SleepTaskTest.php b/tests/Task/BuiltIn/SleepTaskTest.php index 26f1756..0349b3c 100644 --- a/tests/Task/BuiltIn/SleepTaskTest.php +++ b/tests/Task/BuiltIn/SleepTaskTest.php @@ -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); + } }