Skip to content
Snippets Groups Projects
Commit 001f1761 authored by Stein Magne Bjorklund's avatar Stein Magne Bjorklund
Browse files

Wrap global config in a config class

- Inject this into the needed classes and get
away from globals.
parent db8fd1e5
No related branches found
No related tags found
1 merge request!18Resolve "Remove all use of GLOBALS"
<?php declare(strict_types=1);
namespace uib\ub\loadspeakr;
final class Configuration
{
private array $configs;
/**
* @throws \Exception
*/
public function __construct(array $conf)
{
if (count($conf) === 0) {
throw new \Exception(
'Configuration empty. Giving up.'
);
}
$this->configs['conf'] = $conf;
}
public function getConfigValue(string $type, string $key)
{
if (!isset($this->configs[$type][$key])) {
return '';
}
return $this->configs[$type][$key];
}
public function debugMode(): bool
{
return $this->configs['conf']['debug'];
}
}
......@@ -13,8 +13,11 @@ const LOADSPEAKR_ROOT = __DIR__;
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/common.inc.php';
global $conf;
$configuration = new uib\ub\loadspeakr\Configuration($conf);
if (isset($_GET['q']) && $_GET['q'] === 'import') {
$imp = new Importer();
$imp = new Importer($configuration);
$imp->run();
exit(0);
}
......
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use uib\ub\loadspeakr\Configuration;
final class ConfigurationTest extends TestCase
{
public Configuration $configuration;
public function setUp(): void
{
$conf = [
'foo' => 'bar',
'debug' => true,
'multiple' => ['one', 'two']
];
$this->configuration = new Configuration($conf);
}
/**
* @covers \uib\ub\loadspeakr\Configuration::getConfigValue
*/
public function testGetConfigValue(): void
{
self::assertSame('bar', $this->configuration->getConfigValue('conf', 'foo'));
self::assertCount(2, $this->configuration->getConfigValue('conf', 'multiple'),
'Unable to get settings storing multiple values.'
);
}
/**
* @covers \uib\ub\loadspeakr\Configuration::debugMode
*/
public function testDebugMode(): void
{
self::assertTrue($this->configuration->debugMode());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment