]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Config/PConfigurationTest.php
3d8fb586cedaf80e17ea985b59c6b546473c30d8
[friendica.git] / tests / src / Core / Config / PConfigurationTest.php
1 <?php
2
3 namespace Friendica\Test\Core\Config;
4
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\Config\PConfiguration;
7 use Friendica\Test\MockedTest;
8
9 class PConfigurationTest extends MockedTest
10 {
11         /**
12          * Test if the configuration load() method
13          */
14         public function testCacheLoad()
15         {
16                 $uid = 234;
17                 $configCache = new ConfigCache();
18                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
19                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->once();
20                 // expected loading
21                 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
22
23                 $configuration = new PConfiguration($configCache, $configAdapter);
24                 $configuration->load($uid, 'testing');
25
26                 $this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
27         }
28
29         /**
30          * Test if the configuration load() method with overwrite
31          */
32         public function testCacheLoadDouble()
33         {
34                 $uid = 234;
35                 $configCache = new ConfigCache();
36                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
37                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
38                 // expected loading
39                 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
40                 // expected next loading
41                 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'again']])->once();
42
43                 $configuration = new PConfiguration($configCache, $configAdapter);
44                 $configuration->load($uid, 'testing');
45
46                 $this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
47
48                 $configuration->load($uid, 'testing');
49
50                 $this->assertEquals('again', $configuration->get($uid, 'testing', 'test'));
51         }
52
53         /**
54          * Test if the configuration get() and set() methods without adapter
55          */
56         public function testSetGetWithoutDB()
57         {
58                 $uid = 234;
59                 $configCache = new ConfigCache();
60                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
61                 $configAdapter->shouldReceive('isConnected')->andReturn(false)->once();
62
63                 $configuration = new PConfiguration($configCache, $configAdapter);
64
65                 $this->assertTrue($configuration->set($uid, 'test', 'it', 'now'));
66
67                 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
68         }
69
70         /**
71          * Test if the configuration get() and set() methods with adapter
72          */
73         public function testSetGetWithDB()
74         {
75                 $uid = 234;
76                 $configCache = new ConfigCache();
77                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
78                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->once();
79                 $configAdapter->shouldReceive('set')->with($uid, 'test', 'it', 'now')->andReturn(true)->once();
80
81                 $configuration = new PConfiguration($configCache, $configAdapter);
82
83                 $this->assertTrue($configuration->set($uid, 'test', 'it', 'now'));
84
85                 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
86         }
87
88         /**
89          * Test the configuration get() method with wrong value and no db
90          */
91         public function testGetWrongWithoutDB()
92         {
93                 $uid = 234;
94                 $configCache = new ConfigCache();
95                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
96                 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
97
98                 $configuration = new PConfiguration($configCache, $configAdapter);
99
100                 // without refresh
101                 $this->assertNull($configuration->get($uid, 'test', 'it'));
102
103                 // with default value
104                 $this->assertEquals('default', $configuration->get($uid, 'test', 'it', 'default'));
105
106                 // with default value and refresh
107                 $this->assertEquals('default', $configuration->get($uid, 'test', 'it', 'default', true));
108         }
109
110         /**
111          * Test the configuration get() method with refresh
112          */
113         public function testGetWithRefresh()
114         {
115                 $uid = 234;
116                 $configCache = new ConfigCache();
117                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
118                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
119                 $configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn('now')->once();
120                 $configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn('again')->once();
121                 $configAdapter->shouldReceive('get')->with($uid, 'test', 'not')->andReturn('!<unset>!')->once();
122
123                 $configuration = new PConfiguration($configCache, $configAdapter);
124
125                 // without refresh
126                 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
127                 // use the cache again
128                 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
129
130                 // with refresh (and load the second value out of the db)
131                 $this->assertEquals('again', $configuration->get($uid, 'test', 'it', null, true));
132
133                 // without refresh and wrong value and default
134                 $this->assertEquals('default', $configuration->get($uid, 'test', 'not', 'default'));
135         }
136
137         /**
138          * Test the configuration delete() method without adapter
139          */
140         public function testDeleteWithoutDB()
141         {
142                 $uid = 234;
143                 $configCache = new ConfigCache();
144                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
145                 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
146
147                 $configuration = new PConfiguration($configCache, $configAdapter);
148
149                 $this->assertTrue($configuration->set($uid, 'test', 'it', 'now'));
150                 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
151
152                 $this->assertTrue($configuration->delete($uid, 'test', 'it'));
153                 $this->assertNull($configuration->get($uid, 'test', 'it'));
154         }
155
156         /**
157          * Test the configuration delete() method with adapter
158          */
159         public function testDeleteWithDB()
160         {
161                 $uid = 234;
162                 $configCache = new ConfigCache();
163                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
164                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(5);
165                 $configAdapter->shouldReceive('set')->with($uid, 'test', 'it', 'now')->andReturn(false)->once();
166                 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'it')->andReturn(false)->once();
167
168                 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'second')->andReturn(true)->once();
169                 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'third')->andReturn(false)->once();
170                 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'quarter')->andReturn(true)->once();
171
172                 $configuration = new PConfiguration($configCache, $configAdapter);
173
174                 $this->assertFalse($configuration->set($uid, 'test', 'it', 'now'));
175                 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
176
177                 // delete from set
178                 $this->assertTrue($configuration->delete($uid, 'test', 'it'));
179                 // delete from db only
180                 $this->assertTrue($configuration->delete($uid, 'test', 'second'));
181                 // no delete
182                 $this->assertFalse($configuration->delete($uid, 'test', 'third'));
183                 // delete both
184                 $this->assertTrue($configuration->delete($uid, 'test', 'quarter'));
185         }
186 }