Skip to content

Commit

Permalink
Merge pull request #197 from WebFiori/dev
Browse files Browse the repository at this point in the history
Enhanced How Missing Connection Properties are Shown in `JsonDriver`
  • Loading branch information
usernane authored Oct 30, 2023
2 parents debbcff + 13750f3 commit 9e0fa84
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ php-cs-fixer-v2.phar
app/sto
app/config/*
.idea/*
!/app/config/config-with-err-00.json
48 changes: 48 additions & 0 deletions app/config/config-with-err-00.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"base-url":"DYNAMIC",
"theme":null,
"home-page":"BASE_URL",
"primary-lang":"EN",
"titles":{
"AR":"افتراضي",
"EN":"Default"
},
"name-separator":"|",
"scheduler-password":"NO_PASSWORD",
"app-names":{
"AR":"تطبيق",
"EN":"Application"
},
"app-descriptions":{
"AR":"",
"EN":""
},
"version-info":{
"version":"1.0",
"version-type":"Stable",
"release-date":"2023-10-30"
},
"env-vars":{
"WF_VERBOSE":{
"value":false,
"description":"Configure the verbosity of error messsages at run-time. This should be set to true in testing and false in production."
},
"CLI_HTTP_HOST":{
"value":"example.com",
"description":"Host name that will be used when runing the application as command line utility."
}
},
"smtp-connections":{
},
"database-connections":{
"New_Connection":{
"type":"mysql",
"host":"localhost",
"port":3306,
"database":"my_db",
"password":"test@222",
"extras":{
}
}
}
}
50 changes: 50 additions & 0 deletions tests/webfiori/framework/test/cli/RunSQLCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use tables\Schema2;
use webfiori\database\ConnectionInfo;
use webfiori\framework\App;
use webfiori\framework\config\Controller;
use webfiori\framework\config\JsonDriver;

class RunSQLCommandTest extends TestCase {
/**
Expand Down Expand Up @@ -82,6 +84,54 @@ public function testCLIQuery01() {
"Error: 1051 - Unknown table 'testing_db.test2_x'\n"
], $runner->getOutput());
}
/**
* @test
*/
public function testCLIQuery02() {

JsonDriver::setConfigFileName('run-sql-test');
App::setConfigDriver(JsonDriver::class);

$conn = new ConnectionInfo('mysql', 'root', '123456', 'testing_db', '127.0.0.1');
$conn->setName('testing-connection');
App::getConfig()->addOrUpdateDBConnection($conn);
$driver = new JsonDriver();
$driver->setConfigFileName('run-sql-test');

Controller::setDriver($driver);

$this->assertTrue(get_class(App::getConfig()) == JsonDriver::class);

$runner = App::getRunner();
$runner->setArgsVector([
'webfiori',
'run-query',
]);
$runner->setInputs([
'0',
'0',
'select * from hello;',
'y'
]);


$this->assertEquals(1146, $runner->start());

$this->assertEquals([
"Select database connection:\n",
"0: testing-connection <--\n",
"What type of query you would like to run?\n",
"0: Run general query.\n",
"1: Run query on table instance.\n",
"2: Run query from file.\n",
"Please type in SQL query:\n",
"The following query will be executed on the database 'testing_db':\n",
"select * from hello;\n",
"Continue?(Y/n)\n",
"Info: Executing query on database testing_db...\n",
"Error: 1146 - Table 'testing_db.hello' doesn't exist\n"
], $runner->getOutput());
}
/**
* @test
*/
Expand Down
13 changes: 12 additions & 1 deletion tests/webfiori/framework/test/config/JsonDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function test00() {
* @test
*/
public function testSetConfigFileName00() {
JsonDriver::setConfigFileName('app-config.json');
$this->assertEquals('app-config', JsonDriver::getConfigFileName());
JsonDriver::setConfigFileName('super-conf.json');
$this->assertEquals('super-conf', JsonDriver::getConfigFileName());
Expand Down Expand Up @@ -425,5 +426,15 @@ public function testDatabaseConnections01() {
$this->assertEquals('root', $account->getUsername());

}

/**
* @test
* @depends testSetConfigFileName00
*/
public function testAppWithError00() {
$this->expectExceptionMessage('The property "username" of the connection "New_Connection" is missing.');
JsonDriver::setConfigFileName('config-with-err-00');
$driver = new JsonDriver();
$driver->initialize();
$driver->getDBConnections();
}
}
20 changes: 12 additions & 8 deletions webfiori/framework/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function getConfigDriver() : string {
*
* @var string
*/
private static $ConfigDriver = '\\webfiori\\framework\\config\\ClassDriver';
private static $ConfigDriver = 'webfiori\\framework\\config\\ClassDriver';
/**
* A constant that indicates that the status of the class is 'initialized'.
*
Expand Down Expand Up @@ -136,7 +136,7 @@ private function __construct() {
mb_regex_encoding($encoding);
}
$this->initAutoLoader();
$this->loadEnvVars();
Controller::get()->updateEnv();
/**
* Set memory limit.
*/
Expand Down Expand Up @@ -294,7 +294,14 @@ public static function getClassStatus() {
* @return ConfigurationDriver
*/
public static function getConfig(): ConfigurationDriver {
return Controller::getDriver();
$driver = Controller::getDriver();

if (get_class($driver) != self::$ConfigDriver) {
Controller::setDriver(new self::$ConfigDriver());
Controller::get()->updateEnv();
$driver = Controller::getDriver();
}
return $driver;
}

/**
Expand Down Expand Up @@ -533,7 +540,7 @@ private function initFrameworkVersionInfo() {
*
* @since 2.1
*/
define('WF_VERSION', '3.0.0-RC16');
define('WF_VERSION', '3.0.0-RC18');
/**
* A constant that tells the type of framework version.
*
Expand All @@ -549,7 +556,7 @@ private function initFrameworkVersionInfo() {
*
* @since 2.1
*/
define('WF_RELEASE_DATE', '2023-09-11');
define('WF_RELEASE_DATE', '2023-10-30');
}

/**
Expand Down Expand Up @@ -621,9 +628,6 @@ private function initThemesPath() {
define('THEMES_PATH', $themesPath);
}
}
private function loadEnvVars() {
Controller::get()->updateEnv();
}
/**
* Sets new error and exception handler.
*/
Expand Down
2 changes: 1 addition & 1 deletion webfiori/framework/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct($connName) {
}

if (!($conn instanceof ConnectionInfo)) {
throw new DatabaseException("No connection was found which has the name '$connName'.");
throw new DatabaseException("No connection was found which has the name '$connName'. Driver: ". get_class(App::getConfig()).'.');
}
}
parent::__construct($conn);
Expand Down
80 changes: 59 additions & 21 deletions webfiori/framework/config/JsonDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use webfiori\database\ConnectionInfo;
use webfiori\email\SMTPAccount;
use webfiori\file\File;
use webfiori\framework\exceptions\InitializationException;
use webfiori\http\Uri;
use webfiori\json\Json;

Expand Down Expand Up @@ -134,8 +135,8 @@ public function addOrUpdateDBConnection(ConnectionInfo $dbConnectionsInfo) {
'username' => $dbConnectionsInfo->getUsername(),
'database' => $dbConnectionsInfo->getDBName(),
'password' => $dbConnectionsInfo->getPassword(),
'extars' => $dbConnectionsInfo->getExtars(),
]);
$connectionJAsJson->addArray('extras', $dbConnectionsInfo->getExtars(), true);
$this->json->get('database-connections')->add($dbConnectionsInfo->getName(), $connectionJAsJson);
$this->writeJson();
}
Expand Down Expand Up @@ -207,33 +208,66 @@ public function getDBConnection(string $conName) {
$jsonObj = $this->json->get('database-connections')->get($conName);

if ($jsonObj !== null) {
$extras = $jsonObj->get('extras');
$extrasArr = [];
if ($extras instanceof Json) {
foreach ($extras->getProperties() as $prop) {
$extrasArr[$prop->getName()] = $prop->getValue();
}
}
return new ConnectionInfo(
$jsonObj->get('type'),
$jsonObj->get('username'),
$jsonObj->get('password'),
$jsonObj->get('database'),
$jsonObj->get('host'),
$jsonObj->get('port'),
$jsonObj->get('extras') !== null ? $jsonObj->get('extras') : []);
$extrasArr);
}
}

/**
* Returns an associative array that contain the information of database connections.
*
* @return array An associative array. The indices are connections names and
* values are objects of type 'ConnectionInfo'.
*/
public function getDBConnections(): array {
$accountsInfo = $this->json->get('database-connections');
$retVal = [];

foreach ($accountsInfo->getProperties() as $propObj) {
$name = $propObj->getName();
$jsonObj = $propObj->getValue();
$acc = new ConnectionInfo($jsonObj->get('type'), $jsonObj->get('username'), $jsonObj->get('password'), $jsonObj->get('database'));
$acc->setExtras($jsonObj->get('extras') !== null ? $jsonObj->get('extras') : []);
$acc->setHost($jsonObj->get('host'));
$acc = new ConnectionInfo(
$this->getProp($jsonObj, 'type', $name),
$this->getProp($jsonObj, 'username', $name),
$this->getProp($jsonObj, 'password', $name),
$this->getProp($jsonObj, 'database', $name));
$extrasObj = $jsonObj->get('extras');

if ($extrasObj !== null && $extrasObj instanceof Json) {
$extrasArr = [];

foreach ($extrasObj->getProperties() as $prop) {
$extrasArr[$prop->getName()] = $prop->getValue();
}
$acc->setExtras($extrasArr);
}
$acc->setHost($this->getProp($jsonObj, 'host', $name));
$acc->setName($propObj->getName());
$acc->setPort($jsonObj->get('port'));
$acc->setPort($this->getProp($jsonObj, 'port', $name));
$retVal[$propObj->getName()] = $acc;
}

return $retVal;
}
private function getProp(Json $j, $name, string $connName) {
$val = $j->get($name);
if ($val === null) {
throw new InitializationException('The property "'.$name.'" of the connection "'.$connName.'" is missing.');
}
return $val;
}

public function getDescription(string $langCode) {
return $this->json->get('app-descriptions')->get(strtoupper(trim($langCode)));
Expand Down Expand Up @@ -321,17 +355,21 @@ public function getSMTPConnection(string $name) {

if ($jsonObj !== null) {
return new SMTPAccount([
'sender-address' => $jsonObj->get('address'),
'pass' => $jsonObj->get('password'),
'port' => $jsonObj->get('port'),
'sender-name' => $jsonObj->get('sender-name'),
'server-address' => $jsonObj->get('host'),
'user' => $jsonObj->get('username'),
'sender-address' => $this->getProp($jsonObj, 'address', $name),
'pass' => $this->getProp($jsonObj, 'password', $name),
'port' => $this->getProp($jsonObj, 'port', $name),
'sender-name' => $this->getProp($jsonObj, 'sender-name', $name),
'server-address' => $this->getProp($jsonObj, 'host', $name),
'user' => $this->getProp($jsonObj, 'username', $name),
'account-name' => $name
]);
}
}

/**
* Returns an array that contains all added SMTP accounts.
*
* @return array An array that contains all added SMTP accounts.
*/
public function getSMTPConnections(): array {
$accountsInfo = $this->json->get('smtp-connections');
$retVal = [];
Expand All @@ -340,13 +378,13 @@ public function getSMTPConnections(): array {
$jsonObj = $prop->getValue();
$acc = new SMTPAccount();
$acc->setAccountName($name);
$acc->setAddress($jsonObj->get('address'));
$acc->setPassword($jsonObj->get('password'));
$acc->setPort($jsonObj->get('port'));
$acc->setSenderName($jsonObj->get('sender-name'));
$acc->setServerAddress($jsonObj->get('host'));
$acc->setUsername($jsonObj->get('username'));
$retVal[] = $acc;
$acc->setAddress($this->getProp($jsonObj, 'address', $name));
$acc->setPassword($this->getProp($jsonObj, 'password', $name));
$acc->setPort($this->getProp($jsonObj, 'port', $name));
$acc->setSenderName($this->getProp($jsonObj, 'sender-name', $name));
$acc->setServerAddress($this->getProp($jsonObj, 'host', $name));
$acc->setUsername($this->getProp($jsonObj, 'username', $name));
$retVal[$name] = $acc;
}

return $retVal;
Expand Down
2 changes: 1 addition & 1 deletion webfiori/framework/session/DatabaseSessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct() {
try {
$this->dbController = new SessionDB();
} catch (DatabaseException $ex) {
if ($ex->getMessage() == "No connection was found which has the name 'sessions-connection'.") {
if (strpos($ex->getMessage(), 'sessions-connection') !== false) {
throw new SessionException("Connection 'sessions-connection' was not found in application configuration.");
} else {
throw $ex;
Expand Down

0 comments on commit 9e0fa84

Please sign in to comment.