| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
namespace Wikimedia\Tests; |
| 7 |
|
| 8 |
use Generator; |
| 9 |
use ReflectionClass; |
| 10 |
|
| 11 |
trait SerializationTestTrait { |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public function provideTestDeserialization(): Generator { |
| 21 |
|
| 22 |
$this->markTestSkippedIfPhp( '>=', '8.2' ); |
| 23 |
|
| 24 |
$className = self::getClassToTest(); |
| 25 |
foreach ( self::getSupportedSerializationFormats() as $serializationFormat ) { |
| 26 |
$serializationUtils = new SerializationTestUtils( |
| 27 |
self::getSerializedDataPath(), |
| 28 |
self::getTestInstances( self::getTestInstancesAndAssertions() ), |
| 29 |
$serializationFormat['ext'], |
| 30 |
$serializationFormat['serializer'], |
| 31 |
$serializationFormat['deserializer'] |
| 32 |
); |
| 33 |
foreach ( $serializationUtils->getTestInstances() as $testCaseName => $expectedObject ) { |
| 34 |
$deserializationFixtures = $serializationUtils->getFixturesForTestCase( |
| 35 |
$className, |
| 36 |
$testCaseName |
| 37 |
); |
| 38 |
foreach ( $deserializationFixtures as $deserializedObjectInfo ) { |
| 39 |
yield "{$className}:{$testCaseName}, " . |
| 40 |
"deserialized from {$deserializedObjectInfo->ext}, " . |
| 41 |
"{$deserializedObjectInfo->version}" => |
| 42 |
[ $serializationFormat['deserializer'], $expectedObject, $deserializedObjectInfo->data ]; |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function testDeserialization( callable $deserializer, object $expected, string $data ) { |
| 54 |
$deserialized = $deserializer( $data ); |
| 55 |
$this->assertInstanceOf( self::getClassToTest(), $deserialized ); |
| 56 |
$this->validateObjectEquality( $expected, $deserialized ); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function provideSerialization(): Generator { |
| 66 |
|
| 67 |
$this->markTestSkippedIfPhp( '>=', '8.2' ); |
| 68 |
|
| 69 |
$className = self::getClassToTest(); |
| 70 |
foreach ( self::getSupportedSerializationFormats() as $serializationFormat ) { |
| 71 |
$serializationUtils = new SerializationTestUtils( |
| 72 |
self::getSerializedDataPath(), |
| 73 |
self::getTestInstances( self::getTestInstancesAndAssertions() ), |
| 74 |
$serializationFormat['ext'], |
| 75 |
$serializationFormat['serializer'], |
| 76 |
$serializationFormat['deserializer'] |
| 77 |
); |
| 78 |
foreach ( $serializationUtils->getTestInstances() as $testCaseName => $testInstance ) { |
| 79 |
$expected = $serializationUtils->getStoredSerializedInstance( $className, $testCaseName ); |
| 80 |
|
| 81 |
if ( $expected->data === null ) { |
| 82 |
|
| 83 |
|
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
yield "{$className}:{$testCaseName}, " . |
| 88 |
"serialized with {$serializationFormat['ext']}" => |
| 89 |
[ |
| 90 |
$serializationFormat['serializer'], |
| 91 |
$serializationFormat['deserializer'], |
| 92 |
$expected->data, |
| 93 |
$testInstance |
| 94 |
]; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function testSerialization( callable $serializer, callable $deserializer, string $expected, object $testInstance ) { |
| 108 |
$serTestInstance = $serializer( $testInstance ); |
| 109 |
$deserExpected = $deserializer( $expected ); |
| 110 |
$this->assertNotEmpty( $deserExpected ); |
| 111 |
$deserTestInstance = $deserializer( $serTestInstance ); |
| 112 |
$this->assertNotEmpty( $deserTestInstance ); |
| 113 |
|
| 114 |
$this->validateObjectEquality( $deserExpected, $deserTestInstance ); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public static function provideSerializationRoundTrip(): Generator { |
| 124 |
$testCases = self::getTestInstancesAndAssertions(); |
| 125 |
$className = self::getClassToTest(); |
| 126 |
foreach ( self::getSupportedSerializationFormats() as $serializationFormat ) { |
| 127 |
foreach ( $testCases as $testCaseName => [ 'instance' => $instance ] ) { |
| 128 |
yield "{$className}:{$testCaseName}, " . |
| 129 |
"serialized with {$serializationFormat['ext']}" => [ |
| 130 |
$instance, |
| 131 |
$serializationFormat['serializer'], |
| 132 |
$serializationFormat['deserializer'] |
| 133 |
]; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function testSerializationRoundTrip( |
| 144 |
object $instance, |
| 145 |
callable $serializer, |
| 146 |
callable $deserializer |
| 147 |
) { |
| 148 |
$blob = $serializer( $instance ); |
| 149 |
$this->assertNotEmpty( $blob ); |
| 150 |
|
| 151 |
$actual = $deserializer( $blob ); |
| 152 |
$this->assertNotEmpty( $actual ); |
| 153 |
|
| 154 |
$this->validateObjectEquality( $instance, $actual ); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
private function validateArrayEquality( $expected, $actual, ?string $propName = null ) { |
| 163 |
$this->assertIsArray( $actual, "$propName: Expected array." ); |
| 164 |
$eKeys = array_keys( $expected ); |
| 165 |
$aKeys = array_keys( $actual ); |
| 166 |
$this->assertSame( count( $eKeys ), count( $aKeys ), "$propName: Expected equal-sized arrays." ); |
| 167 |
$i = 0; |
| 168 |
foreach ( $expected as $k => $v ) { |
| 169 |
$this->validateEquality( $k, $aKeys[$i], "$propName:$i" ); |
| 170 |
$this->validateEquality( $v, $actual[$k], $k ); |
| 171 |
$i++; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
private function validateEquality( $expected, $actual, ?string $propName = null ) { |
| 181 |
if ( is_array( $expected ) ) { |
| 182 |
$this->validateArrayEquality( $expected, $actual, $propName ); |
| 183 |
} elseif ( is_object( $expected ) ) { |
| 184 |
$this->assertIsObject( $actual, "Expected an object, but found: " . get_debug_type( $actual ) ); |
| 185 |
$this->validateObjectEquality( $expected, $actual ); |
| 186 |
} else { |
| 187 |
$this->assertSame( $expected, $actual, $propName ); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
private function validateObjectEquality( |
| 199 |
object $expected, |
| 200 |
object $actual, |
| 201 |
?ReflectionClass $class = null |
| 202 |
) { |
| 203 |
if ( !$class ) { |
| 204 |
$class = new ReflectionClass( $expected ); |
| 205 |
} |
| 206 |
|
| 207 |
foreach ( $class->getProperties() as $prop ) { |
| 208 |
$this->validateEquality( |
| 209 |
$prop->getValue( $expected ), |
| 210 |
$prop->getValue( $actual ), |
| 211 |
$prop->getName() |
| 212 |
); |
| 213 |
} |
| 214 |
|
| 215 |
$parent = $class->getParentClass(); |
| 216 |
if ( $parent ) { |
| 217 |
$this->validateObjectEquality( $expected, $actual, $parent ); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public static function provideCurrentVersionTestObjects(): Generator { |
| 227 |
$className = self::getClassToTest(); |
| 228 |
$testCases = self::getTestInstancesAndAssertions(); |
| 229 |
foreach ( $testCases as $testCaseName => $testCase ) { |
| 230 |
yield "{$className}:{$testCaseName}, current" => |
| 231 |
[ $testCase['instance'], $testCase['assertions'] ]; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public function provideDeserializedTestObjects(): Generator { |
| 243 |
|
| 244 |
$this->markTestSkippedIfPhp( '>=', '8.2' ); |
| 245 |
|
| 246 |
$className = self::getClassToTest(); |
| 247 |
$testCases = self::getTestInstancesAndAssertions(); |
| 248 |
$testObjects = self::getTestInstances( $testCases ); |
| 249 |
foreach ( self::getSupportedSerializationFormats() as $serializationFormat ) { |
| 250 |
$serializationUtils = new SerializationTestUtils( |
| 251 |
self::getSerializedDataPath(), |
| 252 |
$testObjects, |
| 253 |
$serializationFormat['ext'], |
| 254 |
$serializationFormat['serializer'], |
| 255 |
$serializationFormat['deserializer'] |
| 256 |
); |
| 257 |
foreach ( $testCases as $testCaseName => [ 'assertions' => $assertions ] ) { |
| 258 |
$deserializedObjects = $serializationUtils->getDeserializedInstancesForTestCase( |
| 259 |
$className, |
| 260 |
$testCaseName |
| 261 |
); |
| 262 |
foreach ( $deserializedObjects as $deserializedObjectInfo ) { |
| 263 |
yield "{$className}:{$testCaseName}, " . |
| 264 |
"deserialized from {$deserializedObjectInfo->ext}, " . |
| 265 |
"{$deserializedObjectInfo->version}" => |
| 266 |
[ |
| 267 |
$deserializedObjectInfo->object, |
| 268 |
$assertions |
| 269 |
]; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public function testAcceptanceOfDeserializedInstances( |
| 282 |
object $testInstance, |
| 283 |
callable $assertionsCallback |
| 284 |
) { |
| 285 |
call_user_func( $assertionsCallback, $this, $testInstance ); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
private static function getTestInstances( array $instancesAndAssertions ): array { |
| 294 |
return array_map( static function ( $testCase ) { |
| 295 |
return $testCase['instance']; |
| 296 |
}, $instancesAndAssertions ); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
abstract public static function getClassToTest(): string; |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
abstract public static function getSerializedDataPath(): string; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
abstract public static function getTestInstancesAndAssertions(): array; |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
abstract public static function getSupportedSerializationFormats(): array; |
| 326 |
} |