@@ -174,14 +174,180 @@ public function testGloballyDisabledButViewEnabled()
174174 'html ' => true ,
175175 'json ' => true
176176 ];
177-
177+
178178 $ Config = new CacheConfig ( $ Settings );
179-
179+
180180 // Global cache is disabled
181181 $ this ->assertFalse ( $ Config ->isEnabled () );
182-
182+
183183 // But view types can still have their settings
184184 $ this ->assertTrue ( $ Config ->isViewTypeEnabled ( 'html ' ) );
185185 $ this ->assertTrue ( $ Config ->isViewTypeEnabled ( 'json ' ) );
186186 }
187+
188+ /**
189+ * Test Redis configuration defaults
190+ */
191+ public function testRedisConfigDefaults ()
192+ {
193+ $ Config = new CacheConfig ( [] );
194+
195+ $ this ->assertEquals ( '127.0.0.1 ' , $ Config ->getRedisHost () );
196+ $ this ->assertEquals ( 6379 , $ Config ->getRedisPort () );
197+ $ this ->assertEquals ( 0 , $ Config ->getRedisDatabase () );
198+ $ this ->assertEquals ( 'neuron_cache_ ' , $ Config ->getRedisPrefix () );
199+ $ this ->assertEquals ( 2.0 , $ Config ->getRedisTimeout () );
200+ $ this ->assertNull ( $ Config ->getRedisAuth () );
201+ $ this ->assertFalse ( $ Config ->getRedisPersistent () );
202+ }
203+
204+ /**
205+ * Test Redis configuration with custom values
206+ */
207+ public function testRedisConfigCustomValues ()
208+ {
209+ $ Settings = [
210+ 'redis_host ' => '192.168.1.100 ' ,
211+ 'redis_port ' => 6380 ,
212+ 'redis_database ' => 2 ,
213+ 'redis_prefix ' => 'myapp_ ' ,
214+ 'redis_timeout ' => 5.0 ,
215+ 'redis_auth ' => 'mypassword ' ,
216+ 'redis_persistent ' => true
217+ ];
218+
219+ $ Config = new CacheConfig ( $ Settings );
220+
221+ $ this ->assertEquals ( '192.168.1.100 ' , $ Config ->getRedisHost () );
222+ $ this ->assertEquals ( 6380 , $ Config ->getRedisPort () );
223+ $ this ->assertEquals ( 2 , $ Config ->getRedisDatabase () );
224+ $ this ->assertEquals ( 'myapp_ ' , $ Config ->getRedisPrefix () );
225+ $ this ->assertEquals ( 5.0 , $ Config ->getRedisTimeout () );
226+ $ this ->assertEquals ( 'mypassword ' , $ Config ->getRedisAuth () );
227+ $ this ->assertTrue ( $ Config ->getRedisPersistent () );
228+ }
229+
230+ /**
231+ * Test Redis persistent connection variations
232+ */
233+ public function testRedisPersistentVariations ()
234+ {
235+ // Test with true
236+ $ Config1 = new CacheConfig ( ['redis_persistent ' => true ] );
237+ $ this ->assertTrue ( $ Config1 ->getRedisPersistent () );
238+
239+ // Test with 'true' string
240+ $ Config2 = new CacheConfig ( ['redis_persistent ' => 'true ' ] );
241+ $ this ->assertTrue ( $ Config2 ->getRedisPersistent () );
242+
243+ // Test with '1' string
244+ $ Config3 = new CacheConfig ( ['redis_persistent ' => '1 ' ] );
245+ $ this ->assertTrue ( $ Config3 ->getRedisPersistent () );
246+
247+ // Test with false
248+ $ Config4 = new CacheConfig ( ['redis_persistent ' => false ] );
249+ $ this ->assertFalse ( $ Config4 ->getRedisPersistent () );
250+
251+ // Test with '0' string
252+ $ Config5 = new CacheConfig ( ['redis_persistent ' => '0 ' ] );
253+ $ this ->assertFalse ( $ Config5 ->getRedisPersistent () );
254+ }
255+
256+ /**
257+ * Test getRedisConfig returns complete configuration
258+ */
259+ public function testGetRedisConfig ()
260+ {
261+ $ Settings = [
262+ 'redis_host ' => 'redis.example.com ' ,
263+ 'redis_port ' => 6379 ,
264+ 'redis_database ' => 1 ,
265+ 'redis_prefix ' => 'test_ ' ,
266+ 'redis_timeout ' => 3.0 ,
267+ 'redis_auth ' => 'secret ' ,
268+ 'redis_persistent ' => true
269+ ];
270+
271+ $ Config = new CacheConfig ( $ Settings );
272+ $ redisConfig = $ Config ->getRedisConfig ();
273+
274+ $ this ->assertIsArray ( $ redisConfig );
275+ $ this ->assertEquals ( 'redis.example.com ' , $ redisConfig ['host ' ] );
276+ $ this ->assertEquals ( 6379 , $ redisConfig ['port ' ] );
277+ $ this ->assertEquals ( 1 , $ redisConfig ['database ' ] );
278+ $ this ->assertEquals ( 'test_ ' , $ redisConfig ['prefix ' ] );
279+ $ this ->assertEquals ( 3.0 , $ redisConfig ['timeout ' ] );
280+ $ this ->assertEquals ( 'secret ' , $ redisConfig ['auth ' ] );
281+ $ this ->assertTrue ( $ redisConfig ['persistent ' ] );
282+ }
283+
284+ /**
285+ * Test fromSettings with Redis configuration
286+ */
287+ public function testFromSettingsWithRedisConfig ()
288+ {
289+ $ Settings = new Memory ();
290+ $ Settings ->set ( 'cache ' , 'enabled ' , 'true ' );
291+ $ Settings ->set ( 'cache ' , 'storage ' , 'redis ' );
292+ $ Settings ->set ( 'cache ' , 'redis_host ' , 'redis-server ' );
293+ $ Settings ->set ( 'cache ' , 'redis_port ' , '6380 ' );
294+ $ Settings ->set ( 'cache ' , 'redis_database ' , '3 ' );
295+ $ Settings ->set ( 'cache ' , 'redis_prefix ' , 'cache_ ' );
296+ $ Settings ->set ( 'cache ' , 'redis_timeout ' , '4.5 ' );
297+ $ Settings ->set ( 'cache ' , 'redis_auth ' , 'password123 ' );
298+ $ Settings ->set ( 'cache ' , 'redis_persistent ' , 'true ' );
299+
300+ $ Config = CacheConfig::fromSettings ( $ Settings );
301+
302+ $ this ->assertEquals ( 'redis ' , $ Config ->getStorageType () );
303+ $ this ->assertEquals ( 'redis-server ' , $ Config ->getRedisHost () );
304+ $ this ->assertEquals ( 6380 , $ Config ->getRedisPort () );
305+ $ this ->assertEquals ( 3 , $ Config ->getRedisDatabase () );
306+ $ this ->assertEquals ( 'cache_ ' , $ Config ->getRedisPrefix () );
307+ $ this ->assertEquals ( 4.5 , $ Config ->getRedisTimeout () );
308+ $ this ->assertEquals ( 'password123 ' , $ Config ->getRedisAuth () );
309+ $ this ->assertTrue ( $ Config ->getRedisPersistent () );
310+ }
311+
312+ /**
313+ * Test type casting for numeric Redis settings
314+ */
315+ public function testRedisNumericTypeCasting ()
316+ {
317+ $ Settings = [
318+ 'redis_port ' => '6380 ' , // String should be cast to int
319+ 'redis_database ' => '5 ' , // String should be cast to int
320+ 'redis_timeout ' => '3.5 ' // String should be cast to float
321+ ];
322+
323+ $ Config = new CacheConfig ( $ Settings );
324+
325+ $ this ->assertIsInt ( $ Config ->getRedisPort () );
326+ $ this ->assertEquals ( 6380 , $ Config ->getRedisPort () );
327+
328+ $ this ->assertIsInt ( $ Config ->getRedisDatabase () );
329+ $ this ->assertEquals ( 5 , $ Config ->getRedisDatabase () );
330+
331+ $ this ->assertIsFloat ( $ Config ->getRedisTimeout () );
332+ $ this ->assertEquals ( 3.5 , $ Config ->getRedisTimeout () );
333+ }
334+
335+ /**
336+ * Test fromSettings with minimal Redis configuration
337+ */
338+ public function testFromSettingsMinimalRedisConfig ()
339+ {
340+ $ Settings = new Memory ();
341+ $ Settings ->set ( 'cache ' , 'enabled ' , 'true ' );
342+ $ Settings ->set ( 'cache ' , 'storage ' , 'redis ' );
343+ // Don't set any Redis-specific parameters
344+
345+ $ Config = CacheConfig::fromSettings ( $ Settings );
346+
347+ // Should use defaults
348+ $ this ->assertEquals ( '127.0.0.1 ' , $ Config ->getRedisHost () );
349+ $ this ->assertEquals ( 6379 , $ Config ->getRedisPort () );
350+ $ this ->assertEquals ( 0 , $ Config ->getRedisDatabase () );
351+ $ this ->assertEquals ( 'neuron_cache_ ' , $ Config ->getRedisPrefix () );
352+ }
187353}
0 commit comments