diff --git a/tests/webfiori/framework/test/cli/SchedulerCommandTest.php b/tests/webfiori/framework/test/cli/SchedulerCommandTest.php index 605c1bd3..506289db 100644 --- a/tests/webfiori/framework/test/cli/SchedulerCommandTest.php +++ b/tests/webfiori/framework/test/cli/SchedulerCommandTest.php @@ -173,8 +173,8 @@ public function test05() { "Line: 44\n", "Stack Trace:\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", + "#1 At class webfiori\\framework\scheduler\AbstractTask line 1101\n", + "#2 At class webfiori\\framework\scheduler\AbstractTask line 422\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", diff --git a/tests/webfiori/framework/test/cli/UpdateSettingsCommandTest.php b/tests/webfiori/framework/test/cli/UpdateSettingsCommandTest.php index 3cc422cb..8b9796b9 100644 --- a/tests/webfiori/framework/test/cli/UpdateSettingsCommandTest.php +++ b/tests/webfiori/framework/test/cli/UpdateSettingsCommandTest.php @@ -315,7 +315,56 @@ public function testUpdatePrimaryTheme01() { $this->assertEquals('themes\\fioriTheme2\\NewTestTheme2', App::getConfig()->getTheme()); } + /** + * @test + */ + public function testUpdatePrimaryTheme02() { + $runner = App::getRunner(); + $runner->setInputs([ + 'webfiori\\framework\\Privilege', + 'themes\\fioriTheme2\\NewTestTheme2' + ]); + $runner->setArgsVector([ + 'webfiori', + 'update-settings', + '--w' => 'theme' + ]); + $runner->start(); + //$this->assertEquals(0, $runner->start()); + $this->assertEquals([ + "Enter theme class name with namespace:\n", + "Error: Invalid input is given. Try again.\n", + "Enter theme class name with namespace:\n", + "Success: Theme successfully updated.\n" + ], $runner->getOutput()); + $this->assertEquals('themes\\fioriTheme2\\NewTestTheme2', App::getConfig()->getTheme()); + } + /** + * @test + */ + public function testUpdatePrimaryTheme03() { + $runner = App::getRunner(); + $runner->setInputs([ + 'webfiori\\framework\\App', + 'themes\\fioriTheme2\\NewTestTheme2' + ]); + $runner->setArgsVector([ + 'webfiori', + 'update-settings', + '--w' => 'theme' + ]); + $runner->start(); + //$this->assertEquals(0, $runner->start()); + $this->assertEquals([ + "Enter theme class name with namespace:\n", + "Error: Invalid input is given. Try again.\n", + "Enter theme class name with namespace:\n", + "Success: Theme successfully updated.\n" + ], $runner->getOutput()); + + $this->assertEquals('themes\\fioriTheme2\\NewTestTheme2', App::getConfig()->getTheme()); + } /** * @test */ diff --git a/tests/webfiori/framework/test/config/JsonDriverTest.php b/tests/webfiori/framework/test/config/JsonDriverTest.php index 2101f70f..f703829a 100644 --- a/tests/webfiori/framework/test/config/JsonDriverTest.php +++ b/tests/webfiori/framework/test/config/JsonDriverTest.php @@ -142,6 +142,7 @@ public function testAddEnvVar00() { $driver->addEnvVar('COOL_OR_NOT', 'cool'); $driver->addEnvVar('DO_IT', false); $driver->addEnvVar('MULTIPLY_BY', 4, 'A number to multiply by.'); + $driver->addEnvVar('NUL'); } /** * @test @@ -171,6 +172,10 @@ public function testAddEnvVar01() { "value" => 4, "description" => "A number to multiply by." ], + 'NUL' => [ + "value" => null, + "description" => null + ] ], $driver->getEnvVars()); $driver->removeEnvVar('COOL_OR_NOT'); } @@ -198,6 +203,10 @@ public function testAddEnvVar02() { "value" => 4, "description" => "A number to multiply by." ], + 'NUL' => [ + "value" => null, + "description" => null + ] ], $driver->getEnvVars()); } /** @@ -409,6 +418,7 @@ public function testDatabaseConnections00() { $conn = new ConnectionInfo('mysql', 'root', 'test@222', 'my_db', 'localhost', 3306); $driver->addOrUpdateDBConnection($conn); $this->assertEquals(1, count($driver->getDBConnections())); + } /** * @test @@ -417,14 +427,42 @@ public function testDatabaseConnections00() { public function testDatabaseConnections01() { $driver = new JsonDriver(); $driver->initialize(); - $account =$driver->getDBConnection('New_Connection'); + $account = $driver->getDBConnection('New_Connection'); $this->assertEquals(3306, $account->getPort()); $this->assertEquals('my_db', $account->getDBName()); $this->assertEquals('mysql', $account->getDatabaseType()); $this->assertEquals('localhost', $account->getHost()); $this->assertEquals('test@222', $account->getPassword()); $this->assertEquals('root', $account->getUsername()); - + $driver->removeAllDBConnections(); + $this->assertEquals(0, count($driver->getDBConnections())); + $this->assertNull($driver->getDBConnection('New_Connection')); + } + /** + * @test + * @depends testDatabaseConnections01 + */ + public function testDatabaseConnections02() { + $driver = new JsonDriver(); + $this->assertEquals(0, count($driver->getDBConnections())); + $this->assertNull($driver->getDBConnection('olf')); + $conn = new ConnectionInfo('mysql', 'root', 'test@222', 'my_db', 'localhost', 3306, [ + 'KG' => 9, + 'OP' => 'hello' + ]); + $driver->addOrUpdateDBConnection($conn); + $this->assertEquals(1, count($driver->getDBConnections())); + $account = $driver->getDBConnection('New_Connection'); + $this->assertEquals(3306, $account->getPort()); + $this->assertEquals('my_db', $account->getDBName()); + $this->assertEquals('mysql', $account->getDatabaseType()); + $this->assertEquals('localhost', $account->getHost()); + $this->assertEquals('test@222', $account->getPassword()); + $this->assertEquals('root', $account->getUsername()); + $this->assertEquals([ + 'KG' => 9, + 'OP' => 'hello' + ], $account->getExtars()); } /** * @test diff --git a/tests/webfiori/framework/test/scheduler/SchedulerTaskTest.php b/tests/webfiori/framework/test/scheduler/SchedulerTaskTest.php index a8af8e48..e06af5c3 100644 --- a/tests/webfiori/framework/test/scheduler/SchedulerTaskTest.php +++ b/tests/webfiori/framework/test/scheduler/SchedulerTaskTest.php @@ -76,6 +76,15 @@ public function testAttributes05() { * @test */ 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()); + } + /** + * @test + */ + public function testAttributes07() { $job = new BaseTask(); $job->addExecutionArgs([ 'one' => [ diff --git a/tests/webfiori/framework/test/writers/CLICommandClassWriterTest.php b/tests/webfiori/framework/test/writers/CLICommandClassWriterTest.php index 66b4b2a4..29e4ab50 100644 --- a/tests/webfiori/framework/test/writers/CLICommandClassWriterTest.php +++ b/tests/webfiori/framework/test/writers/CLICommandClassWriterTest.php @@ -67,4 +67,47 @@ public function test01() { $this->assertEquals('A do arg', $arg->getDescription()); $this->assertTrue($arg->isOptional()); } + /** + * @test + */ + public function test02() { + $writer = new CLICommandClassWriter(); + $this->assertFalse($writer->setCommandName('invalid name')); + $this->assertFalse($writer->setCommandName(' ')); + $this->assertTrue($writer->setCommandName('Lets-Do-It')); + $this->assertEquals('Lets-Do-It', $writer->getCommandName()); + $this->assertFalse($writer->setClassName('Invalid Name')); + $this->assertFalse($writer->setClassName(' ')); + $this->assertTrue($writer->setClassName('DoItX2Command')); + $this->assertEquals('DoItX2Command', $writer->getName()); + $this->assertEquals(ROOT_PATH.DS.APP_DIR.DS.'commands'.DS.'DoItX2Command.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(' '); + $this->assertEquals('', $writer->getDescription()); + $this->assertEquals([ + 'webfiori\cli\CLICommand' + ], $writer->getUseStatements()); + $writer->writeClass(); + $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('', $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/UpdateSettingsCommand.php b/webfiori/framework/cli/commands/UpdateSettingsCommand.php index e6844bbc..dde9ee0b 100644 --- a/webfiori/framework/cli/commands/UpdateSettingsCommand.php +++ b/webfiori/framework/cli/commands/UpdateSettingsCommand.php @@ -104,6 +104,7 @@ private function getThemeNs() { if ($instance instanceof Theme) { return true; } + return false; } catch (Throwable $exc) { return false; } diff --git a/webfiori/framework/config/ClassDriver.php b/webfiori/framework/config/ClassDriver.php index 43b3209d..ca230c84 100644 --- a/webfiori/framework/config/ClassDriver.php +++ b/webfiori/framework/config/ClassDriver.php @@ -67,7 +67,7 @@ public static function a($file, $str, $tabSize = 0) { * @param string $description An optional description to describe the porpuse * of the constant. */ - public function addEnvVar(string $name, $value, string $description = null) { + public function addEnvVar(string $name, $value = null, string $description = null) { $this->configVars['env-vars'][$name] = [ 'value' => $value, 'description' => $description diff --git a/webfiori/framework/config/ConfigurationDriver.php b/webfiori/framework/config/ConfigurationDriver.php index 367eade9..d41af29a 100644 --- a/webfiori/framework/config/ConfigurationDriver.php +++ b/webfiori/framework/config/ConfigurationDriver.php @@ -18,15 +18,16 @@ interface ConfigurationDriver { * The variables which are added using this method will be defined as * a named constant at run time using the function 'define'. This means * the constant will be accesaable anywhere within the application's environment. + * Additionally, it will be added as environment variable using 'putenv()'. * * @param string $name The name of the named constant such as 'MY_CONSTANT'. * - * @param mixed $value The value of the constant. + * @param mixed|null $value The value of the constant. * * @param string $description An optional description to describe the porpuse * of the constant. */ - public function addEnvVar(string $name, $value, string $description = null); + public function addEnvVar(string $name, $value = null, string $description = null); /** * Adds new database connections information or update existing connections. * diff --git a/webfiori/framework/config/Controller.php b/webfiori/framework/config/Controller.php index d338c95a..254dc4eb 100644 --- a/webfiori/framework/config/Controller.php +++ b/webfiori/framework/config/Controller.php @@ -22,6 +22,15 @@ public function __construct() { $this->driver = new $driverClazz(); $this->driver->initialize(); } + /** + * Adds new environment variable to the configuration of the app. + * + * @param string $name The name of the variable such as 'MY_VAR'. + * + * @param mixed $value The value of the variable. + * + * @param string|null $description An optional text that describes the variable. + */ public function addEnvVar(string $name, $value, string $description = null) { $this->getDriver()->addEnvVar($name, $value, $description); } @@ -56,6 +65,7 @@ public function copy(ConfigurationDriver $new) { } foreach ($current->getEnvVars() as $name => $probs) { + $new->addEnvVar($name, $probs['value'], $probs['description']); } $new->setPrimaryLanguage($current->getPrimaryLanguage()); @@ -104,7 +114,13 @@ public static function setDriver(ConfigurationDriver $driver) { public static function updateEnv() { foreach (self::getDriver()->getEnvVars() as $name => $envVar) { if (!defined($name)) { - define($name, $envVar['value']); + if (isset($envVar['value'])) { + define($name, $envVar['value']); + putenv($name.'='.$envVar['value']); + } else { + define($name, null); + putenv($name.'='); + } } } } diff --git a/webfiori/framework/config/JsonDriver.php b/webfiori/framework/config/JsonDriver.php index 1b1c8d4a..6588baff 100644 --- a/webfiori/framework/config/JsonDriver.php +++ b/webfiori/framework/config/JsonDriver.php @@ -87,7 +87,7 @@ public function __construct() { * @param string $description An optional description to describe the porpuse * of the constant. */ - public function addEnvVar(string $name, $value, string $description = null) { + public function addEnvVar(string $name, $value = null, string $description = null) { $this->json->get('env-vars')->add($name, new Json([ 'value' => $value, 'description' => $description @@ -193,6 +193,8 @@ public function getDBConnection(string $conName) { foreach ($extras->getProperties() as $prop) { $extrasArr[$prop->getName()] = $prop->getValue(); } + } else if (gettype($extras) == 'array') { + $extrasArr = $extras; } return new ConnectionInfo( @@ -225,11 +227,16 @@ public function getDBConnections(): array { $this->getProp($jsonObj, 'database', $name)); $extrasObj = $jsonObj->get('extras'); - if ($extrasObj !== null && $extrasObj instanceof Json) { + if ($extrasObj !== null) { $extrasArr = []; - - foreach ($extrasObj->getProperties() as $prop) { - $extrasArr[$prop->getName()] = $prop->getValue(); + if ($extrasObj instanceof Json) { + + + foreach ($extrasObj->getProperties() as $prop) { + $extrasArr[$prop->getName()] = $prop->getValue(); + } + } else if (gettype($extrasObj) == 'array') { + $extrasArr = $extrasObj; } $acc->setExtras($extrasArr); } @@ -262,7 +269,15 @@ public function getDescriptions(): array { return $retVal; } - + /** + * Returns an array that holds the information of defined application environment + * variables. + * + * @return array The returned array will be associative. The key will represent + * the name of the variable and its value is a sub-associative array with + * two indices, 'description' and 'value'. The description index is a text that describes + * the variable and the value index will hold its value. + */ public function getEnvVars(): array { $retVal = []; $vars = $this->json->get('env-vars'); @@ -364,7 +379,12 @@ public function getSMTPConnections(): array { return $retVal; } - + /** + * Returns the name or the namespace of default theme that the application + * will use in case a page does not have specific theme. + * + * @return string + */ public function getTheme(): string { return $this->json->get('theme') ?? ''; } diff --git a/webfiori/framework/scheduler/AbstractTask.php b/webfiori/framework/scheduler/AbstractTask.php index 2f40a2ea..0e0af20e 100644 --- a/webfiori/framework/scheduler/AbstractTask.php +++ b/webfiori/framework/scheduler/AbstractTask.php @@ -11,6 +11,7 @@ namespace webfiori\framework\scheduler; use Exception; +use InvalidArgumentException; use Throwable; use webfiori\collections\Queue; use webfiori\error\TraceEntry; @@ -205,6 +206,9 @@ public function __construct(string $taskName = '', string $when = '* * * * *', s * * @param string|TaskArgument $nameOrObj The name of the argument. This also can be an * object of type TaskArgument. + * + * @throws InvalidArgumentException If provided argument is not a string or an object of type + * 'TaskArgument'. * * @since 1.0 */ @@ -214,7 +218,7 @@ public function addExecutionArg($nameOrObj) { } else if ($nameOrObj instanceof TaskArgument) { $arg = $nameOrObj; } else { - return; + throw new InvalidArgumentException('Invalid argument type. Expected \'string\' or \''.TaskArgument::class.'\'');; } if (!$this->hasArg($arg->getName())) { diff --git a/webfiori/framework/writers/CLICommandClassWriter.php b/webfiori/framework/writers/CLICommandClassWriter.php index 511e9880..d233cfe4 100644 --- a/webfiori/framework/writers/CLICommandClassWriter.php +++ b/webfiori/framework/writers/CLICommandClassWriter.php @@ -94,7 +94,7 @@ public function setArgs(array $argsArr) { public function setCommandDescription(string $desc) { $trimmed = trim($desc); - if (strlen($desc) == 0) { + if (strlen($trimmed) == 0) { return; } $this->desc = $trimmed;