diff --git a/src/Task/BuiltIn/SleepTask.php b/src/Task/BuiltIn/SleepTask.php new file mode 100644 index 0000000..24c6051 --- /dev/null +++ b/src/Task/BuiltIn/SleepTask.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Task\BuiltIn; + +use Mage\Task\Exception\ErrorException; +use Mage\Task\AbstractTask; + +/** + * Sleep task. Sleeps for a given number of seconds so you can delay task + * execution. + * + * @author Yanick Witschi + */ +class SleepTask extends AbstractTask +{ + /** + * @return string + */ + public function getName() + { + return 'sleep'; + } + + /** + * @return string + */ + public function getDescription() + { + $options = $this->getOptions(); + + return sprintf('[Sleep] Sleeping for %s second(s)', (int) $options['seconds']); + } + + /** + * @return bool + * + * @throws ErrorException + */ + public function execute() + { + $options = $this->getOptions(); + + sleep((int) $options['seconds']); + + return true; + } + + /** + * @return array + */ + protected function getOptions() + { + $options = array_merge( + ['seconds' => 1], + $this->options + ); + + return $options; + } +} diff --git a/tests/Task/BuiltIn/SleepTaskTest.php b/tests/Task/BuiltIn/SleepTaskTest.php new file mode 100644 index 0000000..26f1756 --- /dev/null +++ b/tests/Task/BuiltIn/SleepTaskTest.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Tests\Task\BuiltIn; + +use Mage\Task\BuiltIn\SleepTask; +use Mage\Tests\Runtime\RuntimeMockup; +use PHPUnit_Framework_TestCase as TestCase; + +class SleepTaskTest extends TestCase +{ + public function testCommand() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new SleepTask(); + $task->setRuntime($runtime); + + $this->assertSame('[Sleep] Sleeping for 1 second(s)', $task->getDescription()); + $task->execute(); + } +}