mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Refactor tests (#10625)
* Refactor tests Boot Laravel for all tests. Config use private static property for storage instead of global * Backup/restore modules * disable snmpsim log * Fixing DBTestCase * Fix macros loading to the wrong place * trap and other tests should check if db is available * don't include snmp.inc.php if mock.snmp.inc.php is already included... * fix migration * if we don't reset the db, run migrations at least. * set vars for migrate too * Fix style * ignore issues with undefined indexes in legacy code
This commit is contained in:
@@ -27,9 +27,19 @@ namespace LibreNMS\Tests;
|
||||
|
||||
use LibreNMS\Config;
|
||||
use LibreNMS\DB\Eloquent;
|
||||
use ReflectionClass;
|
||||
|
||||
class ConfigTest extends LaravelTestCase
|
||||
class ConfigTest extends TestCase
|
||||
{
|
||||
private $config;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->config = new \ReflectionProperty(Config::class, 'config');
|
||||
$this->config->setAccessible(true);
|
||||
}
|
||||
|
||||
public function testGetBasic()
|
||||
{
|
||||
$dir = realpath(__DIR__ . '/..');
|
||||
@@ -38,26 +48,27 @@ class ConfigTest extends LaravelTestCase
|
||||
|
||||
public function testSetBasic()
|
||||
{
|
||||
global $config;
|
||||
Config::set('basics', 'first');
|
||||
$this->assertEquals('first', $config['basics']);
|
||||
$this->assertEquals('first', $this->config->getValue()['basics']);
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
global $config;
|
||||
$config['one']['two']['three'] = 'easy';
|
||||
$this->setConfig(function (&$config) {
|
||||
$config['one']['two']['three'] = 'easy';
|
||||
});
|
||||
|
||||
$this->assertEquals('easy', Config::get('one.two.three'));
|
||||
}
|
||||
|
||||
public function testGetDeviceSetting()
|
||||
{
|
||||
global $config;
|
||||
$device = array('set' => true, 'null' => null);
|
||||
$config['null'] = 'notnull!';
|
||||
$config['noprefix'] = true;
|
||||
$config['prefix']['global'] = true;
|
||||
$this->setConfig(function (&$config) {
|
||||
$config['null'] = 'notnull!';
|
||||
$config['noprefix'] = true;
|
||||
$config['prefix']['global'] = true;
|
||||
});
|
||||
|
||||
$this->assertNull(Config::getDeviceSetting($device, 'unset'), 'Non-existing settings should return null');
|
||||
$this->assertTrue(Config::getDeviceSetting($device, 'set'), 'Could not get setting from device array');
|
||||
@@ -80,9 +91,10 @@ class ConfigTest extends LaravelTestCase
|
||||
|
||||
public function testGetOsSetting()
|
||||
{
|
||||
global $config;
|
||||
$config['os']['nullos']['fancy'] = true;
|
||||
$config['fallback'] = true;
|
||||
$this->setConfig(function (&$config) {
|
||||
$config['os']['nullos']['fancy'] = true;
|
||||
$config['fallback'] = true;
|
||||
});
|
||||
|
||||
$this->assertNull(Config::getOsSetting(null, 'unset'), '$os is null, should return null');
|
||||
$this->assertNull(Config::getOsSetting('nullos', 'unset'), 'Non-existing settings should return null');
|
||||
@@ -93,19 +105,20 @@ class ConfigTest extends LaravelTestCase
|
||||
|
||||
public function testGetCombined()
|
||||
{
|
||||
global $config;
|
||||
$config['num'] = array('one', 'two');
|
||||
$config['os']['nullos']['num'] = array('two', 'three');
|
||||
$config['assoc'] = array('a' => 'same', 'b' => 'same');
|
||||
$config['os']['nullos']['assoc'] = array('b' => 'different', 'c' => 'still same');
|
||||
$config['os']['nullos']['osset'] = true;
|
||||
$config['gset'] = true;
|
||||
$this->setConfig(function (&$config) {
|
||||
$config['num'] = array('one', 'two');
|
||||
$config['os']['nullos']['num'] = array('two', 'three');
|
||||
$config['assoc'] = array('a' => 'same', 'b' => 'same');
|
||||
$config['os']['nullos']['assoc'] = array('b' => 'different', 'c' => 'still same');
|
||||
$config['os']['nullos']['osset'] = true;
|
||||
$config['gset'] = true;
|
||||
});
|
||||
|
||||
$this->assertTrue(Config::getCombined('nullos', 'non-existent', true), 'Did not return default value on non-existent key');
|
||||
$this->assertTrue(Config::getCombined('nullos', 'osset', false), 'Did not return OS value when global value is not set');
|
||||
$this->assertTrue(Config::getCombined('nullos', 'gset', false), 'Did not return global value when OS value is not set');
|
||||
|
||||
$combined = Config::getCombined('nullos', 'num');
|
||||
$combined = Config::getCombined('nullos', 'num');
|
||||
sort($combined);
|
||||
$this->assertEquals(array('one', 'three', 'two'), $combined);
|
||||
|
||||
@@ -114,10 +127,9 @@ class ConfigTest extends LaravelTestCase
|
||||
|
||||
public function testSet()
|
||||
{
|
||||
global $config;
|
||||
Config::set('you.and.me', "I'll be there");
|
||||
|
||||
$this->assertEquals("I'll be there", $config['you']['and']['me']);
|
||||
$this->assertEquals("I'll be there", $this->config->getValue()['you']['and']['me']);
|
||||
}
|
||||
|
||||
public function testSetPersist()
|
||||
@@ -164,7 +176,6 @@ class ConfigTest extends LaravelTestCase
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function testGetSubtree()
|
||||
{
|
||||
Config::set('words.top', 'August');
|
||||
@@ -178,4 +189,15 @@ class ConfigTest extends LaravelTestCase
|
||||
|
||||
$this->assertEquals($expected, Config::get('words'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass an anonymous function which will be passed the config variable to modify before it is set
|
||||
* @param callable $function
|
||||
*/
|
||||
private function setConfig($function)
|
||||
{
|
||||
$config = $this->config->getValue();
|
||||
$function($config);
|
||||
$this->config->setValue($config);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user