Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added Additional Logging Methods to Tasks Manager #226

Merged
merged 14 commits into from
Jul 2, 2024
50 changes: 50 additions & 0 deletions app/middleware/CoolMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
namespace app\middleware;

use webfiori\framework\middleware\AbstractMiddleware;
use webfiori\framework\session\SessionsManager;
use webfiori\http\Request;
use webfiori\http\Response;
/**
* A middleware which is created using the command "create".
*
* The middleware will have the name 'Super Middleware' and
* Priority 100.
* In addition, the middleware is added to the following groups:
* <ul>
* <li>one-group</li>
* <li>two-group</li>
* <li>global</li>
* </ul>
*/
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.
}
}
3 changes: 3 additions & 0 deletions app/tasks/Fail1TestTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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".
*
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions app/tasks/SuccessTestEveryMinute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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".
*
Expand Down Expand Up @@ -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.
}
/**
Expand Down
5 changes: 5 additions & 0 deletions tests/webfiori/framework/test/cli/CreateTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ public function test03() {
'end',
'End date of the report.',
'2021-07-07',
'y',
'',
'n'
]);

Expand All @@ -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: <empty string>\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';
Expand Down
73 changes: 61 additions & 12 deletions tests/webfiori/framework/test/cli/SchedulerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
2 changes: 2 additions & 0 deletions tests/webfiori/framework/test/router/RouterUriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
/**
Expand Down
Loading
Loading