55use SolutionForest \InspireCms \Tests \TestCase ;
66
77uses (TestCase::class);
8+ pest ()->group ('unit ' , 'facade ' );
89
910function createSampleData ($ slug = null , $ value = null )
1011{
@@ -14,83 +15,79 @@ function createSampleData($slug = null, $value = null)
1415 ]);
1516}
1617
17- describe ('key vaue cache facade ' , function () {
18+ test ('cacheAll stores all key-value pairs in cache ' , function () {
19+ // Create multiple test records
20+ $ record1 = createSampleData ('test_key1 ' , 'test_value1 ' );
21+ $ record2 = createSampleData ('test_key2 ' , 'test_value2 ' );
1822
19- test ('cacheAll stores all key-value pairs in cache ' , function () {
20- // Create multiple test records
21- $ record1 = createSampleData ('test_key1 ' , 'test_value1 ' );
22- $ record2 = createSampleData ('test_key2 ' , 'test_value2 ' );
23+ // Call the cacheAll method which should cache all records
24+ KeyValueCache::cacheAll ();
2325
24- // Call the cacheAll method which should cache all records
25- KeyValueCache::cacheAll ();
26+ // Verify that getting these values returns the correct result from cache
27+ expect (KeyValueCache::get ('test_key1 ' ))->toBe ('test_value1 ' );
28+ expect (KeyValueCache::get ('test_key2 ' ))->toBe ('test_value2 ' );
29+ });
2630
27- // Verify that getting these values returns the correct result from cache
28- expect (KeyValueCache::get ('test_key1 ' ))->toBe ('test_value1 ' );
29- expect (KeyValueCache::get ('test_key2 ' ))->toBe ('test_value2 ' );
30- });
31+ test ('set stores value in cache and can be retrieved ' , function () {
32+ $ key = 'new_test_key ' ;
33+ $ value = 'new_test_value ' ;
3134
32- test ('set stores value in cache and can be retrieved ' , function () {
33- $ key = 'new_test_key ' ;
34- $ value = 'new_test_value ' ;
35+ // Set the value in cache
36+ KeyValueCache::set ($ key , $ value );
3537
36- // Set the value in cache
37- KeyValueCache::set ($ key , $ value );
38+ // Verify it can be retrieved
39+ expect (KeyValueCache::get ($ key ))->toBe ($ value );
40+ });
3841
39- // Verify it can be retrieved
40- expect (KeyValueCache:: get ( $ key))-> toBe ( $ value ) ;
41- }) ;
42+ test ( ' forget removes a value from cache ' , function () {
43+ $ key = ' forget_test_key ' ;
44+ $ value = ' forget_test_value ' ;
4245
43- test ( ' forget removes a value from cache' , function () {
44- $ key = ' forget_test_key ' ;
45- $ value = ' forget_test_value ' ;
46+ // Create and cache a test record
47+ $ record = createSampleData ( $ key, $ value ) ;
48+ KeyValueCache:: set ( $ record -> key , $ record -> value ) ;
4649
47- // Create and cache a test record
48- $ record = createSampleData ($ key , $ value );
49- KeyValueCache::set ($ record ->key , $ record ->value );
50+ // Verify it's in cache
51+ expect (KeyValueCache::get ($ key ))->toBe ($ value );
5052
51- // Verify it's in cache
52- expect ( KeyValueCache::get ($ key))-> toBe ( $ value );
53+ // Forget the value
54+ KeyValueCache::forget ($ key );
5355
54- // Forget the value
55- KeyValueCache::forget ($ key );
56+ // The value should still be in the database but no longer in cache
57+ // Since the database is the fallback, we need to mock or spy on the cache
58+ // implementation to properly test this, but we can verify it still exists
59+ expect (KeyValue::where ('key ' , $ key )->exists ())->toBeTrue ();
60+ });
5661
57- // The value should still be in the database but no longer in cache
58- // Since the database is the fallback, we need to mock or spy on the cache
59- // implementation to properly test this, but we can verify it still exists
60- expect (KeyValue::where ('key ' , $ key )->exists ())->toBeTrue ();
61- });
62-
63- test ('clear removes all values from cache ' , function () {
64- // Create multiple test records and cache them
65- $ keys = ['clear_key1 ' , 'clear_key2 ' , 'clear_key3 ' ];
66- $ values = ['clear_value1 ' , 'clear_value2 ' , 'clear_value3 ' ];
62+ test ('clear removes all values from cache ' , function () {
63+ // Create multiple test records and cache them
64+ $ keys = ['clear_key1 ' , 'clear_key2 ' , 'clear_key3 ' ];
65+ $ values = ['clear_value1 ' , 'clear_value2 ' , 'clear_value3 ' ];
6766
68- foreach ($ keys as $ index => $ key ) {
69- createSampleData ($ key , $ values [$ index ]);
70- KeyValueCache::set ($ key , $ values [$ index ]);
71- }
67+ foreach ($ keys as $ index => $ key ) {
68+ createSampleData ($ key , $ values [$ index ]);
69+ KeyValueCache::set ($ key , $ values [$ index ]);
70+ }
7271
73- // Verify they're all in cache
74- foreach ($ keys as $ index => $ key ) {
75- expect (KeyValueCache::get ($ key ))->toBe ($ values [$ index ]);
76- }
72+ // Verify they're all in cache
73+ foreach ($ keys as $ index => $ key ) {
74+ expect (KeyValueCache::get ($ key ))->toBe ($ values [$ index ]);
75+ }
7776
78- // Clear the cache
79- KeyValueCache::clear ();
77+ // Clear the cache
78+ KeyValueCache::clear ();
8079
81- // All records should still exist in the database
82- foreach ($ keys as $ key ) {
83- expect (KeyValue::where ('key ' , $ key )->exists ())->toBeTrue ();
84- }
85- });
86-
87- test ('get returns default value when key does not exist ' , function () {
88- $ nonExistentKey = 'non_existent_key_ ' . time ();
89- $ defaultValue = 'default_test_value ' ;
80+ // All records should still exist in the database
81+ foreach ($ keys as $ key ) {
82+ expect (KeyValue::where ('key ' , $ key )->exists ())->toBeTrue ();
83+ }
84+ });
9085
91- $ result = KeyValueCache::get ($ nonExistentKey , $ defaultValue );
86+ test ('get returns default value when key does not exist ' , function () {
87+ $ nonExistentKey = 'non_existent_key_ ' . time ();
88+ $ defaultValue = 'default_test_value ' ;
9289
93- expect ($ result )->toBe ($ defaultValue );
94- });
90+ $ result = KeyValueCache::get ($ nonExistentKey , $ defaultValue );
9591
96- })->group ('unit ' , 'cache ' , 'key-value ' );
92+ expect ($ result )->toBe ($ defaultValue );
93+ });
0 commit comments