2016-05-18 21:43:59 +02:00
|
|
|
<?php
|
2017-03-03 23:06:12 +01:00
|
|
|
namespace Shaarli\Config;
|
2016-05-18 21:43:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for Class ConfigManagerTest
|
|
|
|
*
|
|
|
|
* Note: it only test the manager with ConfigJson,
|
|
|
|
* ConfigPhp is only a workaround to handle the transition to JSON type.
|
|
|
|
*/
|
2019-01-12 23:55:38 +01:00
|
|
|
class ConfigManagerTest extends \PHPUnit\Framework\TestCase
|
2016-05-18 21:43:59 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ConfigManager
|
|
|
|
*/
|
|
|
|
protected $conf;
|
|
|
|
|
2020-09-26 15:08:39 +02:00
|
|
|
protected function setUp(): void
|
2016-05-18 21:43:59 +02:00
|
|
|
{
|
2016-06-09 20:04:02 +02:00
|
|
|
$this->conf = new ConfigManager('tests/utils/config/configJson');
|
2016-05-18 21:43:59 +02:00
|
|
|
}
|
|
|
|
|
2016-05-29 12:32:14 +02:00
|
|
|
/**
|
|
|
|
* Simple config test:
|
|
|
|
* 1. Set settings.
|
|
|
|
* 2. Check settings value.
|
|
|
|
*/
|
|
|
|
public function testSetGet()
|
2016-05-18 21:43:59 +02:00
|
|
|
{
|
2016-05-29 12:32:14 +02:00
|
|
|
$this->conf->set('paramInt', 42);
|
|
|
|
$this->conf->set('paramString', 'value1');
|
|
|
|
$this->conf->set('paramBool', false);
|
|
|
|
$this->conf->set('paramArray', array('foo' => 'bar'));
|
|
|
|
$this->conf->set('paramNull', null);
|
|
|
|
|
|
|
|
$this->assertEquals(42, $this->conf->get('paramInt'));
|
|
|
|
$this->assertEquals('value1', $this->conf->get('paramString'));
|
|
|
|
$this->assertFalse($this->conf->get('paramBool'));
|
|
|
|
$this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray'));
|
|
|
|
$this->assertEquals(null, $this->conf->get('paramNull'));
|
2016-05-18 21:43:59 +02:00
|
|
|
}
|
|
|
|
|
2016-05-29 12:32:14 +02:00
|
|
|
/**
|
|
|
|
* Set/write/get config test:
|
|
|
|
* 1. Set settings.
|
|
|
|
* 2. Write it to the config file.
|
|
|
|
* 3. Read the file.
|
|
|
|
* 4. Check settings value.
|
|
|
|
*/
|
2016-05-18 21:43:59 +02:00
|
|
|
public function testSetWriteGet()
|
|
|
|
{
|
|
|
|
$this->conf->set('paramInt', 42);
|
|
|
|
$this->conf->set('paramString', 'value1');
|
|
|
|
$this->conf->set('paramBool', false);
|
|
|
|
$this->conf->set('paramArray', array('foo' => 'bar'));
|
|
|
|
$this->conf->set('paramNull', null);
|
|
|
|
|
2016-06-09 20:04:02 +02:00
|
|
|
$this->conf->setConfigFile('tests/utils/config/configTmp');
|
2016-05-18 21:43:59 +02:00
|
|
|
$this->conf->write(true);
|
|
|
|
$this->conf->reload();
|
2016-06-09 20:04:02 +02:00
|
|
|
unlink($this->conf->getConfigFileExt());
|
2016-05-18 21:43:59 +02:00
|
|
|
|
|
|
|
$this->assertEquals(42, $this->conf->get('paramInt'));
|
|
|
|
$this->assertEquals('value1', $this->conf->get('paramString'));
|
|
|
|
$this->assertFalse($this->conf->get('paramBool'));
|
|
|
|
$this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray'));
|
|
|
|
$this->assertEquals(null, $this->conf->get('paramNull'));
|
|
|
|
}
|
2016-05-29 12:32:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test set/write/get with nested keys.
|
|
|
|
*/
|
|
|
|
public function testSetWriteGetNested()
|
|
|
|
{
|
|
|
|
$this->conf->set('foo.bar.key.stuff', 'testSetWriteGetNested');
|
|
|
|
|
2016-06-09 20:04:02 +02:00
|
|
|
$this->conf->setConfigFile('tests/utils/config/configTmp');
|
2016-05-29 12:32:14 +02:00
|
|
|
$this->conf->write(true);
|
|
|
|
$this->conf->reload();
|
2016-06-09 20:04:02 +02:00
|
|
|
unlink($this->conf->getConfigFileExt());
|
2016-05-29 12:32:14 +02:00
|
|
|
|
|
|
|
$this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff'));
|
|
|
|
}
|
|
|
|
|
2017-11-11 14:00:18 +01:00
|
|
|
public function testSetDeleteNested()
|
|
|
|
{
|
|
|
|
$this->conf->set('foo.bar.key.stuff', 'testSetDeleteNested');
|
|
|
|
$this->assertTrue($this->conf->exists('foo.bar'));
|
|
|
|
$this->assertTrue($this->conf->exists('foo.bar.key.stuff'));
|
|
|
|
$this->assertEquals('testSetDeleteNested', $this->conf->get('foo.bar.key.stuff'));
|
|
|
|
|
|
|
|
$this->conf->remove('foo.bar');
|
|
|
|
$this->assertFalse($this->conf->exists('foo.bar.key.stuff'));
|
|
|
|
$this->assertFalse($this->conf->exists('foo.bar'));
|
|
|
|
}
|
|
|
|
|
2016-05-29 12:32:14 +02:00
|
|
|
/**
|
|
|
|
* Set with an empty key.
|
|
|
|
*
|
2017-03-03 23:06:12 +01:00
|
|
|
* @expectedException \Exception
|
2016-05-29 12:32:14 +02:00
|
|
|
* @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
|
|
|
|
*/
|
|
|
|
public function testSetEmptyKey()
|
|
|
|
{
|
|
|
|
$this->conf->set('', 'stuff');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set with an array key.
|
|
|
|
*
|
2017-03-03 23:06:12 +01:00
|
|
|
* @expectedException \Exception
|
2016-05-29 12:32:14 +02:00
|
|
|
* @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
|
|
|
|
*/
|
|
|
|
public function testSetArrayKey()
|
|
|
|
{
|
|
|
|
$this->conf->set(array('foo' => 'bar'), 'stuff');
|
|
|
|
}
|
|
|
|
|
2017-11-11 14:00:18 +01:00
|
|
|
/**
|
|
|
|
* Remove with an empty key.
|
|
|
|
*
|
|
|
|
* @expectedException \Exception
|
|
|
|
* @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
|
|
|
|
*/
|
|
|
|
public function testRmoveEmptyKey()
|
|
|
|
{
|
|
|
|
$this->conf->remove('');
|
|
|
|
}
|
|
|
|
|
2016-05-29 12:32:14 +02:00
|
|
|
/**
|
|
|
|
* Try to write the config without mandatory parameter (e.g. 'login').
|
|
|
|
*
|
2017-03-08 20:28:33 +01:00
|
|
|
* @expectedException Shaarli\Config\Exception\MissingFieldConfigException
|
2016-05-29 12:32:14 +02:00
|
|
|
*/
|
|
|
|
public function testWriteMissingParameter()
|
|
|
|
{
|
2016-06-09 20:04:02 +02:00
|
|
|
$this->conf->setConfigFile('tests/utils/config/configTmp');
|
|
|
|
$this->assertFalse(file_exists($this->conf->getConfigFileExt()));
|
2016-05-29 12:32:14 +02:00
|
|
|
$this->conf->reload();
|
|
|
|
|
|
|
|
$this->conf->write(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to get non existent config keys.
|
|
|
|
*/
|
|
|
|
public function testGetNonExistent()
|
|
|
|
{
|
|
|
|
$this->assertEquals('', $this->conf->get('nope.test'));
|
|
|
|
$this->assertEquals('default', $this->conf->get('nope.test', 'default'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the 'exists' method with existent values.
|
|
|
|
*/
|
|
|
|
public function testExistsOk()
|
|
|
|
{
|
2016-05-29 16:10:32 +02:00
|
|
|
$this->assertTrue($this->conf->exists('credentials.login'));
|
2016-05-29 12:32:14 +02:00
|
|
|
$this->assertTrue($this->conf->exists('config.foo'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the 'exists' method with non existent or invalid values.
|
|
|
|
*/
|
|
|
|
public function testExistsKo()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->conf->exists('nope'));
|
|
|
|
$this->assertFalse($this->conf->exists('nope.nope'));
|
|
|
|
$this->assertFalse($this->conf->exists(''));
|
|
|
|
$this->assertFalse($this->conf->exists(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the ConfigManager instance.
|
|
|
|
*/
|
|
|
|
public function testReset()
|
|
|
|
{
|
2016-06-09 20:04:02 +02:00
|
|
|
$confIO = $this->conf->getConfigIO();
|
|
|
|
$this->conf->reset();
|
|
|
|
$this->assertFalse($confIO === $this->conf->getConfigIO());
|
2016-05-29 12:32:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reload the config from file.
|
|
|
|
*/
|
|
|
|
public function testReload()
|
|
|
|
{
|
2016-06-09 20:04:02 +02:00
|
|
|
$this->conf->setConfigFile('tests/utils/config/configTmp');
|
2016-05-29 16:10:32 +02:00
|
|
|
$newConf = ConfigJson::getPhpHeaders() . '{ "key": "value" }';
|
2016-06-09 20:04:02 +02:00
|
|
|
file_put_contents($this->conf->getConfigFileExt(), $newConf);
|
2016-05-29 12:32:14 +02:00
|
|
|
$this->conf->reload();
|
2016-06-09 20:04:02 +02:00
|
|
|
unlink($this->conf->getConfigFileExt());
|
2016-05-29 12:32:14 +02:00
|
|
|
// Previous conf no longer exists, and new values have been loaded.
|
2016-05-29 16:10:32 +02:00
|
|
|
$this->assertFalse($this->conf->exists('credentials.login'));
|
2016-05-29 12:32:14 +02:00
|
|
|
$this->assertEquals('value', $this->conf->get('key'));
|
|
|
|
}
|
|
|
|
}
|