@@ -81,4 +81,136 @@ public function testTimeToLiveExceeded()
8181 sleep (2 );
8282 $ this ->assertEquals (null , $ cache ->get ($ key ));
8383 }
84+
85+ public function testCacheWithIPv6DifferentNotations ()
86+ {
87+ // Create cache instance
88+ $ cache = new DefaultCache (10 , 600 );
89+
90+ // Original IPv6 address
91+ $ standard_ip = "2607:f8b0:4005:805::200e " ;
92+ $ standard_value = "standard_value " ;
93+ $ cache ->set ($ standard_ip , $ standard_value );
94+
95+ // Variations with zeros
96+ $ variations = [
97+ "2607:f8b0:4005:805:0:0:0:200e " , // Full form
98+ "2607:f8b0:4005:805:0000:0000:0000:200e " , // Full form with leading zeros
99+ "2607:f8b0:4005:805:0000:00:00:200e " , // Full form with few leading zeros
100+ "2607:F8B0:4005:805::200E " , // Uppercase notation
101+ "2607:f8b0:4005:0805::200e " , // Leading zero in a group
102+ "2607:f8b0:4005:805:0::200e " , // Partially expanded
103+ "2607:f8b0:4005:805:0000::200e " , // Full zeros in a second group
104+ ];
105+
106+ // DefaultCache does normalize IPs, so we need to check if the cache has the same value
107+ foreach ($ variations as $ ip ) {
108+ $ this ->assertTrue ($ cache ->has ($ ip ), "Cache should have variation: $ ip " );
109+ }
110+ }
111+
112+ public function testDefaultCacheWithIPv4AndPostfixes ()
113+ {
114+ // Create cache
115+ $ cache = new DefaultCache (5 , 600 );
116+
117+ // Test IPv4 with various postfixes
118+ $ ipv4 = '8.8.8.8 ' ;
119+ $ postfixes = ['_v1 ' , '_latest ' , '_beta ' , '_test_123 ' ];
120+
121+ foreach ($ postfixes as $ postfix ) {
122+ $ key = $ ipv4 . $ postfix ;
123+ $ value = "value_for_ $ key " ;
124+
125+ // Set value with postfix
126+ $ cache ->set ($ key , $ value );
127+
128+ // Verify it's in cache
129+ $ this ->assertTrue ($ cache ->has ($ key ), "Cache should have key with postfix: $ key " );
130+ $ this ->assertEquals ($ value , $ cache ->get ($ key ), "Should get correct value for key with postfix " );
131+
132+ // Verify base IP is not affected
133+ if (!$ cache ->has ($ ipv4 )) {
134+ $ cache ->set ($ ipv4 , "base_ip_value " );
135+ }
136+
137+ $ this ->assertNotEquals ($ cache ->get ($ ipv4 ), $ cache ->get ($ key ), "Base IP and IP with postfix should have different values " );
138+ }
139+
140+ // Check all keys are still available (capacity not exceeded)
141+ foreach ($ postfixes as $ postfix ) {
142+ $ this ->assertTrue ($ cache ->has ($ ipv4 . $ postfix ), "All postfix keys should be available " );
143+ }
144+ $ this ->assertTrue ($ cache ->has ($ ipv4 ), "Base IP should be available " );
145+ }
146+
147+ public function testCacheWithIPv6AndPostfixes ()
148+ {
149+ // Create cache
150+ $ cache = new DefaultCache (5 , 600 );
151+
152+ // Test IPv6 with various postfixes
153+ $ ipv6 = '2607:f8b0:4005:805::200e ' ;
154+ $ postfixes = ['_v1 ' , '_latest ' , '_beta ' , '_test_123 ' ];
155+
156+ foreach ($ postfixes as $ postfix ) {
157+ $ key = $ ipv6 . $ postfix ;
158+ $ value = "value_for_ $ key " ;
159+
160+ // Set value with postfix
161+ $ cache ->set ($ key , $ value );
162+
163+ // Verify it's in cache
164+ $ this ->assertTrue ($ cache ->has ($ key ), "Cache should have key with postfix: $ key " );
165+ $ this ->assertEquals ($ value , $ cache ->get ($ key ), "Should get correct value for key with postfix " );
166+ }
167+
168+ // Add the base IP to cache if not present
169+ if (!$ cache ->has ($ ipv6 )) {
170+ $ cache ->set ($ ipv6 , "base_ip_value " );
171+ }
172+
173+ // Ensure all keys are distinct and have different values
174+ $ this ->assertEquals ("base_ip_value " , $ cache ->get ($ ipv6 ), "Base IP should have its own value " );
175+ foreach ($ postfixes as $ postfix ) {
176+ $ key = $ ipv6 . $ postfix ;
177+ $ expected = "value_for_ $ key " ;
178+ $ this ->assertEquals ($ expected , $ cache ->get ($ key ), "Each postfix should have its own value " );
179+ }
180+ }
181+
182+ public function testCacheWithIPv6NotationsAndPostfixes ()
183+ {
184+ // Create cache instance
185+ $ cache = new DefaultCache (100 , 600 );
186+
187+ // Original IPv6 address
188+ $ standard_ip = "2607:f8b0:4005:805::200e " ;
189+ $ postfixes = ['_v1 ' , '_latest ' , '_beta ' , '_test_123 ' ];
190+
191+ // Variations with zeros
192+ $ variations = [
193+ "2607:f8b0:4005:805:0:0:0:200e " , // Full form
194+ "2607:f8b0:4005:805:0000:0000:0000:200e " , // Full form with leading zeros
195+ "2607:f8b0:4005:805:0000:00:00:200e " , // Full form with few leading zeros
196+ "2607:F8B0:4005:805::200E " , // Uppercase notation
197+ "2607:f8b0:4005:0805::200e " , // Leading zero in a group
198+ "2607:f8b0:4005:805:0::200e " , // Partially expanded
199+ "2607:f8b0:4005:805:0000::200e " , // Full zeros in a second group
200+ ];
201+
202+ foreach ($ postfixes as $ postfix ) {
203+ // Set cache with first postfix
204+ $ value = "value_for_ $ standard_ip " ;
205+ $ cache ->set ($ standard_ip . $ postfix , $ value );
206+ foreach ($ variations as $ variation_id => $ ip ) {
207+ $ key = $ ip . $ postfix ;
208+ $ this ->assertTrue ($ cache ->has ($ key ), "Cache should have variation # $ variation_id with postfix: $ key " );
209+ $ this ->assertEquals ($ value , $ cache ->get ($ key ), "Should get correct value for key with postfix " );
210+ }
211+ }
212+
213+ // Check that the base IP not cached
214+ $ this ->assertFalse ($ cache ->has ($ standard_ip ), "Base IP should not be cached " );
215+ }
84216}
0 commit comments