|
3 | 3 | namespace mle86\Value\Tests; |
4 | 4 |
|
5 | 5 | use mle86\Value\AbstractSerializableValue; |
| 6 | +use mle86\Value\InvalidArgumentException; |
6 | 7 | use mle86\Value\Tests\Helpers\TestSWrapper6; |
7 | 8 | use mle86\Value\Value; |
8 | 9 | use PHPUnit\Framework\TestCase; |
|
14 | 15 | class SerializableValueTest extends TestCase |
15 | 16 | { |
16 | 17 |
|
17 | | - const VALID_INPUT = "61234"; |
18 | | - const INVALID_INPUT = "61234 "; |
19 | | - const VALID_INPUT2 = "69999"; |
| 18 | + const VALID_INPUT = "61234"; |
| 19 | + const INVALID_INPUT = "61234 "; |
| 20 | + const VALID_INPUT2 = "69999"; |
| 21 | + const INVALID_INPUT2 = "79990"; |
20 | 22 |
|
21 | 23 |
|
22 | 24 | public function testClassExists() |
@@ -118,4 +120,31 @@ public function testPhpUnserialize(string $serialization, AbstractSerializableVa |
118 | 120 | "Original object doesn't equal an unserialized object anymore!"); |
119 | 121 | } |
120 | 122 |
|
| 123 | + /** |
| 124 | + * @depends testPhpSerialize |
| 125 | + * @depends testPhpUnserialize |
| 126 | + */ |
| 127 | + public function testSerializedValueValidity() |
| 128 | + { |
| 129 | + $validInput = self::VALID_INPUT2; |
| 130 | + $invalidInput = self::INVALID_INPUT2; |
| 131 | + if (strlen($validInput) !== strlen($invalidInput)) { |
| 132 | + // We just want to replace the stored string without breaking the serialization format. |
| 133 | + // That only works if the replacement string is of the same length |
| 134 | + // because the serialization contains a length indicator. |
| 135 | + $this->markTestSkipped("Cannot test invalid serialization; strings are not of same length"); |
| 136 | + } |
| 137 | + |
| 138 | + $validSerialization = serialize(new TestSWrapper6($validInput)); |
| 139 | + |
| 140 | + $reValidInput = '/\b' . preg_quote($validInput, '/') . '\b/'; |
| 141 | + $invalidSerialization = preg_replace($reValidInput, $invalidInput, $validSerialization, -1, $nReplaced); |
| 142 | + if ($nReplaced !== 1) { |
| 143 | + $this->markTestSkipped("Cannot test invalid serialization; manipulation of serialization string failed"); |
| 144 | + } |
| 145 | + |
| 146 | + $this->expectException(InvalidArgumentException::class); // ! |
| 147 | + unserialize($invalidSerialization); |
| 148 | + } |
| 149 | + |
121 | 150 | } |
0 commit comments