Skip to content

Commit

Permalink
mvc-unittests : remove Phalcon\Di\FactoryDefault usage and support pa…
Browse files Browse the repository at this point in the history
…rtial app config updates in AppConfig to support different config files in the unit tests.

for #6389
  • Loading branch information
AdSchellevis committed Apr 30, 2024
1 parent cbc01cb commit 100aac2
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 14 deletions.
21 changes: 21 additions & 0 deletions src/opnsense/mvc/app/config/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,25 @@ public function merge($cnf)
// simple conversion from array to StdClass container, current representation of self::$data.
self::$obj = json_decode(json_encode(self::$data));
}

/**
* update a property inside the container
* @param string $path in dot notation a.b.c
* @param mixed $value
* @return bool true when found and updated
*/
public function update($path, $value)
{
$tmp = &self::$data;
foreach (explode('.', $path) as $key) {
if (isset($tmp[$key])) {
$tmp = &$tmp[$key];
} else {
return false;
}
}
$tmp = $value;
self::$obj = json_decode(json_encode(self::$data));
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
// @CodingStandardsIgnoreEnd

use OPNsense\Base\FieldTypes\AuthGroupField;
use Phalcon\Di\FactoryDefault;
use OPNsense\Core\AppConfig;
use OPNsense\Core\Config;

class AuthGroupFieldTest extends Field_Framework_TestCase
Expand All @@ -47,7 +47,7 @@ public function testCanBeCreated()
{
$this->assertInstanceOf('\OPNsense\Base\FieldTypes\AuthGroupField', new AuthGroupField());
// switch config to test set for this type
FactoryDefault::getDefault()->get('config')->globals->config_path = __DIR__ . '/AuthGroupFieldTest/';
(new AppConfig())->update('globals.config_path', __DIR__ . '/AuthGroupFieldTest/');
Config::getInstance()->forceReload();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
// @CodingStandardsIgnoreEnd

use OPNsense\Base\FieldTypes\AuthenticationServerField;
use Phalcon\Di\FactoryDefault;
use OPNsense\Core\AppConfig;
use OPNsense\Core\Config;

class AuthenticationServerFieldTest extends Field_Framework_TestCase
Expand All @@ -47,7 +47,7 @@ public function testCanBeCreated()
{
$this->assertInstanceOf('\OPNsense\Base\FieldTypes\AuthenticationServerField', new AuthenticationServerField());
// switch config to test set for this type
FactoryDefault::getDefault()->get('config')->globals->config_path = __DIR__ . '/AuthenticationServerFieldTest/';
(new AppConfig())->update('globals.config_path', __DIR__ . '/AuthenticationServerFieldTest/');
Config::getInstance()->forceReload();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
// @CodingStandardsIgnoreEnd

use OPNsense\Base\FieldTypes\CertificateField;
use Phalcon\Di\FactoryDefault;
use OPNsense\Core\AppConfig;
use OPNsense\Core\Config;

class CertificateFieldTest extends Field_Framework_TestCase
Expand All @@ -47,7 +47,7 @@ public function testCanBeCreated()
{
$this->assertInstanceOf('\OPNsense\Base\FieldTypes\CertificateField', new CertificateField());
// switch config to test set for this type
FactoryDefault::getDefault()->get('config')->globals->config_path = __DIR__ . '/CertificateFieldTest/';
(new AppConfig())->update('globals.config_path', __DIR__ . '/CertificateFieldTest/');
Config::getInstance()->forceReload();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
// @CodingStandardsIgnoreEnd

use OPNsense\Base\FieldTypes\InterfaceField;
use Phalcon\Di\FactoryDefault;
use OPNsense\Core\AppConfig;
use OPNsense\Core\Config;

class InterfaceFieldTest extends Field_Framework_TestCase
Expand All @@ -47,7 +47,7 @@ public function testCanBeCreated()
{
$this->assertInstanceOf('\OPNsense\Base\FieldTypes\InterfaceField', new InterfaceField());
// switch config to test set for this type
FactoryDefault::getDefault()->get('config')->globals->config_path = __DIR__ . '/InterfaceFieldTest/';
(new AppConfig())->update('globals.config_path', __DIR__ . '/InterfaceFieldTest/');
Config::getInstance()->forceReload();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
// @CodingStandardsIgnoreEnd

use OPNsense\Base\FieldTypes\ModelRelationField;
use Phalcon\Di\FactoryDefault;
use OPNsense\Core\AppConfig;
use OPNsense\Core\Config;

class ModelRelationFieldTest extends Field_Framework_TestCase
{
protected function setUp(): void
{
FactoryDefault::getDefault()->get('config')->globals->config_path = __DIR__ . '/ModelRelationFieldTest/';
(new AppConfig())->update('globals.config_path', __DIR__ . '/ModelRelationFieldTest/');
Config::getInstance()->forceReload();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
// @CodingStandardsIgnoreEnd

use OPNsense\Base\FieldTypes\NetworkAliasField;
use Phalcon\Di\FactoryDefault;
use OPNsense\Core\AppConfig;
use OPNsense\Core\Config;

class NetworkAliasFieldTest extends Field_Framework_TestCase
Expand All @@ -47,7 +47,7 @@ public function testCanBeCreated()
{
$this->assertInstanceOf('\OPNsense\Base\FieldTypes\NetworkAliasField', new NetworkAliasField());
// switch config to test set for this type
FactoryDefault::getDefault()->get('config')->globals->config_path = __DIR__ . '/NetworkAliasFieldTest/';
(new AppConfig())->update('globals.config_path', __DIR__ . '/NetworkAliasFieldTest/');
Config::getInstance()->forceReload();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
// @CodingStandardsIgnoreEnd

use OPNsense\Base\FieldTypes\VirtualIPField;
use Phalcon\Di\FactoryDefault;
use OPNsense\Core\AppConfig;
use OPNsense\Core\Config;

class VirtualIPFieldTest extends Field_Framework_TestCase
Expand All @@ -47,7 +47,7 @@ public function testCanBeCreated()
{
$this->assertInstanceOf('\OPNsense\Base\FieldTypes\VirtualIPField', new VirtualIPField());
// switch config to test set for this type
FactoryDefault::getDefault()->get('config')->globals->config_path = __DIR__ . '/VirtualIPFieldTest/';
(new AppConfig())->update('globals.config_path', __DIR__ . '/VirtualIPFieldTest/');
Config::getInstance()->forceReload();
}

Expand Down

0 comments on commit 100aac2

Please sign in to comment.