From 2546d1035e844da378b03e1fb42d3d1cf53187e2 Mon Sep 17 00:00:00 2001 From: e-zannelli <41945822+e-zannelli@users.noreply.github.com> Date: Wed, 14 Jun 2023 13:19:21 +0200 Subject: [PATCH] fix: restore cache entry unserialization compatibility (#181) --- src/CacheEntry.php | 19 ++++++++++++------- tests/CacheEntryTest.php | 23 +++++++++++++++++++++++ tests/data/v4.0.0_serialized_cache_entry | Bin 0 -> 2761 bytes tests/data/v4.1.0_serialized_cache_entry | Bin 0 -> 2761 bytes tests/data/v4.1.1_serialized_cache_entry | Bin 0 -> 2737 bytes 5 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 tests/data/v4.0.0_serialized_cache_entry create mode 100644 tests/data/v4.1.0_serialized_cache_entry create mode 100644 tests/data/v4.1.1_serialized_cache_entry diff --git a/src/CacheEntry.php b/src/CacheEntry.php index ebeeaf3..4d2a965 100644 --- a/src/CacheEntry.php +++ b/src/CacheEntry.php @@ -272,13 +272,18 @@ public function __serialize(): array public function __unserialize(array $data): void { - $this->request = self::restoreStreamBody($data['request']); - $this->response = $data['response'] !== null ? self::restoreStreamBody($data['response']) : null; - $this->staleAt = $data['staleAt']; - $this->staleIfErrorTo = $data['staleIfErrorTo']; - $this->staleWhileRevalidateTo = $data['staleWhileRevalidateTo']; - $this->dateCreated = $data['dateCreated']; - $this->timestampStale = $data['timestampStale']; + $prefix = ''; + if (isset($data["\0*\0request"])) { + // We are unserializing a cache entry which was serialized with a version < 4.1.1 + $prefix = "\0*\0"; + } + $this->request = self::restoreStreamBody($data[$prefix.'request']); + $this->response = $data[$prefix.'response'] !== null ? self::restoreStreamBody($data[$prefix.'response']) : null; + $this->staleAt = $data[$prefix.'staleAt']; + $this->staleIfErrorTo = $data[$prefix.'staleIfErrorTo']; + $this->staleWhileRevalidateTo = $data[$prefix.'staleWhileRevalidateTo']; + $this->dateCreated = $data[$prefix.'dateCreated']; + $this->timestampStale = $data[$prefix.'timestampStale']; } /** diff --git a/tests/CacheEntryTest.php b/tests/CacheEntryTest.php index 833ad78..5d3a504 100644 --- a/tests/CacheEntryTest.php +++ b/tests/CacheEntryTest.php @@ -134,6 +134,29 @@ public function testSerializationShouldNotMutateCacheEntry() $this->assertEquals($cacheEntry, $originalCacheEntry); } + /** + * @dataProvider versionsToTestProvider + */ + public function testPreviousUnserialize($version) + { + if (version_compare(PHP_VERSION, '7.4.0') < 0) { + $this->markTestSkipped('Compat with previous version is not available with \Serializable interface'); + } + + $cacheEntry = unserialize(file_get_contents(__DIR__."/data/{$version}_serialized_cache_entry")); + + self::assertInstanceOf(CacheEntry::class, $cacheEntry); + } + + public function versionsToTestProvider() + { + return [ + ['v4.0.0'], + ['v4.1.0'], + ['v4.1.1'], + ]; + } + private function setResponseHeader($name, $value) { $this->responseHeaders[$name] = [$value]; diff --git a/tests/data/v4.0.0_serialized_cache_entry b/tests/data/v4.0.0_serialized_cache_entry new file mode 100644 index 0000000000000000000000000000000000000000..339af48429b81c5abcbbd52bbf2a85197da5ddb4 GIT binary patch literal 2761 zcmeHJ%Wm306!oV>UZw*15gr}XMMI-DiWCqb>ISKrzz|!q89E-P4H5sIbH_GuU`*Od zT~sOyK%R3S=RVGO#6q9BSK=XFDS0>hoNqRX81i`}X6E~1rPZU$dh97L^o-{F}h{0qv-WUl6zhY<@=aO=*T%f;i3mP)u7X>v(5IF5AgU}L(7Rk`8mIuR$; zqhB!xMXXTFJgNpQ!yDnWoRk*6KWJ)doprnc=a>PF0Jc%Afwf4Iu|!MDd~737gF^uL zf@@wInm2lImoJw>nU;dh@_{z$-x^BjKkXc6I4PP+YLoJxEq*UPbUk-K^h2A5?)l#> z`!2?u?wQuSieU{*&TTWXbKal}cBv{W`;Q3&xJ)7MCse6YlK-+Y1iz&W!GC2i_lRRv z2CNH|0b7}fkGM`p3`_rsYcY-Sa#36p7;wm7vEYcBs?SnzPHn1odwZELFxWRRf)y+ E85Jav_W%F@ literal 0 HcmV?d00001 diff --git a/tests/data/v4.1.0_serialized_cache_entry b/tests/data/v4.1.0_serialized_cache_entry new file mode 100644 index 0000000000000000000000000000000000000000..72d74bf38817d45f5ae42a72a49095af3b435fd4 GIT binary patch literal 2761 zcmeHJ%Wm306!oV>UZw*15g;AZMMI-Dic}CG>ISKrzz|!qJ#;)y8zTO_=Z0DS0>lSZp?l81Y3U=H~k{)9TS>1NM|N-(#+G>L~H8 z5V>}TV-|$WEloaYy`E2UHJDGUj-{F}h{4>hQWuX?BhcOFLaO=)o$mQdWmP)u7X>v(5IF5AgU}L(7Rk`8mIuR%J zqhB!xMXXTFJgNpQ!zPF0Jc$VfVD`Hi9}1Qd~74ofI|TI zl55@=npb*oSFBb-nU;db$B2MmKRiDping3z7}PmbJG|YZ ziazNaX=A>YS#E|T0G0g&r}a7Oo8;``Rr-K4p37_!DNYU{Kg=Cpo1wR*Si5#|&2^EF z?~Ek$pLUKjoRm!^wMqGp4!;-g`kp%^`k_rj_x$gc zeHUXv_e^I|$FK$_=eC*HId9PgyHr(`{l|m>TxO8>6RK1t$$wcHg5Oex;J-4Md&IFS z1J(t~fUQi#dt9euhNb_&wV1_txhSs*3^-)4TypGo;00mF>v#O{#P2h|$HK7dnUA?A zu$>yV8<~l(`f+X6oWvP^>*s`nqc@(%JX%q#7Hw#i#?|UlDXC_7Uoo#xB&sZ literal 0 HcmV?d00001 diff --git a/tests/data/v4.1.1_serialized_cache_entry b/tests/data/v4.1.1_serialized_cache_entry new file mode 100644 index 0000000000000000000000000000000000000000..4866e7b8f2f273b96164732169c6d6c1d559b3b5 GIT binary patch literal 2737 zcmeHJO>f#j5cN-qe62wK0Fo~1p`lS5MGA-z^@3DwV2Lf+UblTx>Rp81Y3U=H~k%)9TS>r|c=mm#f70Lgd;V zj#&^gw}gMudOe@y>U2J>j!Qgwc3L1#Ds&{5?l5QM=VE65Iy+)eg`RPBFF@cLmccQ6 zp<<%xzdxjTi=&cafSe8};@HVifm9GFkGb>{z>gJKMTdy&N zQ*4EB9g!;^{4`q9P2_?nhUuUsI$DQbHo~{1v{L*&71;~bExHs~yjJ~oBEgqJaintx8`DLs$_-D~i8!er{faqK z#0tgCqiWDHyb(^zX=UO2!?vb2vyL~&Ic7j3fNc~TU@eklBGJ-{k8K1Ra0mcja?Kk< z^F|Nuiq%Re(^BNR-_mn)S~oQ?9k9(*6X!%fFe1or5T5lYMH}Z=1~rFjhqqf)5oDCe z*D}k^fCNZmpWL)QX9GjbE?lJ#2VKo&Hi;A`hmaoTj<3wX+vwGEekv| z6t)WNEE;R;6D=QTqxzkZgznS6afXw!p`ZstO~(zsY38y70eyt*i-?VfsIVW zM_i+0(uhx7i&>19it>8Ea6|se1;<_oUJ!P?LB|hI{8Q$6tk>^)=40*$Y^H|oMrPuh zeq5UsCsBsq`Z?j?=pAf5T2HJMVi*pWs|%&1n&BnIyfT@5r x5&b`c5@~@O=1=&C{ch0ndY-weNm**1JO1Gzf52{MqaArN0a3cEQAr}deFhyckSPEF literal 0 HcmV?d00001