Skip to content

Commit

Permalink
chore: Code Quality Improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Jul 24, 2024
1 parent 880f8bf commit f8e9ed9
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 24 deletions.
2 changes: 2 additions & 0 deletions tests/webfiori/framework/test/PrivilegesGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function testRemoveParentGroup00($gArr) {
public function testRemoveParentGroup01() {
$child = new PrivilegesGroup('CH_GROUP_1', 'Child Group #1');
$this->assertFalse($child->setParentGroup());

}
/**
* @test
Expand Down Expand Up @@ -141,6 +142,7 @@ public function testSetID01() {
public function testSetParentGroup00() {
$child = new PrivilegesGroup('CH_GROUP_1', 'Child Group #1');
$parentGroup = new PrivilegesGroup('PARENT_1', 'Parent Group #1');
$this->assertTrue($parentGroup->setParentGroup(null));
$this->assertFalse($child->setParentGroup($child));
$this->assertTrue($child->setParentGroup($parentGroup));
$this->assertSame($parentGroup,$child->getParentGroup());
Expand Down
131 changes: 130 additions & 1 deletion tests/webfiori/framework/test/scheduler/SchedulerTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use Exception;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use webfiori\framework\exceptions\InvalidCRONExprException;
use webfiori\framework\Privilege;
use webfiori\framework\scheduler\BaseTask;
use webfiori\framework\scheduler\TaskArgument;
use webfiori\framework\scheduler\TasksManager;
Expand Down Expand Up @@ -79,7 +81,7 @@ public function testAttributes06() {
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid argument type. Expected 'string' or 'webfiori\\framework\\scheduler\\TaskArgument'");
$job = new BaseTask();
$job->addExecutionArg(new \webfiori\framework\Privilege());
$job->addExecutionArg(new Privilege());
}
/**
* @test
Expand Down Expand Up @@ -376,6 +378,30 @@ public function testConstructor28() {
$this->expectExceptionMessage('Invalid cron expression: \'5-a * * * *\'.');
$task = new BaseTask('5-a * * * *');
}
/**
* @test
*/
public function testConstructor29() {
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid cron expression: \'5-7,8- * * * *\'.');
$task = new BaseTask('5-7,8- * * * *');
}
/**
* @test
*/
public function testConstructor30() {
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid cron expression: \'5/,8- * * * *\'.');
$task = new BaseTask('5/,8- * * * *');
}
/**
* @test
*/
public function testConstructor31() {
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid cron expression: \'5/b,8- * * * *\'.');
$task = new BaseTask('5/b,8- * * * *');
}
/**
* @test
*/
Expand Down Expand Up @@ -708,6 +734,37 @@ public function testIsDayOfWeek02() {
TasksManager::setDayOfWeek(5);
$this->assertFalse($job->isDayOfWeek());
}
/**
* @test
*/
public function testIsDayOfWeek03() {
$job = new BaseTask();
$job->everyWeek();

TasksManager::setDayOfWeek(0);
TasksManager::setHour(0);
$this->assertTrue($job->isDayOfWeek());
$this->assertTrue($job->isHour());

}
/**
* @test
*/
public function testIsDayOfWeek04() {
$this->expectException(InvalidCRONExprException::class);
$this->expectExceptionMessage("Invalid cron expression: '5 4 * * 0-a,5-6'");
$job = new BaseTask('5 4 * * 0-a,5-6');

}
/**
* @test
*/
public function testIsDayOfWeek05() {
$this->expectException(InvalidCRONExprException::class);
$this->expectExceptionMessage("Invalid cron expression: '5 4 * * 0-,5-6'");
$job = new BaseTask('5 4 * * 0-,5-6');

}
/**
* @test
*/
Expand Down Expand Up @@ -774,7 +831,40 @@ public function testIsHour02() {
$this->assertFalse($job->isHour());
}

/**
* @test
*/
public function testIsHour03() {
$job = new BaseTask();
$job->everyXHour(2);

for ($x = 0 ; $x < 24 ; $x++) {
TasksManager::setHour($x);

if ($x % 2 == 0) {
$this->assertTrue($job->isHour());
} else {
$this->assertFalse($job->isHour());
}
}
}
/**
* @test
*/
public function testIsHour04() {
$job = new BaseTask();
$job->everyXHour(3);

for ($x = 0 ; $x < 24 ; $x++) {
TasksManager::setHour($x);

if ($x % 3 == 0) {
$this->assertTrue($job->isHour());
} else {
$this->assertFalse($job->isHour());
}
}
}
/**
* @test
*/
Expand Down Expand Up @@ -914,6 +1004,24 @@ public function testIsMonth02() {
TasksManager::setMinute(1);
$this->assertFalse($job->isMinute());
}
/**
* @test
*/
public function testIsMonth03() {
$this->expectException(InvalidCRONExprException::class);
$this->expectExceptionMessage("Invalid cron expression: '* 4 * 1-3,9-b *");
$job = new BaseTask('* 4 * 1-3,9-b *');

}
/**
* @test
*/
public function testIsMonth04() {
$this->expectException(InvalidCRONExprException::class);
$this->expectExceptionMessage("Invalid cron expression: '* 4 * 1-3,9- *");
$job = new BaseTask('* 4 * 1-3,9- *');

}
/**
* @test
*/
Expand Down Expand Up @@ -1255,4 +1363,25 @@ public function testEveryXMinute00() {
$this->assertTrue($task->isDayOfMonth());

}
/**
* @test
*/
public function testEveryXMinute01() {
$task = new BaseTask();
$task->everyXMinuts(17);
TasksManager::setDayOfMonth(15);
TasksManager::setHour(23);
TasksManager::setMonth(5);
TasksManager::setMinute(33);

for ($x = 1 ; $x < 60 ; $x++) {
TasksManager::setMinute($x);
if ($x % 17 == 0) {
$this->assertTrue($task->isMinute());
} else {
$this->assertFalse($task->isMinute());
}
}

}
}
57 changes: 34 additions & 23 deletions webfiori/framework/scheduler/AbstractTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,33 @@ public function dailyAt(int $hour = 0, int $minute = 0) : bool {
public function everyHour() {
$this->cron('0 * * * *');
}
/**
* Schedules a task to run weekly at specific week day and time.
*
* @param int $dayNameOrNum A 3 letter day name (such as 'sun' or 'tue') or a day number from 0 to 6.
* 0 for sunday. Default is 0.
*
* @param string $time A time in the form 'HH:MM'. HH can have any value
* between 0 and 23 inclusive. MM can have any value between 0 and 59 inclusive.
* default is '00:00'.
*
* @return bool If the time for the task is set, the method will
* return true. If not, it will return false.
*/
public function everyWeek($dayNameOrNum = 0, string $time = '00:00') : bool {
return $this->weeklyOn($dayNameOrNum, $time);
}
/**
* Schedule a task to run every specific number of hours.
*
* The expression that will be used is "At minute 0 past every X hour" where
* x is the number of hours.
*
* @param int $xHour The number of hours at which the job will be executed.
*/
public function everyXHour(int $xHour) {
$this->cron('0 */'.$xHour.' * * *');
}
/**
* Schedules a task to run every month on specific day and time.
*
Expand Down Expand Up @@ -1190,7 +1217,7 @@ private function checkHoursHelper(string $hoursField) {
}
$hoursAttrs['every-x-hour'][] = $stepVal;
} else if ($exprType == self::SPECIFIC_VAL) {
if (!$this->isNumberHelper($subExpr)) {
if (!is_numeric($subExpr)) {
$isValidExpr = false;
break;
}
Expand Down Expand Up @@ -1231,6 +1258,10 @@ private function checkMinutesHelper(string $minutesField) {
break;
} else if ($exprType == self::RANGE_VAL) {
$range = explode('-', $subExpr);
if (!(is_numeric($range[0]) && is_numeric($range[1]))) {
$isValidExpr = false;
break;
}
$start = intval($range[0]);
$end = intval($range[1]);

Expand All @@ -1248,7 +1279,7 @@ private function checkMinutesHelper(string $minutesField) {
}
$minuteAttrs['every-x-minute'][] = $stepVal;
} else if ($exprType == self::SPECIFIC_VAL) {
if (!$this->isNumberHelper($subExpr)) {
if (!is_numeric($subExpr)) {
$isValidExpr = false;
break;
}
Expand Down Expand Up @@ -1402,7 +1433,7 @@ private function getArgValFromTerminal($name) {
*/
private function getSubExprType(string $expr): string {
$retVal = self::ANY_VAL;

if ($expr != '*') {
$split0 = explode('/', $expr);
$count = count($split0);
Expand Down Expand Up @@ -1472,26 +1503,6 @@ private function isMinuteHelper($minuteArr, $current) {

return $retVal;
}
/**
* Checks if a given string represents a number or not.
* @param string $str
* @return bool
*/
private function isNumberHelper(string $str): bool {
$len = strlen($str);

if ($len != 0) {
for ($x = 0 ; $x < $len ; $x++) {
$ch = $str[$x];

if (!($ch >= '0' && $ch <= '9')) {
return false;
}
}
}

return true;
}
private function isValidRange(int $start, int $end, int $min, int $max): bool {
$isValidExpr = true;

Expand Down

0 comments on commit f8e9ed9

Please sign in to comment.