File tree 3 files changed +41
-0
lines changed
3 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -93,6 +93,10 @@ public function onWriteItemPre(Event $event)
93
93
$ serializer = $ this ->getOptions ()->getSerializer ();
94
94
$ params = $ event ->getParams ();
95
95
$ params ['value ' ] = $ serializer ->serialize ($ params ['value ' ]);
96
+ /** Passed by {@see AbstractAdapter::checkAndSetItem()}. Used to compare with the already cached value. */
97
+ if (isset ($ params ['token ' ])) {
98
+ $ params ['token ' ] = $ serializer ->serialize ($ params ['token ' ]);
99
+ }
96
100
}
97
101
98
102
/**
Original file line number Diff line number Diff line change 14
14
use Laminas \Cache \Storage \Capabilities ;
15
15
use Laminas \Cache \Storage \Event ;
16
16
use Laminas \Cache \Storage \Plugin \PluginOptions ;
17
+ use Laminas \Cache \Storage \Plugin \Serializer ;
17
18
use Laminas \Cache \Storage \PostEvent ;
18
19
use Laminas \EventManager \ResponseCollection ;
19
20
use LaminasTest \Cache \Storage \TestAsset \MockPlugin ;
30
31
use function call_user_func_array ;
31
32
use function count ;
32
33
use function current ;
34
+ use function serialize ;
33
35
use function ucfirst ;
34
36
35
37
final class AbstractAdapterTest extends TestCase
@@ -962,4 +964,24 @@ protected function getMockForAbstractAdapter(array $methods = []): MockObject&Ab
962
964
963
965
return $ adapter ;
964
966
}
967
+
968
+ public function testCanCompareOldValueWithTokenWhenUsedWithSerializerPlugin (): void
969
+ {
970
+ $ storage = $ this ->getMockForAbstractAdapter ();
971
+ $ storage
972
+ ->addPlugin (new Serializer ());
973
+ $ storage
974
+ ->expects (self ::once ())
975
+ ->method ('internalSetItem ' )
976
+ ->with ('foo ' , serialize ('baz ' ))
977
+ ->willReturn (true );
978
+
979
+ $ storage
980
+ ->expects (self ::once ())
981
+ ->method ('internalGetItem ' )
982
+ ->with ('foo ' )
983
+ ->willReturn (serialize ('bar ' ));
984
+
985
+ self ::assertTrue ($ storage ->checkAndSetItem ('bar ' , 'foo ' , 'baz ' ));
986
+ }
965
987
}
Original file line number Diff line number Diff line change @@ -220,4 +220,19 @@ public function testOnIncrementItemWillAssumeZeroForNonExistingCacheItem(): void
220
220
221
221
self ::assertEquals (10 , $ plugin ->onIncrementItemPre ($ event ));
222
222
}
223
+
224
+ public function testonWriteItemPreSerializesTokenOncePassed (): void
225
+ {
226
+ $ plugin = new Serializer ();
227
+ $ parameters = new ArrayObject ([
228
+ 'key ' => 'foo ' ,
229
+ 'value ' => 10 ,
230
+ 'token ' => 10 ,
231
+ ]);
232
+ $ event = new Event ('foo ' , $ this ->createMock (StorageInterface::class), $ parameters );
233
+
234
+ $ plugin ->onWriteItemPre ($ event );
235
+
236
+ self ::assertSame ($ parameters ['value ' ], $ parameters ['token ' ]);
237
+ }
223
238
}
You can’t perform that action at this time.
0 commit comments