]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - tests/config/ConfigManagerTest.php
Compatibility with PHPUnit 9
[github/shaarli/Shaarli.git] / tests / config / ConfigManagerTest.php
index 9ff0f473bbf373470e10400b125ecfafda3cbcad..65d8ba2c64f70aa58893a9a31f0ae0d08b42c2d5 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+namespace Shaarli\Config;
 
 /**
  * Unit tests for Class ConfigManagerTest
@@ -6,17 +7,16 @@
  * Note: it only test the manager with ConfigJson,
  *  ConfigPhp is only a workaround to handle the transition to JSON type.
  */
-class ConfigManagerTest extends PHPUnit_Framework_TestCase
+class ConfigManagerTest extends \Shaarli\TestCase
 {
     /**
      * @var ConfigManager
      */
     protected $conf;
 
-    public function setUp()
+    protected function setUp(): void
     {
-        ConfigManager::$CONFIG_FILE = 'tests/utils/config/configJson';
-        $this->conf = ConfigManager::reset();
+        $this->conf = new ConfigManager('tests/utils/config/configJson');
     }
 
     /**
@@ -54,10 +54,10 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase
         $this->conf->set('paramArray', array('foo' => 'bar'));
         $this->conf->set('paramNull', null);
 
-        ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp';
+        $this->conf->setConfigFile('tests/utils/config/configTmp');
         $this->conf->write(true);
         $this->conf->reload();
-        unlink($this->conf->getConfigFile());
+        unlink($this->conf->getConfigFileExt());
 
         $this->assertEquals(42, $this->conf->get('paramInt'));
         $this->assertEquals('value1', $this->conf->get('paramString'));
@@ -73,45 +73,68 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase
     {
         $this->conf->set('foo.bar.key.stuff', 'testSetWriteGetNested');
 
-        ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp';
+        $this->conf->setConfigFile('tests/utils/config/configTmp');
         $this->conf->write(true);
         $this->conf->reload();
-        unlink($this->conf->getConfigFile());
+        unlink($this->conf->getConfigFileExt());
 
         $this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff'));
     }
 
+    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'));
+    }
+
     /**
      * Set with an empty key.
-     *
-     * @expectedException Exception
-     * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
      */
     public function testSetEmptyKey()
     {
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessageRegExp('#^Invalid setting key parameter. String expected, got.*#');
+
         $this->conf->set('', 'stuff');
     }
 
     /**
      * Set with an array key.
-     *
-     * @expectedException Exception
-     * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
      */
     public function testSetArrayKey()
     {
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessageRegExp('#^Invalid setting key parameter. String expected, got.*#');
+
         $this->conf->set(array('foo' => 'bar'), 'stuff');
     }
 
+    /**
+     * Remove with an empty key.
+     */
+    public function testRmoveEmptyKey()
+    {
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessageRegExp('#^Invalid setting key parameter. String expected, got.*#');
+
+        $this->conf->remove('');
+    }
+
     /**
      * Try to write the config without mandatory parameter (e.g. 'login').
-     *
-     * @expectedException MissingFieldConfigException
      */
     public function testWriteMissingParameter()
     {
-        ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp';
-        $this->assertFalse(file_exists($this->conf->getConfigFile()));
+        $this->expectException(\Shaarli\Config\Exception\MissingFieldConfigException::class);
+
+        $this->conf->setConfigFile('tests/utils/config/configTmp');
+        $this->assertFalse(file_exists($this->conf->getConfigFileExt()));
         $this->conf->reload();
 
         $this->conf->write(true);
@@ -151,10 +174,9 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase
      */
     public function testReset()
     {
-        $conf = $this->conf;
-        $this->assertTrue($conf === ConfigManager::getInstance());
-        $this->assertFalse($conf === $this->conf->reset());
-        $this->assertFalse($conf === ConfigManager::getInstance());
+        $confIO = $this->conf->getConfigIO();
+        $this->conf->reset();
+        $this->assertFalse($confIO === $this->conf->getConfigIO());
     }
 
     /**
@@ -162,11 +184,11 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase
      */
     public function testReload()
     {
-        ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp';
+        $this->conf->setConfigFile('tests/utils/config/configTmp');
         $newConf = ConfigJson::getPhpHeaders() . '{ "key": "value" }';
-        file_put_contents($this->conf->getConfigFile(), $newConf);
+        file_put_contents($this->conf->getConfigFileExt(), $newConf);
         $this->conf->reload();
-        unlink($this->conf->getConfigFile());
+        unlink($this->conf->getConfigFileExt());
         // Previous conf no longer exists, and new values have been loaded.
         $this->assertFalse($this->conf->exists('credentials.login'));
         $this->assertEquals('value', $this->conf->get('key'));