]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Core/Config/ConfigurationTest.php
Merge pull request #7071 from nupplaphil/task/mod_bookmarklet
[friendica.git] / tests / src / Core / Config / ConfigurationTest.php
index 6ce81a41b0b49e43b264abf8923d2f954add7849..b07f9e6302bd0fa1f5ff8c19a5fb3f86a0de8bcd 100644 (file)
@@ -1,7 +1,8 @@
 <?php
 
-namespace Friendica\Test\Core\Config;
+namespace Friendica\Test\src\Core\Config;
 
+use Friendica\Core\Config\Adapter\IConfigAdapter;
 use Friendica\Core\Config\Cache\ConfigCache;
 use Friendica\Core\Config\Cache\IConfigCache;
 use Friendica\Core\Config\Configuration;
@@ -9,13 +10,27 @@ use Friendica\Test\MockedTest;
 
 class ConfigurationTest extends MockedTest
 {
+       public function dataTests()
+       {
+               return [
+                       'string'       => ['data' => 'it'],
+                       'boolTrue'     => ['data' => true],
+                       'boolFalse'    => ['data' => false],
+                       'integer'      => ['data' => 235],
+                       'decimal'      => ['data' => 2.456],
+                       'array'        => ['data' => ['1', 2, '3', true, false]],
+                       'boolIntTrue'  => ['data' => 1],
+                       'boolIntFalse' => ['Data' => 0],
+               ];
+       }
+
        /**
         * Test the configuration initialization
         */
        public function testSetUp()
        {
                $configCache = new ConfigCache();
-               $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
+               $configAdapter = \Mockery::mock(IConfigAdapter::class);
                $configAdapter->shouldReceive('isConnected')->andReturn(false)->once();
 
                $configuration = new Configuration($configCache, $configAdapter);
@@ -24,17 +39,18 @@ class ConfigurationTest extends MockedTest
        }
 
        /**
-        * Test if the configuration load() method
+        * Test the configuration load() method
         */
        public function testCacheLoad()
        {
                $configCache = new ConfigCache();
-               $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
-               $configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
+               $configAdapter = \Mockery::mock(IConfigAdapter::class);
+               $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
                // constructor loading
                $configAdapter->shouldReceive('load')->andReturn([])->once();
                // expected loading
                $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
+               $configAdapter->shouldReceive('isLoaded')->with('testing', 'test')->andReturn(true)->once();
 
                $configuration = new Configuration($configCache, $configAdapter);
                $configuration->load('testing');
@@ -44,17 +60,18 @@ class ConfigurationTest extends MockedTest
        }
 
        /**
-        * Test if the configuration load() method with overwrite
+        * Test the configuration load() method with overwrite
         */
        public function testCacheLoadDouble()
        {
                $configCache = new ConfigCache();
-               $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
-               $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
+               $configAdapter = \Mockery::mock(IConfigAdapter::class);
+               $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(5);
                // constructor loading
                $configAdapter->shouldReceive('load')->andReturn([])->once();
                // expected loading
                $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
+               $configAdapter->shouldReceive('isLoaded')->with('testing', 'test')->andReturn(true)->twice();
                // expected next loading
                $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'again']])->once();
 
@@ -71,40 +88,43 @@ class ConfigurationTest extends MockedTest
        }
 
        /**
-        * Test if the configuration get() and set() methods without adapter
+        * Test the configuration get() and set() methods without adapter
+        * @dataProvider dataTests
         */
-       public function testSetGetWithoutDB()
+       public function testSetGetWithoutDB($data)
        {
                $configCache = new ConfigCache();
-               $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
-               $configAdapter->shouldReceive('isConnected')->andReturn(false)->twice();
+               $configAdapter = \Mockery::mock(IConfigAdapter::class);
+               $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
 
                $configuration = new Configuration($configCache, $configAdapter);
 
-               $this->assertTrue($configuration->set('test', 'it', 'now'));
+               $this->assertTrue($configuration->set('test', 'it', $data));
 
-               $this->assertEquals('now', $configuration->get('test', 'it'));
-               $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
+               $this->assertEquals($data, $configuration->get('test', 'it'));
+               $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
        }
 
        /**
-        * Test if the configuration get() and set() methods with adapter
+        * Test the configuration get() and set() methods with adapter
+        * @dataProvider dataTests
         */
-       public function testSetGetWithDB()
+       public function testSetGetWithDB($data)
        {
                $configCache = new ConfigCache();
-               $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
-               $configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
+               $configAdapter = \Mockery::mock(IConfigAdapter::class);
+               $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
                // constructor loading
                $configAdapter->shouldReceive('load')->andReturn([])->once();
-               $configAdapter->shouldReceive('set')->with('test', 'it', 'now')->andReturn(true)->once();
+               $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
+               $configAdapter->shouldReceive('set')->with('test', 'it', $data)->andReturn(true)->once();
 
                $configuration = new Configuration($configCache, $configAdapter);
 
-               $this->assertTrue($configuration->set('test', 'it', 'now'));
+               $this->assertTrue($configuration->set('test', 'it', $data));
 
-               $this->assertEquals('now', $configuration->get('test', 'it'));
-               $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
+               $this->assertEquals($data, $configuration->get('test', 'it'));
+               $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
        }
 
        /**
@@ -113,7 +133,7 @@ class ConfigurationTest extends MockedTest
        public function testGetWrongWithoutDB()
        {
                $configCache = new ConfigCache();
-               $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
+               $configAdapter = \Mockery::mock(IConfigAdapter::class);
                $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
 
                $configuration = new Configuration($configCache, $configAdapter);
@@ -122,7 +142,7 @@ class ConfigurationTest extends MockedTest
                $this->assertNull($configuration->get('test', 'it'));
 
                /// beware that the cache returns '!<unset>!' and not null for a non existing value
-               $this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'it'));
+               $this->assertNull($configuration->getCache()->get('test', 'it'));
 
                // with default value
                $this->assertEquals('default', $configuration->get('test', 'it', 'default'));
@@ -133,16 +153,19 @@ class ConfigurationTest extends MockedTest
 
        /**
         * Test the configuration get() method with refresh
+        * @dataProvider dataTests
         */
-       public function testGetWithRefresh()
+       public function testGetWithRefresh($data)
        {
                $configCache = new ConfigCache(['test' => ['it' => 'now']]);
-               $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
-               $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
+               $configAdapter = \Mockery::mock(IConfigAdapter::class);
+               $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
                // constructor loading
                $configAdapter->shouldReceive('load')->andReturn([])->once();
-               $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn('again')->once();
-               $configAdapter->shouldReceive('get')->with('test', 'not')->andReturn('!<unset>!')->once();
+               $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->twice();
+               $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn($data)->once();
+               $configAdapter->shouldReceive('isLoaded')->with('test', 'not')->andReturn(false)->once();
+               $configAdapter->shouldReceive('get')->with('test', 'not')->andReturn(null)->once();
 
                $configuration = new Configuration($configCache, $configAdapter);
 
@@ -151,31 +174,67 @@ class ConfigurationTest extends MockedTest
                $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
 
                // with refresh
-               $this->assertEquals('again', $configuration->get('test', 'it', null, true));
-               $this->assertEquals('again', $configuration->getCache()->get('test', 'it'));
+               $this->assertEquals($data, $configuration->get('test', 'it', null, true));
+               $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
 
                // without refresh and wrong value and default
                $this->assertEquals('default', $configuration->get('test', 'not', 'default'));
-               $this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'not'));
+               $this->assertNull($configuration->getCache()->get('test', 'not'));
        }
 
        /**
-        * Test the configuration delete() method without adapter
+        * Test the configuration get() method with different isLoaded settings
+        * @dataProvider dataTests
         */
-       public function testDeleteWithoutDB()
+       public function testGetWithoutLoaded($data)
        {
                $configCache = new ConfigCache(['test' => ['it' => 'now']]);
-               $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
-               $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
+               $configAdapter = \Mockery::mock(IConfigAdapter::class);
+               $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
+               // constructor loading
+               $configAdapter->shouldReceive('load')->andReturn([])->once();
+
+               $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(false)->once();
+               $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn(null)->once();
+
+               $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(false)->once();
+               $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn($data)->once();
+
+               $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
 
                $configuration = new Configuration($configCache, $configAdapter);
 
+               // first run is not loaded and no data is found in the DB
                $this->assertEquals('now', $configuration->get('test', 'it'));
                $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
 
+               // second run is not loaded, but now data is found in the db (overwrote cache)
+               $this->assertEquals($data, $configuration->get('test', 'it'));
+               $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
+
+               // third run is loaded and therefore cache is used
+               $this->assertEquals($data, $configuration->get('test', 'it'));
+               $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
+       }
+
+       /**
+        * Test the configuration delete() method without adapter
+        * @dataProvider dataTests
+        */
+       public function testDeleteWithoutDB($data)
+       {
+               $configCache = new ConfigCache(['test' => ['it' => $data]]);
+               $configAdapter = \Mockery::mock(IConfigAdapter::class);
+               $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
+
+               $configuration = new Configuration($configCache, $configAdapter);
+
+               $this->assertEquals($data, $configuration->get('test', 'it'));
+               $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
+
                $this->assertTrue($configuration->delete('test', 'it'));
                $this->assertNull($configuration->get('test', 'it'));
-               $this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'it'));
+               $this->assertNull($configuration->getCache()->get('test', 'it'));
 
                $this->assertEmpty($configuration->getCache()->getAll());
        }
@@ -186,10 +245,12 @@ class ConfigurationTest extends MockedTest
        public function testDeleteWithDB()
        {
                $configCache = new ConfigCache(['test' => ['it' => 'now', 'quarter' => 'true']]);
-               $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
-               $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(5);
+               $configAdapter = \Mockery::mock(IConfigAdapter::class);
+               $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(6);
                // constructor loading
                $configAdapter->shouldReceive('load')->andReturn([])->once();
+               $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
+
                $configAdapter->shouldReceive('delete')->with('test', 'it')->andReturn(false)->once();
 
                $configAdapter->shouldReceive('delete')->with('test', 'second')->andReturn(true)->once();