diff --git a/app/middleware/CoolMiddleware.php b/app/middleware/CoolMiddleware.php new file mode 100644 index 000000000..d6a7056e3 --- /dev/null +++ b/app/middleware/CoolMiddleware.php @@ -0,0 +1,50 @@ + + *
  • one-group
  • + *
  • two-group
  • + *
  • global
  • + * + */ +class CoolMiddleware extends AbstractMiddleware { + /** + * Creates new instance of the class. + */ + public function __construct() { + parent::__construct('Super Cool Middleware'); + $this->setPriority(100); + $this->addToGroups([ + 'one-group', + 'two-group', + ]); + } + /** + * Execute a set of instructions before accessing the application. + */ + public function before(Request $request, Response $response) { + //TODO: Implement the action to perform before processing the request. + } + /** + * Execute a set of instructions after processing the request and before sending back the response. + */ + public function after(Request $request, Response $response) { + //TODO: Implement the action to perform after processing the request. + } + /** + * Execute a set of instructions after sending the response. + */ + public function afterSend(Request $request, Response $response) { + //TODO: Implement the action to perform after sending the request. + } +} diff --git a/app/tasks/Fail1TestTask.php b/app/tasks/Fail1TestTask.php index 3b5ca2c0e..ab1869dd2 100644 --- a/app/tasks/Fail1TestTask.php +++ b/app/tasks/Fail1TestTask.php @@ -2,6 +2,7 @@ namespace app\tasks; use webfiori\framework\scheduler\AbstractTask; +use webfiori\framework\scheduler\TasksManager; /** * A background process which was created using the command "create". * @@ -40,12 +41,14 @@ public function afterExec() { * Execute the process. */ public function execute() { + TasksManager::logInfo('Task '.$this->getTaskName().' Is executing...'); return false; } /** * Execute a set of instructions when the job failed to complete without errors. */ public function onFail() { + TasksManager::logErr('Task '.$this->getTaskName().' Failed.'); } /** * Execute a set of instructions when the job completed without errors. diff --git a/app/tasks/SuccessTestEveryMinute.php b/app/tasks/SuccessTestEveryMinute.php index b4a1ebc52..712fa5a7e 100644 --- a/app/tasks/SuccessTestEveryMinute.php +++ b/app/tasks/SuccessTestEveryMinute.php @@ -2,6 +2,7 @@ namespace app\tasks; use webfiori\framework\scheduler\AbstractTask; +use webfiori\framework\scheduler\TasksManager; /** * A background process which was created using the command "create". * @@ -49,6 +50,7 @@ public function afterExec() { * Execute the process. */ public function execute() { + TasksManager::logErr('Task '.$this->getTaskName().' Successfully completed.'); //TODO: Write the code that represents the process. } /** diff --git a/tests/webfiori/framework/test/cli/CreateTaskTest.php b/tests/webfiori/framework/test/cli/CreateTaskTest.php index c2dcfa595..c31f0ff32 100644 --- a/tests/webfiori/framework/test/cli/CreateTaskTest.php +++ b/tests/webfiori/framework/test/cli/CreateTaskTest.php @@ -182,6 +182,8 @@ public function test03() { 'end', 'End date of the report.', '2021-07-07', + 'y', + '', 'n' ]); @@ -203,6 +205,9 @@ public function test03() { "Describe the use of the argument: Enter = ''\n", "Default value: Enter = ''\n", "Would you like to add more arguments?(y/N)\n", + "Enter argument name:\n", + "Error: Invalid argument name: \n", + "Would you like to add more arguments?(y/N)\n", "Info: New class was created at \"".ROOT_PATH.DS.'app'.DS."tasks\".\n", ], $runner->getOutput()); $clazz = '\\app\\tasks\\SendDailyReportTask'; diff --git a/tests/webfiori/framework/test/cli/SchedulerCommandTest.php b/tests/webfiori/framework/test/cli/SchedulerCommandTest.php index 8c9063486..605c1bd3f 100644 --- a/tests/webfiori/framework/test/cli/SchedulerCommandTest.php +++ b/tests/webfiori/framework/test/cli/SchedulerCommandTest.php @@ -125,7 +125,9 @@ public function test04() { "Forcing task 'Fail 1' to execute...\n", "Active task: \"Fail 1\" ...\n", "Calling the method app\\tasks\Fail1TestTask::execute()\n", + "Info: Task Fail 1 Is executing...\n", "Calling the method app\\tasks\Fail1TestTask::onFail()\n", + "Error: Task Fail 1 Failed.\n", "Calling the method app\\tasks\Fail1TestTask::afterExec()\n", "Check finished.\n", "Total number of tasks: 5\n", @@ -170,21 +172,22 @@ public function test05() { "Thrown in: Fail2TestTask\n", "Line: 44\n", "Stack Trace:\n", - "#0 At class app\\tasks\Fail2TestTask line 1083\n", - "#1 At class webfiori\\framework\scheduler\AbstractTask line 406\n", - "#2 At class webfiori\\framework\scheduler\AbstractTask line 903\n", - "#3 At class webfiori\\framework\scheduler\TasksManager line 625\n", - "#4 At class webfiori\\framework\scheduler\TasksManager line 135\n", - "#5 At class webfiori\\framework\cli\commands\SchedulerCommand line 86\n", - "#6 At class webfiori\\framework\cli\commands\SchedulerCommand line 328\n", - "#7 At class webfiori\\cli\CLICommand line 409\n", - "#8 At class webfiori\\cli\Runner line 684\n", - "#9 At class webfiori\\cli\Runner line 615\n", - "#10 At class webfiori\cli\Runner line 154\n", + "#0 At class app\\tasks\Fail2TestTask line 44\n", + "#1 At class webfiori\\framework\scheduler\AbstractTask line 1097\n", + "#2 At class webfiori\\framework\scheduler\AbstractTask line 418\n", + "#3 At class webfiori\\framework\scheduler\AbstractTask line 951\n", + "#4 At class webfiori\\framework\scheduler\TasksManager line 673\n", + "#5 At class webfiori\\framework\scheduler\TasksManager line 139\n", + "#6 At class webfiori\\framework\cli\commands\SchedulerCommand line 86\n", + "#7 At class webfiori\\framework\cli\commands\SchedulerCommand line 328\n", + "#8 At class webfiori\\cli\CLICommand line 409\n", + "#9 At class webfiori\\cli\Runner line 684\n", + "#10 At class webfiori\\cli\Runner line 615\n", + "#11 At class webfiori\cli\Runner line 156\n", "Skip"]; $actual = $runner->getOutput(); $idx = 0; - + foreach ($expected as $item) { if ($item == 'Skip') { break; @@ -444,4 +447,50 @@ public function test13() { "Check finished.", ], TasksManager::getLogArray()); } + /** + * @test + */ + public function test14() { + $runner = App::getRunner(); + $runner->setInputs([ + '5' + ]); + $runner->setArgsVector([ + 'webfiori', + 'scheduler', + '--force', + 'p' => '123456' + ]); + $this->assertEquals(0, $runner->start()); + $this->assertEquals([ + "Select one of the scheduled tasks to force:\n", + "0: Fail 1\n", + "1: Fail 2\n", + "2: Fail 3\n", + "3: Success Every Minute\n", + "4: Success 1\n", + "5: Cancel <--\n", + ], $runner->getOutput()); + } + /** + * @test + */ + public function test15() { + $runner = App::getRunner(); + $runner->setInputs([ + 'Hell', + '5' + ]); + $runner->setArgsVector([ + 'webfiori', + 'scheduler', + '--force', + '--task-name="Rand"', + 'p' => '123456' + ]); + $this->assertEquals(-1, $runner->start()); + $this->assertEquals([ + "Error: No task was found which has the name 'Rand'\n", + ], $runner->getOutput()); + } } diff --git a/tests/webfiori/framework/test/router/RouterUriTest.php b/tests/webfiori/framework/test/router/RouterUriTest.php index 6b769ff9f..9aba8465e 100644 --- a/tests/webfiori/framework/test/router/RouterUriTest.php +++ b/tests/webfiori/framework/test/router/RouterUriTest.php @@ -18,6 +18,8 @@ public function testAddToMiddleware00() { \webfiori\framework\middleware\MiddlewareManager::register(new \TestMiddleware()); $uri->addMiddleware('global'); $this->assertEquals(1, $uri->getMiddleware()->size()); + $uri->addMiddleware('Super Cool Middleware'); + $this->assertEquals(2, $uri->getMiddleware()->size()); $this->assertFalse($uri->isDynamic()); } /** diff --git a/tests/webfiori/framework/test/scheduler/SchedulerTaskTest.php b/tests/webfiori/framework/test/scheduler/SchedulerTaskTest.php index e6711b90f..a8af8e48c 100644 --- a/tests/webfiori/framework/test/scheduler/SchedulerTaskTest.php +++ b/tests/webfiori/framework/test/scheduler/SchedulerTaskTest.php @@ -1,6 +1,7 @@ expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'\'.'); $task = new BaseTask(''); } @@ -155,7 +156,7 @@ public function testConstructor04() { * @test */ public function testConstructor05() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'0-5,7,15,60 0-4,8 * * *\'.'); $task = new BaseTask('0-5,7,15,60 0-4,8 * * *'); } @@ -163,7 +164,7 @@ public function testConstructor05() { * @test */ public function testConstructor06() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'0-5,7,15 0-4,24,8 * * *\'.'); $task = new BaseTask('0-5,7,15 0-4,24,8 * * *'); } @@ -202,7 +203,7 @@ public function testConstructor07() { * @test */ public function testConstructor08() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'*/15,*/20,30-35 */2 */3,6,9 jan-mar 0,mon,3-6\'.'); $task = new BaseTask('*/15,*/20,30-35 */2 */3,6,9 jan-mar 0,mon,3-6'); } @@ -210,7 +211,7 @@ public function testConstructor08() { * @test */ public function testConstructor09() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'*/15,*/20,30-35 */2 * */3 0,mon,3-6\'.'); $task = new BaseTask('*/15,*/20,30-35 */2 * */3 0,mon,3-6'); } @@ -218,7 +219,7 @@ public function testConstructor09() { * @test */ public function testConstructor10() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'*/15,*/20,30-35 */2 * * */3\'.'); $task = new BaseTask('*/15,*/20,30-35 */2 * * */3'); } @@ -226,7 +227,7 @@ public function testConstructor10() { * @test */ public function testConstructor11() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'5,a * * * *\''); $task = new BaseTask('5,a * * * *'); } @@ -234,7 +235,7 @@ public function testConstructor11() { * @test */ public function testConstructor12() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'5, * * * *\'.'); $task = new BaseTask('5, * * * *'); } @@ -242,7 +243,7 @@ public function testConstructor12() { * @test */ public function testConstructor13() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'5-0 * * * *\'.'); $task = new BaseTask('5-0 * * * *'); } @@ -250,7 +251,7 @@ public function testConstructor13() { * @test */ public function testConstructor14() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'60-60 * * * *\'.'); $task = new BaseTask('60-60 * * * *'); } @@ -258,7 +259,7 @@ public function testConstructor14() { * @test */ public function testConstructor15() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'5-60 * * * *\'.'); $task = new BaseTask('5-60 * * * *'); } @@ -266,7 +267,7 @@ public function testConstructor15() { * @test */ public function testConstructor16() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* 15,a * * *\''); $task = new BaseTask('* 15,a * * *'); } @@ -274,7 +275,7 @@ public function testConstructor16() { * @test */ public function testConstructor17() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* 7, * * *\'.'); $task = new BaseTask('* 7, * * *'); } @@ -282,7 +283,7 @@ public function testConstructor17() { * @test */ public function testConstructor18() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* 23-9 * * *\'.'); $task = new BaseTask('* 23-9 * * *'); } @@ -290,7 +291,7 @@ public function testConstructor18() { * @test */ public function testConstructor19() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'24-24 * * * *\'.'); $task = new BaseTask('24-24 * * * *'); } @@ -298,7 +299,7 @@ public function testConstructor19() { * @test */ public function testConstructor20() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* 5-24 * * *\'.'); $task = new BaseTask('* 5-24 * * *'); } @@ -306,7 +307,7 @@ public function testConstructor20() { * @test */ public function testConstructor21() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * 1-32 * *\'.'); $task = new BaseTask('* * 1-32 * *'); } @@ -314,7 +315,7 @@ public function testConstructor21() { * @test */ public function testConstructor22() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * 0-30 * *\'.'); $task = new BaseTask('* * 0-30 * *'); } @@ -322,7 +323,7 @@ public function testConstructor22() { * @test */ public function testConstructor23() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * 30-30 * *\'.'); $task = new BaseTask('* * 30-30 * *'); } @@ -330,7 +331,7 @@ public function testConstructor23() { * @test */ public function testConstructor24() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * 20-10 * *\'.'); $task = new BaseTask('* * 20-10 * *'); } @@ -338,7 +339,7 @@ public function testConstructor24() { * @test */ public function testConstructor25() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * 20, * *\'.'); $task = new BaseTask('* * 20, * *'); } @@ -346,7 +347,7 @@ public function testConstructor25() { * @test */ public function testConstructor26() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * 20,a * *\'.'); $task = new BaseTask('* * 20,a * *'); } @@ -354,7 +355,7 @@ public function testConstructor26() { * @test */ public function testConstructor27() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* 5-c * * *\'.'); $task = new BaseTask('* 5-c * * *'); } @@ -362,7 +363,7 @@ public function testConstructor27() { * @test */ public function testConstructor28() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'5-a * * * *\'.'); $task = new BaseTask('5-a * * * *'); } @@ -540,7 +541,7 @@ public function testExecute03() { $job = new BaseTask(); $job->setOnExecution(function() { - throw new \Exception(); + throw new Exception(); }); $job->exec(); $this->assertFalse($job->isSuccess()); @@ -573,7 +574,7 @@ public function testExecute05() { }); $job->setOnFailure(function() { - throw new \Exception(); + throw new Exception(); }); $r = $job->exec(); $this->assertFalse($r); @@ -999,7 +1000,7 @@ public function testSetTaskName00() { * @test */ public function testTasksManager00() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'\'.'); $task = new BaseTask(''); } @@ -1007,7 +1008,7 @@ public function testTasksManager00() { * @test */ public function testTasksManager01() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'11-100 * * * *\'.'); $task = new BaseTask('11-100 * * * *'); } @@ -1015,7 +1016,7 @@ public function testTasksManager01() { * @test */ public function testTasksManager02() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'*/60 * * * *\'.'); $task = new BaseTask('*/60 * * * *'); } @@ -1023,7 +1024,7 @@ public function testTasksManager02() { * @test */ public function testTasksManager03() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* 15-24 * * *\'.'); $task = new BaseTask('* 15-24 * * *'); } @@ -1031,7 +1032,7 @@ public function testTasksManager03() { * @test */ public function testTasksManager04() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* */24 * * *\'.'); $task = new BaseTask('* */24 * * *'); } @@ -1039,7 +1040,7 @@ public function testTasksManager04() { * @test */ public function testTasksManager05() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * 25-37 * *\'.'); $task = new BaseTask('* * 25-37 * *'); } @@ -1047,7 +1048,7 @@ public function testTasksManager05() { * @test */ public function testTasksManager06() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * * 12-13 *\'.'); $task = new BaseTask('* * * 12-13 *'); } @@ -1055,7 +1056,7 @@ public function testTasksManager06() { * @test */ public function testTasksManager07() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * * 13 *\'.'); $task = new BaseTask('* * * 13 *'); } @@ -1063,7 +1064,7 @@ public function testTasksManager07() { * @test */ public function testTasksManager08() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * * 12-1 *\'.'); $task = new BaseTask('* * * 12-1 *'); } @@ -1071,7 +1072,7 @@ public function testTasksManager08() { * @test */ public function testTasksManager09() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * * 5-16 *\'.'); $task = new BaseTask('* * * 5-16 *'); } @@ -1079,7 +1080,7 @@ public function testTasksManager09() { * @test */ public function testTasksManager10() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * * * 0-7\'.'); $task = new BaseTask('* * * * 0-7'); } @@ -1087,7 +1088,7 @@ public function testTasksManager10() { * @test */ public function testTasksManager11() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * * * 0-7\'.'); $task = new BaseTask('* * * * 0-7'); } @@ -1102,7 +1103,7 @@ public function testTasksManager12() { * @test */ public function testTasksManager13() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * * * 7\'.'); $task = new BaseTask('* * * * 7'); } @@ -1110,7 +1111,7 @@ public function testTasksManager13() { * @test */ public function testTasksManager14() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'40-30 * * * *\'.'); $task = new BaseTask('40-30 * * * *'); } @@ -1118,7 +1119,7 @@ public function testTasksManager14() { * @test */ public function testTasksManager15() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'40-30 * * * *\'.'); $task = new BaseTask('40-30 * * * *'); } @@ -1126,7 +1127,7 @@ public function testTasksManager15() { * @test */ public function testTasksManager16() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* * * * 1,2,7,8\'.'); $task = new BaseTask('* * * * 1,2,7,8'); } @@ -1134,7 +1135,7 @@ public function testTasksManager16() { * @test */ public function testTasksManager17() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* 23-8 * * *\'.'); $task = new BaseTask('* 23-8 * * *'); } @@ -1142,7 +1143,7 @@ public function testTasksManager17() { * @test */ public function testTasksManager18() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* 0-13,14,24 * * *\'.'); $task = new BaseTask('* 0-13,14,24 * * *'); } @@ -1150,7 +1151,7 @@ public function testTasksManager18() { * @test */ public function testTasksManager19() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid cron expression: \'* 0-13 * 5,6,13 *\'.'); $task = new BaseTask('* 0-13 * 5,6,13 *'); } @@ -1225,4 +1226,24 @@ public function testWeeklyOn05() { $this->assertFalse($job->weeklyOn('0','24:00')); $this->assertTrue($job->weeklyOn('0','00:00')); } + /** + * @test + */ + public function testEveryXMinute00() { + $task = new BaseTask(); + $task->everyXMinuts(5); + $this->assertEquals('*/5 * * * *', $task->getExpression()); + + TasksManager::setDayOfMonth(15); + TasksManager::setHour(23); + TasksManager::setMonth(5); + TasksManager::setMinute(33); + $this->assertEquals('05-15 23:33', TasksManager::getTimestamp()); + $this->assertTrue($task->isDayOfWeek()); + $this->assertFalse($task->isMinute()); + $this->assertTrue($task->isHour()); + $this->assertTrue($task->isMonth()); + $this->assertTrue($task->isDayOfMonth()); + + } } diff --git a/tests/webfiori/framework/test/writers/CronWritterTest.php b/tests/webfiori/framework/test/writers/BackgroundTaskWritterTest.php similarity index 52% rename from tests/webfiori/framework/test/writers/CronWritterTest.php rename to tests/webfiori/framework/test/writers/BackgroundTaskWritterTest.php index a7492be14..06030d581 100644 --- a/tests/webfiori/framework/test/writers/CronWritterTest.php +++ b/tests/webfiori/framework/test/writers/BackgroundTaskWritterTest.php @@ -9,7 +9,7 @@ * * @author Ibrahim */ -class CronWritterTest extends TestCase { +class BackgroundTaskWritterTest extends TestCase { /** * @test */ @@ -53,4 +53,41 @@ public function test01() { $this->assertEquals(2, count($clazzObj->getArguments())); $writter->removeClass(); } + /** + * @test + */ + public function test02() { + $writter = new SchedulerTaskClassWriter('NewOk2B', 'Super Test Task', 'A test Task', [ + new TaskArgument('name', 'The name of something.') + ]); + + $this->assertEquals('NewOk2BTask', $writter->getName()); + + $this->assertEquals('app\\tasks', $writter->getNamespace()); + $this->assertEquals('Task', $writter->getSuffix()); + $this->assertEquals([ + "webfiori\\framework\\scheduler\\AbstractTask", + "webfiori\\framework\\scheduler\\TaskStatusEmail", + "webfiori\\framework\\scheduler\\TasksManager", + ], $writter->getUseStatements()); + $this->assertEquals('A test Task', $writter->getTaskDescription()); + $this->assertEquals(1, count($writter->getTask()->getArguments())); + $writter->addArgument(new TaskArgument('test', 'A test Arg.')); + $this->assertEquals(2, count($writter->getTask()->getArguments())); + $writter->addArgument(new TaskArgument('test-2', 'Second test arg')); + $writter->writeClass(); + $clazz = '\\'.$writter->getNamespace().'\\'.$writter->getName(); + $this->assertTrue(class_exists($clazz)); + $clazzObj = new $clazz(); + $this->assertTrue($clazzObj instanceof AbstractTask); + $this->assertEquals('A test Task', $clazzObj->getDescription()); + $this->assertEquals(3, count($clazzObj->getArguments())); + $this->assertEquals('name', $clazzObj->getArguments()[0]->getName()); + $this->assertEquals('The name of something.', $clazzObj->getArguments()[0]->getDescription()); + $this->assertEquals('test', $clazzObj->getArguments()[1]->getName()); + $this->assertEquals('A test Arg.', $clazzObj->getArguments()[1]->getDescription()); + $this->assertEquals('test-2', $clazzObj->getArguments()[2]->getName()); + $this->assertEquals('Second test arg', $clazzObj->getArguments()[2]->getDescription()); + $writter->removeClass(); + } } diff --git a/tests/webfiori/framework/test/writers/CLICommandClassWriterTest.php b/tests/webfiori/framework/test/writers/CLICommandClassWriterTest.php index bc2992163..66b4b2a4b 100644 --- a/tests/webfiori/framework/test/writers/CLICommandClassWriterTest.php +++ b/tests/webfiori/framework/test/writers/CLICommandClassWriterTest.php @@ -2,6 +2,7 @@ namespace webfiori\framework\test\writers; use PHPUnit\Framework\TestCase; +use webfiori\cli\CLICommand; use webfiori\framework\writers\CLICommandClassWriter; class CLICommandClassWriterTest extends TestCase { @@ -34,18 +35,36 @@ public function test01() { $this->assertEquals('Lets-Do-It', $writer->getCommandName()); $this->assertFalse($writer->setClassName('Invalid Name')); $this->assertFalse($writer->setClassName(' ')); - $this->assertTrue($writer->setClassName('DoItCommand')); - $this->assertEquals('DoItCommand', $writer->getName()); - $this->assertEquals(ROOT_PATH.DS.APP_DIR.DS.'commands'.DS.'DoItCommand.php', $writer->getAbsolutePath()); + $this->assertTrue($writer->setClassName('DoItXCommand')); + $this->assertEquals('DoItXCommand', $writer->getName()); + $this->assertEquals(ROOT_PATH.DS.APP_DIR.DS.'commands'.DS.'DoItXCommand.php', $writer->getAbsolutePath()); $this->assertEquals('app\\commands', $writer->getNamespace()); $this->assertEquals('Lets-Do-It', $writer->getCommandName()); $this->assertEquals('', $writer->getDescription()); $this->assertEquals([], $writer->getArgs()); + $writer->setArgs([ + new \webfiori\cli\Argument('--do', 'A do arg', true) + ]); + $this->assertEquals('', $writer->getDescription()); + $writer->setCommandDescription('Random desc'); + $this->assertEquals('Random desc', $writer->getDescription()); $this->assertEquals([ 'webfiori\cli\CLICommand' ], $writer->getUseStatements()); $writer->writeClass(); - $this->assertTrue(class_exists($writer->getNamespace().'\\'.$writer->getName())); + $clazz = $writer->getNamespace().'\\'.$writer->getName(); + $this->assertTrue(class_exists($clazz)); $writer->removeClass(); + $clazzObj = new $clazz(); + $this->assertTrue($clazzObj instanceof CLICommand); + $this->assertEquals('Lets-Do-It', $clazzObj->getName()); + $this->assertEquals('Random desc', $clazzObj->getDescription()); + $this->assertEquals([ + '--do' + ], $clazzObj->getArgsNames()); + $arg = $clazzObj->getArg('--do'); + $this->assertTrue($arg instanceof \webfiori\cli\Argument); + $this->assertEquals('A do arg', $arg->getDescription()); + $this->assertTrue($arg->isOptional()); } } diff --git a/webfiori/framework/cli/commands/SchedulerCommand.php b/webfiori/framework/cli/commands/SchedulerCommand.php index b8d5b6af3..cd8291d5d 100644 --- a/webfiori/framework/cli/commands/SchedulerCommand.php +++ b/webfiori/framework/cli/commands/SchedulerCommand.php @@ -111,6 +111,10 @@ public function listTasks() { } private function checkTaskArgs($taskName) { $task = TasksManager::getTask($taskName); + + if ($task === null) { + return; + } $args = $task->getExecArgsNames(); if (count($args) != 0 && $this->confirm('Would you like to customize execution arguments?', false)) { diff --git a/webfiori/framework/cli/helpers/CreateCLIClassHelper.php b/webfiori/framework/cli/helpers/CreateCLIClassHelper.php index 9e7ae9e85..72a9caa42 100644 --- a/webfiori/framework/cli/helpers/CreateCLIClassHelper.php +++ b/webfiori/framework/cli/helpers/CreateCLIClassHelper.php @@ -55,18 +55,21 @@ private function getArgs() : array { $addToMore = true; while ($addToMore) { - $argArr = []; - $groupName = $this->getInput('Enter argument name:'); + $argObj = new \webfiori\cli\Argument(); + $argName = $this->getInput('Enter argument name:'); - if (strlen($groupName) > 0) { - $argArr['name'] = $groupName; + if (!$argObj->setName($argName)) { + $this->error('Invalid name provided.'); + continue; } - $argArr['description'] = $this->getInput('Describe this argument and how to use it:', ''); - $argArr['values'] = $this->getFixedValues(); - $argArr['optional'] = $this->confirm('Is this argument optional or not?', true); - $argArr['default'] = $this->getInput('Enter default value:'); + $argObj->setDescription($this->getInput('Describe this argument and how to use it:', '')); + foreach ($this->getFixedValues() as $v) { + $argObj->addAllowedValue($v); + } + $argObj->setIsOptional($this->confirm('Is this argument optional or not?', true)); + $argObj->setDefault($this->getInput('Enter default value:').''); - $argsArr[] = $argArr; + $argsArr[] = $argObj; $addToMore = $this->confirm('Would you like to add more arguments?', false); } diff --git a/webfiori/framework/handlers/APICallErrHandler.php b/webfiori/framework/handlers/APICallErrHandler.php index 30a9f6c85..e2ee0bfde 100644 --- a/webfiori/framework/handlers/APICallErrHandler.php +++ b/webfiori/framework/handlers/APICallErrHandler.php @@ -19,7 +19,8 @@ /** * Exceptions handler which is used to handle exceptions in case of API call. * - * This handler is also used to handle startup exceptions. + * This handler is also used to handle startup exceptions. The priority of the handler + * is set to 0 which indicates that it will be executed last. * * @author Ibrahim */ @@ -32,6 +33,7 @@ class APICallErrHandler extends AbstractHandler { public function __construct() { parent::__construct(); $this->setName('API Call Errors Handler'); + } /** * Handles the exception diff --git a/webfiori/framework/handlers/CLIErrHandler.php b/webfiori/framework/handlers/CLIErrHandler.php index 812385ea2..96e01ef7c 100644 --- a/webfiori/framework/handlers/CLIErrHandler.php +++ b/webfiori/framework/handlers/CLIErrHandler.php @@ -17,7 +17,10 @@ use webfiori\framework\scheduler\TasksManager; /** * Exceptions handler which is used to handle exceptions in case of running - * CLI applications. + * CLI applications. + * + * The priority of the handler + * is set to 0 which indicates that it will be executed last. * * * @author Ibrahim diff --git a/webfiori/framework/handlers/HTTPErrHandler.php b/webfiori/framework/handlers/HTTPErrHandler.php index 71f515031..8f1a95eb5 100644 --- a/webfiori/framework/handlers/HTTPErrHandler.php +++ b/webfiori/framework/handlers/HTTPErrHandler.php @@ -18,7 +18,10 @@ use webfiori\http\Response; /** * Errors and exceptions handler which is used to handle errors in case of - * HTTP request. + * HTTP request. + * + * The priority of the handler + * is set to 0 which indicates that it will be executed last. * * @author Ibrahim */ diff --git a/webfiori/framework/scheduler/AbstractTask.php b/webfiori/framework/scheduler/AbstractTask.php index acbf478fb..2f40a2eaf 100644 --- a/webfiori/framework/scheduler/AbstractTask.php +++ b/webfiori/framework/scheduler/AbstractTask.php @@ -182,6 +182,7 @@ public function __construct(string $taskName = '', string $when = '* * * * *', s $this->taskDetails['days-of-month'] = []; $this->taskDetails['months'] = []; $this->taskDetails['days-of-week'] = []; + if (!$this->cron($when)) { @@ -377,6 +378,17 @@ public function everyMonthOn(int $dayNum = 1, string $time = '00:00') : bool { return false; } + /** + * Schedule a task to run every specific number of minutes. + * + * Assuming that 5 is supplied as a value, this means that the job will be + * executed every 5 minutes within an hour. + * + * @param int $step The number of minutes that a job will be executed after. + */ + public function everyXMinuts(int $step) { + $this->cron('*/'.$step.' * * * *'); + } /** * Execute the event which should run when it is time to execute the task. * @@ -1388,12 +1400,12 @@ private function getSubExprType(string $expr): string { $retVal = self::ANY_VAL; if ($expr != '*') { - $split = explode('/', $expr); - $count = count($split); + $split0 = explode('/', $expr); + $count = count($split0); if (!($count == 2)) { - $split = explode('-', $expr); - $count = count($split); + $split0 = explode('-', $expr); + $count = count($split0); if (!($count == 2)) { $retVal = self::SPECIFIC_VAL; @@ -1406,16 +1418,17 @@ private function getSubExprType(string $expr): string { } $retVal = self::RANGE_VAL; - if (!(strlen($split[0]) != 0 && strlen($split[1]) != 0)) { + if (!(strlen($split0[0]) != 0 && strlen($split0[1]) != 0)) { $retVal = self::INV_VAL; } return $retVal; - } - $retVal = self::INV_VAL; - - if (!(strlen($split[0]) != 0 && strlen($split[1]) != 0)) { - $retVal = self::STEP_VAL; + } else { + //Step val + if (!(strlen($split0[0]) != 0 && strlen($split0[1]) != 0)) { + return self::INV_VAL; + } + return self::STEP_VAL; } } @@ -1507,8 +1520,14 @@ private function logExeException(Throwable $ex, string $meth = '') { TasksManager::log('Line: '.$ex->getLine()); TasksManager::log('Stack Trace:'); $index = 0; - - foreach ($ex->getTrace() as $traceEntry) { + $trace = debug_backtrace(); + $firstEntry = $ex->getTrace()[0]; + $firstEntry['line'] = $ex->getLine(); + $e = new TraceEntry($firstEntry); + TasksManager::log('#'.$index.' '.$e); + $index++; + + foreach ($trace as $traceEntry) { $e = new TraceEntry($traceEntry); TasksManager::log('#'.$index.' '.$e); $index++; diff --git a/webfiori/framework/scheduler/TasksManager.php b/webfiori/framework/scheduler/TasksManager.php index 932a0f4a8..3d93fed33 100644 --- a/webfiori/framework/scheduler/TasksManager.php +++ b/webfiori/framework/scheduler/TasksManager.php @@ -479,16 +479,64 @@ public static function initRoutes() { * * @param string $message A string that act as a log message. It will be * appended as passed without any changes. - * + * + * @param string $type The type of the message that will be logged. It can + * be one of the following values: + * + * Default is 'none'. + * * @since 1.0.8 */ - public static function log(string $message) { + public static function log(string $message, string $type = 'none') { self::get()->logsArray[] = $message; if (self::get()->command !== null && self::get()->command->isArgProvided('--show-log')) { - self::get()->command->println("%s", $message); + if ($type == 'success') { + self::get()->command->success($message); + } else if ($type == 'error') { + self::get()->command->error($message); + } else if ($type == 'info') { + self::get()->command->info($message); + } else { + self::get()->command->println("%s", $message); + } } } + /** + * Appends a message to the array that contains logged messages. + * + * @param string $msg A string that act as a log message. It will be + * appended as passed without any changes. Note that if running in CLI, + * this will appear as a success message + */ + public static function logSuccess(string $msg) { + self::log($msg, 'success'); + } + /** + * Appends a message to the array that contains logged messages. + * + * @param string $msg A string that act as a log message. It will be + * appended as passed without any changes. Note that if running in CLI, + * this will appear as a info message + */ + public static function logInfo(string $msg) { + self::log($msg, 'info'); + } + /** + * Appends a message to the array that contains logged messages. + * + * @param string $msg A string that act as a log message. It will be + * appended as passed without any changes. Note that if running in CLI, + * this will appear as a error message + */ + public static function logErr(string $msg) { + self::log($msg, 'error'); + } /** * Create a task that will be executed once every month. * diff --git a/webfiori/framework/writers/CLICommandClassWriter.php b/webfiori/framework/writers/CLICommandClassWriter.php index 8b536fe3a..511e98809 100644 --- a/webfiori/framework/writers/CLICommandClassWriter.php +++ b/webfiori/framework/writers/CLICommandClassWriter.php @@ -157,8 +157,8 @@ public function writeClassComment() { $topArr[] = ' * In addition, the command have the following args:'; $topArr[] = ' * '; } @@ -180,18 +180,19 @@ private function writeConstructor() { if (count($this->args) > 0) { $this->append(["parent::__construct('$this->name', ["], 2); - foreach ($this->args as $argArr) { - $this->append("'".$argArr['name']."' => [", 3); - - if (strlen($argArr['description']) != 0) { - $this->append("'description' => '".str_replace("'", "\'", $argArr['description'])."',", 4); + foreach ($this->args as $argObj) { + + $this->append("'".$argObj->getName()."' => [", 3); + + if (strlen($argObj->getDescription()) != 0) { + $this->append("'description' => '".str_replace("'", "\'", $argObj->getDescription())."',", 4); } - $this->append("'optional' => ".($argArr['optional'] === true ? 'true' : 'false').",", 4); + $this->append("'optional' => ".($argObj->isOptional() ? 'true' : 'false').",", 4); - if (count($argArr['values']) != 0) { + if (count($argObj->getAllowedValues()) != 0) { $this->append("'values' => [", 4); - foreach ($argArr['values'] as $val) { + foreach ($argObj->getAllowedValues() as $val) { $this->append("'".str_replace("'", "\'", $val)."',", 5); } $this->append("]", 4); @@ -200,7 +201,7 @@ private function writeConstructor() { } $this->append("], '".str_replace("'", "\'", $this->desc)."');", 2); } else { - $this->append("parent::__construct('$this->name', '".str_replace("'", "\'", $this->desc)."');", 2); + $this->append("parent::__construct('$this->name', [], '".str_replace("'", "\'", $this->desc)."');", 2); } $this->append('}', 1); diff --git a/webfiori/framework/writers/SchedulerTaskClassWriter.php b/webfiori/framework/writers/SchedulerTaskClassWriter.php index 761c92508..b603875f9 100644 --- a/webfiori/framework/writers/SchedulerTaskClassWriter.php +++ b/webfiori/framework/writers/SchedulerTaskClassWriter.php @@ -25,6 +25,9 @@ class SchedulerTaskClassWriter extends ClassWriter { /** * Creates new instance of the class. * + * @param string $className The name of the class that will represent the + * task. + * * @param string $taskName The name of the task. * * @param string $taskDesc A short description that description what does the @@ -33,8 +36,8 @@ class SchedulerTaskClassWriter extends ClassWriter { * @param array $argsArr An associative array that holds any arguments that * the task needs. */ - public function __construct($taskName = '', $taskDesc = '', array $argsArr = []) { - parent::__construct('NewTask', APP_PATH.'tasks', APP_DIR.'\\tasks'); + public function __construct(string $className = 'NewTask', $taskName = '', $taskDesc = '', array $argsArr = []) { + parent::__construct($className, APP_PATH.'tasks', APP_DIR.'\\tasks'); $this->task = new BaseTask(); if (!$this->setTaskName($taskName)) { @@ -44,6 +47,7 @@ public function __construct($taskName = '', $taskDesc = '', array $argsArr = []) if (!$this->setTaskDescription($taskDesc)) { $this->setTaskDescription('No Description'); } + $this->getTask()->setDescription($this->getTaskDescription()); foreach ($argsArr as $taskArg) { $this->addArgument($taskArg);