Skip to content

Commit

Permalink
enhance: Added More Test Cases for JSON Driver
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Oct 9, 2024
1 parent 790ffc8 commit 26371a9
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 10 deletions.
156 changes: 155 additions & 1 deletion tests/webfiori/framework/test/config/JsonDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
use PHPUnit\Framework\TestCase;
use webfiori\database\ConnectionInfo;
use webfiori\email\SMTPAccount;
use webfiori\file\File;
use webfiori\framework\config\JsonDriver;
use const DS;
/**
*
* @author Ibrahim
Expand Down Expand Up @@ -52,6 +54,9 @@ public function test00() {
'AR' => 'افتراضي',
'EN' => 'Default'
],$driver->getTitles());
$this->assertEquals('Application', $driver->getAppName('EN'));
$this->assertEquals('تطبيق', $driver->getAppName('AR'));
$this->assertNull($driver->getAppName('BK'));
}
/**
* @test
Expand All @@ -78,6 +83,11 @@ public function testAppNames00() {
'AR' => 'تطبيق',
'EN' => 'Cool App'
],$driver->getAppNames());
$driver->setAppName('Cool App', 'Enx');
$this->assertEquals([
'AR' => 'تطبيق',
'EN' => 'Cool App'
],$driver->getAppNames());
$driver->initialize();
}
/**
Expand All @@ -95,7 +105,9 @@ public function testAppNames01() {
'AR' => 'تطبيق',
'EN' => 'Cool App'
],$driver->getAppNames());
$this->assertTrue(File::isFileExist(JsonDriver::JSON_CONFIG_FILE_PATH.DS.$driver->getConfigFileName().'.json'));
$driver->remove();
$this->assertFalse(File::isFileExist(JsonDriver::JSON_CONFIG_FILE_PATH.DS.$driver->getConfigFileName().'.json'));
}
/**
* @test
Expand Down Expand Up @@ -408,6 +420,60 @@ public function testSMTPConnections02() {
$this->assertEquals('[email protected]', $account->getServerAddress());
$this->assertEquals('[email protected]', $account->getUsername());
}
/**
* @test
* @depends testSMTPConnections02
*/
public function testSMTPConnections03() {
$driver = new JsonDriver();
$driver->initialize();
$this->assertEquals(1, count($driver->getSMTPConnections()));
$driver->removeSMTPAccount('Cool');
$this->assertEquals(0, count($driver->getSMTPConnections()));
}
/**
* @test
*/
public function testSMTPConnections04() {
$driver = new JsonDriver();
$this->assertEquals(0, count($driver->getSMTPConnections()));
$this->assertNull($driver->getSMTPConnection('olf'));
$conn = new SMTPAccount([
'port' => 6,
'server-address' => '[email protected]',
'user' => '[email protected]',
'pass' => 'some_pass',
'sender-name' => 'WebFiori',
'sender-address' => '[email protected]',
'account-name' => 'Cool'
]);
$driver->addOrUpdateSMTPAccount($conn);
$this->assertEquals(1, count($driver->getSMTPConnections()));
$conn = new SMTPAccount([
'port' => 6,
'server-address' => '[email protected]',
'user' => '[email protected]',
'pass' => 'some_pass',
'sender-name' => 'WebFiori',
'sender-address' => '[email protected]',
'account-name' => 'Cool2'
]);
$driver->addOrUpdateSMTPAccount($conn);
$this->assertEquals(2, count($driver->getSMTPConnections()));
$account =$driver->getSMTPConnection('Cool');
$this->assertEquals(6, $account->getPort());
$this->assertEquals('Cool', $account->getAccountName());
$this->assertEquals('[email protected]', $account->getAddress());
$this->assertEquals('WebFiori', $account->getSenderName());
$this->assertEquals('[email protected]', $account->getServerAddress());
$this->assertEquals('[email protected]', $account->getUsername());
$driver->removeSMTPAccount('Cool');
$this->assertEquals(1, count($driver->getSMTPConnections()));
$account = $driver->getSMTPConnection('Cool');
$this->assertNull($account);
$account = $driver->getSMTPConnection('Cool2');
$this->assertNotNull($account);
}
/**
* @test
*/
Expand Down Expand Up @@ -463,6 +529,42 @@ public function testDatabaseConnections02() {
'KG' => 9,
'OP' => 'hello'
], $account->getExtars());
$driver->removeAllDBConnections();
$this->assertEquals(0, count($driver->getDBConnections()));
}
/**
* @test
*/
public function testDatabaseConnections03() {
$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);
$conn->setName('ok');
$driver->addOrUpdateDBConnection($conn);
$this->assertEquals(1, count($driver->getDBConnections()));
$conn = new ConnectionInfo('mysql', 'root', 'test@222', 'my_db', 'localhost', 3306);
$conn->setName('not_ok');
$conn->setExtras([
'A' => 'B',
'C' => 'D'
]);
$driver->addOrUpdateDBConnection($conn);
$this->assertEquals(2, count($driver->getDBConnections()));
$driver->removeDBConnection('ok');
$this->assertEquals(1, count($driver->getDBConnections()));

$account = $driver->getDBConnection('not_ok');
$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([
'A' => 'B',
'C' => 'D'
], $account->getExtars());
}
/**
* @test
Expand All @@ -473,6 +575,58 @@ public function testAppWithError00() {
$driver = new JsonDriver();
$driver->initialize();
$driver->getDBConnections();
JsonDriver::setConfigFileName('app-config');
}
/**
* @test
*/
public function testSchedulerPass00() {
$driver = new JsonDriver();
$driver->setConfigFileName('app-config.json');
$driver->initialize(true);
$this->assertEquals('NO_PASSWORD', $driver->getSchedulerPassword());
$driver->setSchedulerPassword(hash('sha256', '123'));
$this->assertEquals(hash('sha256', '123'), $driver->getSchedulerPassword());
$driver->setSchedulerPassword('');
$this->assertEquals('NO_PASSWORD', $driver->getSchedulerPassword());
}
/**
* @test
*/
public function testHomePage00() {
$driver = new JsonDriver();
$driver->setConfigFileName('app-config.json');
$driver->initialize(true);
$this->assertEquals('https://127.0.0.1', $driver->getHomePage());
$driver->setHomePage('https://home.com/my-page');
$this->assertEquals('https://home.com/my-page', $driver->getHomePage());
$driver->setHomePage('');
$this->assertEquals('https://127.0.0.1', $driver->getHomePage());
}
/**
* @test
*/
public function testBase00() {
$driver = new JsonDriver();
$driver->setConfigFileName('app-config.json');
$driver->initialize(true);
$this->assertEquals('https://127.0.0.1', $driver->getBaseURL());
$driver->setBaseURL('https://home.com');
$this->assertEquals('https://home.com', $driver->getBaseURL());
$driver->setBaseURL('');
$this->assertEquals('https://127.0.0.1', $driver->getBaseURL());
}
/**
* @test
*/
public function testSetTheme00() {
$driver = new JsonDriver();
$driver->setConfigFileName('app-config.json');
$driver->initialize(true);
$this->assertEquals('', $driver->getTheme());
$driver->setTheme('Test Theme');
$this->assertEquals('Test Theme', $driver->getTheme());
$driver->setTheme('');
$this->assertEquals('', $driver->getTheme());
}

}
107 changes: 98 additions & 9 deletions webfiori/framework/config/JsonDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ public function addOrUpdateSMTPAccount(SMTPAccount $emailAccount) {
$this->json->get('smtp-connections')->add($emailAccount->getAccountName(), $connectionAsJson);
$this->writeJson();
}

/**
* Returns the name of the application in specific display language.
*
* @param string $langCode Language code such as 'AR'.
*
* @return string|null Application name or null if language code does not
* exist.
*/
public function getAppName(string $langCode) {
return $this->json->get('app-names')->get(strtoupper(trim($langCode)));
}
Expand Down Expand Up @@ -324,7 +331,14 @@ public function getPrimaryLanguage(): string {
* password.
*/
public function getSchedulerPassword(): string {
return $this->json->get('scheduler-password') ?? 'NO_PASSWORD';
$pass = $this->json->get('scheduler-password') ?? 'NO_PASSWORD';

if (strlen($pass.'') == 0 || $pass == 'NO_PASSWORD') {

return 'NO_PASSWORD';
}

return $pass;
}
/**
* Returns SMTP connection given its name.
Expand Down Expand Up @@ -364,10 +378,12 @@ public function getSMTPConnections(): array {
$accountsInfo = $this->json->get('smtp-connections');
$retVal = [];

foreach ($accountsInfo->getProperties() as $name => $prop) {
foreach ($accountsInfo->getProperties() as $prop) {
$name = $prop->getName();
$jsonObj = $prop->getValue();
$acc = new SMTPAccount();
$acc->setAccountName($name);

$acc->setAddress($this->getProp($jsonObj, 'address', $name));
$acc->setPassword($this->getProp($jsonObj, 'password', $name));
$acc->setPort($this->getProp($jsonObj, 'port', $name));
Expand Down Expand Up @@ -441,6 +457,15 @@ public function getTitles(): array {
public function getTitleSeparator(): string {
return $this->json->get('name-separator');
}
/**
* Creates application configuration file.
*
* This method will attempt to create a JSON configuration file in the folder
* 'config' of the application.
*
* @param bool $reCreate If this parameter is set to true and there already a configuration
* file with same name, it will be overriden.
*/
public function initialize(bool $reCreate = false) {
$path = self::JSON_CONFIG_FILE_PATH.self::getConfigFileName().'.json';

Expand All @@ -449,8 +474,14 @@ public function initialize(bool $reCreate = false) {
}
$this->json = Json::fromJsonFile($path);
}
/**
* Deletes configuration file.
*
* Note that in order to remove specific configuration file, its name must
* be set using the method JsonDriver::setConfigFileName()
*/
public function remove() {
$f = new File(self::JSON_CONFIG_FILE_PATH);
$f = new File(self::JSON_CONFIG_FILE_PATH.DIRECTORY_SEPARATOR.self::getConfigFileName().'.json');
$f->remove();
}
public function removeAllDBConnections() {
Expand All @@ -459,6 +490,21 @@ public function removeAllDBConnections() {
}

public function removeDBConnection(string $connectionName) {
$connections = $this->getDBConnections();
$accountNameTrimmed = trim($connectionName);
$toAdd = [];

foreach ($connections as $connection) {

if ($connection->getName() != $accountNameTrimmed) {
$toAdd[] = $connection;
}
}
$this->removeAllDBConnections();

foreach ($toAdd as $account) {
$this->addOrUpdateDBConnection($account);
}
}
/**
* Removes specific application environment variable given its name.
Expand All @@ -469,11 +515,35 @@ public function removeEnvVar(string $name) {
$this->json->get('env-vars')->remove($name);
$this->writeJson();
}

public function removeSMTPAccount(string $accountName) {
/**
* Removes all added SMTP connections.
*/
public function removeAllSMTPAccounts() {
$this->json->add('smtp-connections', new Json([], 'none', 'same'));
$this->writeJson();
}
/**
* Removes specific SMTP connection from the configuration given its name.
*
* @param string $accountName The name of the connection.
*/
public function removeSMTPAccount(string $accountName) {
$connections = $this->getSMTPConnections();
$accountNameTrimmed = trim($accountName);
$toAdd = [];

foreach ($connections as $connection) {

if ($connection->getAccountName() != $accountNameTrimmed) {
$toAdd[] = $connection;
}
}
$this->removeAllSMTPAccounts();

foreach ($toAdd as $account) {
$this->addOrUpdateSMTPAccount($account);
}
}
/**
* Sets or updates the name of the application for specific display language.
*
Expand Down Expand Up @@ -510,8 +580,21 @@ public function setAppVersion(string $vNum, string $vType, string $releaseDate)
], 'none', 'same'));
$this->writeJson();
}

/**
* Sets the base URL of the application.
*
* This is usually used in fetching resources.
*
* @param string $url
*/
public function setBaseURL(string $url) {
$trim = trim($url);

if (strlen($trim) == 0) {
$this->json->add('base-url', 'DYNAMIC');
} else {

Check warning on line 595 in webfiori/framework/config/JsonDriver.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/config/JsonDriver.php#L595

Added line #L595 was not covered by tests
$this->json->add('base-url', $trim);
}
}
/**
* Sets the name of the file that configuration values will be taken from.
Expand Down Expand Up @@ -556,7 +639,13 @@ public function setDescription(string $description, string $langCode) {
* This page is served when the user visits the domain without specifying a path.
*/
public function setHomePage(string $url) {
$this->json->add('home-page', $url);
$trim = trim($url);

if (strlen($trim) == 0) {
$this->json->add('home-page', 'BASE_URL');
} else {

Check warning on line 646 in webfiori/framework/config/JsonDriver.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/config/JsonDriver.php#L646

Added line #L646 was not covered by tests
$this->json->add('home-page', $trim);
}
$this->writeJson();
}
/**
Expand Down Expand Up @@ -597,7 +686,7 @@ public function setSchedulerPassword(string $newPass) {
* website UI. This can also be class name of the theme.
*/
public function setTheme(string $theme) {
$this->json->add('theme', $theme);
$this->json->add('theme', trim($theme));
$this->writeJson();
}
/**
Expand Down

0 comments on commit 26371a9

Please sign in to comment.