|
3 | 3 | namespace mle86\Value\Tests; |
4 | 4 |
|
5 | 5 | use mle86\Value\AbstractSerializableValue; |
| 6 | +use mle86\Value\AbstractValue; |
6 | 7 | use mle86\Value\InvalidArgumentException; |
7 | 8 | use mle86\Value\Tests\Helpers\TestSWrapper6; |
8 | 9 | use mle86\Value\Value; |
@@ -100,6 +101,10 @@ public function testPhpSerialize(AbstractSerializableValue $tw): string |
100 | 101 | $ser = serialize($tw); |
101 | 102 | $this->assertNotEmpty($ser); |
102 | 103 | return $ser; |
| 104 | + |
| 105 | + # TODO: if we raise our PHP dependency to 7.4+ |
| 106 | + # we can assume that $set came directly from the __serialize method; |
| 107 | + # that means we can assert that it doesn't contain the $isSet flag. |
103 | 108 | } |
104 | 109 |
|
105 | 110 | /** |
@@ -147,4 +152,78 @@ public function testSerializedValueValidity() |
147 | 152 | unserialize($invalidSerialization); |
148 | 153 | } |
149 | 154 |
|
| 155 | + /** |
| 156 | + * Ensures that the future PHP7.4 serialization format |
| 157 | + * is already accepted (and correctly processed) |
| 158 | + * by this version of the library. |
| 159 | + * |
| 160 | + * @depends testPhpUnserialize |
| 161 | + * @depends testSerializedValueValidity |
| 162 | + */ |
| 163 | + public function testUnserializationForwardCompatibility() |
| 164 | + { |
| 165 | + $twFqcn = TestSWrapper6::class; |
| 166 | + $twFqcnLen = strlen($twFqcn); |
| 167 | + $inputValue = self::VALID_INPUT2; |
| 168 | + $inputLen = strlen($inputValue); |
| 169 | + $invalidInputValue = self::INVALID_INPUT2; |
| 170 | + $invalidInputLen = strlen($inputValue); |
| 171 | + |
| 172 | + $validPhp74Serialization = |
| 173 | + "O:{$twFqcnLen}:\"{$twFqcn}\":1:{i:0;s:{$inputLen}:\"{$inputValue}\";}"; |
| 174 | + $invalidPhp74Serialization = |
| 175 | + "O:{$twFqcnLen}:\"{$twFqcn}\":1:{i:0;s:{$invalidInputLen}:\"{$invalidInputValue}\";}"; |
| 176 | + |
| 177 | + /** @var TestSWrapper6 $unserializedObject */ |
| 178 | + $unserializedObject = unserialize($validPhp74Serialization); |
| 179 | + $this->assertInstanceOf(TestSWrapper6::class, $unserializedObject, |
| 180 | + "FC Unserialization returned object of wrong class!"); |
| 181 | + $this->assertEquals($inputValue, $unserializedObject->value(), |
| 182 | + "FC Unserialized object has incorrect value!"); |
| 183 | + |
| 184 | + $this->expectException(InvalidArgumentException::class); |
| 185 | + unserialize($invalidPhp74Serialization); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Ensures that the pre-PHP7.4 serialization format |
| 190 | + * is still accepted and correctly processed, |
| 191 | + * even if this library is being used within PHP 7.4+. |
| 192 | + * |
| 193 | + * @depends testPhpUnserialize |
| 194 | + * @depends testSerializedValueValidity |
| 195 | + */ |
| 196 | + public function testUnserializationBackwardCompatibility() |
| 197 | + { |
| 198 | + $twFqcn = TestSWrapper6::class; |
| 199 | + $twFqcnLen = strlen($twFqcn); |
| 200 | + $valVarName = "\0" . AbstractValue::class . "\0" . 'value'; |
| 201 | + $valVarLen = strlen($valVarName); |
| 202 | + $setVarName = "\0" . AbstractValue::class . "\0" . 'isSet'; |
| 203 | + $setVarLen = strlen($valVarName); |
| 204 | + $inputValue = self::VALID_INPUT2; |
| 205 | + $inputLen = strlen($inputValue); |
| 206 | + $invalidInputValue = self::INVALID_INPUT2; |
| 207 | + $invalidInputLen = strlen($inputValue); |
| 208 | + |
| 209 | + $validPhp73Serialization = |
| 210 | + "O:{$twFqcnLen}:\"{$twFqcn}\":2:{". |
| 211 | + "s:{$valVarLen}:\"{$valVarName}\";s:{$inputLen}:\"{$inputValue}\";". |
| 212 | + "s:{$setVarLen}:\"{$setVarName}\";b:1;}"; |
| 213 | + $invalidPhp73Serialization = |
| 214 | + "O:{$twFqcnLen}:\"{$twFqcn}\":2:{". |
| 215 | + "s:{$valVarLen}:\"{$valVarName}\";s:{$invalidInputLen}:\"{$invalidInputValue}\";". |
| 216 | + "s:{$setVarLen}:\"{$setVarName}\";b:1;}"; |
| 217 | + |
| 218 | + /** @var TestSWrapper6 $unserializedObject */ |
| 219 | + $unserializedObject = unserialize($validPhp73Serialization); |
| 220 | + $this->assertInstanceOf(TestSWrapper6::class, $unserializedObject, |
| 221 | + "BC Unserialization returned object of wrong class!"); |
| 222 | + $this->assertEquals($inputValue, $unserializedObject->value(), |
| 223 | + "BC Unserialized object has incorrect value!"); |
| 224 | + |
| 225 | + $this->expectException(InvalidArgumentException::class); |
| 226 | + unserialize($invalidPhp73Serialization); |
| 227 | + } |
| 228 | + |
150 | 229 | } |
0 commit comments