1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
<?php
// phpcs:disable MediaWiki.Commenting.FunctionComment.ObjectTypeHintParam
// phpcs:disable MediaWiki.Commenting.FunctionComment.MissingParamTag -- Traits are not excluded
namespace Wikimedia\Tests;
use Generator;
use ReflectionClass;
trait SerializationTestTrait {
/**
* Data provider for deserialization test.
* - For each supported serialization format defined by ::getSupportedSerializationFormats
* - For each acceptance test instance defined by ::getTestInstancesAndAssertions
* - For each object deserialized from stored file for a particular MW version
* @return Generator for [ callable $deserializer, object $expectedObject, string $dataToDeserialize ]
*/
public function provideTestDeserialization(): Generator {
// Creation of dynamic property is deprecated, can happen as backward-compatibility check
$this->markTestSkippedIfPhp( '>=', '8.2' );
$className = self::getClassToTest();
foreach ( self::getSupportedSerializationFormats() as $serializationFormat ) {
$serializationUtils = new SerializationTestUtils(
self::getSerializedDataPath(),
self::getTestInstances( self::getTestInstancesAndAssertions() ),
$serializationFormat['ext'],
$serializationFormat['serializer'],
$serializationFormat['deserializer']
);
foreach ( $serializationUtils->getTestInstances() as $testCaseName => $expectedObject ) {
$deserializationFixtures = $serializationUtils->getFixturesForTestCase(
$className,
$testCaseName
);
foreach ( $deserializationFixtures as $deserializedObjectInfo ) {
yield "{$className}:{$testCaseName}, " .
"deserialized from {$deserializedObjectInfo->ext}, " .
"{$deserializedObjectInfo->version}" =>
[ $serializationFormat['deserializer'], $expectedObject, $deserializedObjectInfo->data ];
}
}
}
}
/**
* Tests that $deserialized objects retrieved from stored files for various MW versions
* equal to the $expected
* @dataProvider provideTestDeserialization
*/
public function testDeserialization( callable $deserializer, object $expected, string $data ) {
$deserialized = $deserializer( $data );
$this->assertInstanceOf( self::getClassToTest(), $deserialized );
$this->validateObjectEquality( $expected, $deserialized );
}
/**
* Data provider for serialization test.
* - For each supported serialization format defined by ::getSupportedSerializationFormats
* - For each acceptance test instance defined by ::getTestInstancesAndAssertions
* @return Generator for [ callable $serializer, string $expectedSerialization, object $testInstanceToSerialize ]
*/
public function provideSerialization(): Generator {
// Creation of dynamic property is deprecated, can happen as backward-compatibility check
$this->markTestSkippedIfPhp( '>=', '8.2' );
$className = self::getClassToTest();
foreach ( self::getSupportedSerializationFormats() as $serializationFormat ) {
$serializationUtils = new SerializationTestUtils(
self::getSerializedDataPath(),
self::getTestInstances( self::getTestInstancesAndAssertions() ),
$serializationFormat['ext'],
$serializationFormat['serializer'],
$serializationFormat['deserializer']
);
foreach ( $serializationUtils->getTestInstances() as $testCaseName => $testInstance ) {
$expected = $serializationUtils->getStoredSerializedInstance( $className, $testCaseName );
if ( $expected->data === null ) {
// The fixture file is missing. This will be detected and reported elsewhere.
// No need to cause an error here.
continue;
}
yield "{$className}:{$testCaseName}, " .
"serialized with {$serializationFormat['ext']}" =>
[
$serializationFormat['serializer'],
$serializationFormat['deserializer'],
$expected->data,
$testInstance
];
}
}
}
/**
* Test that the current master $serialized instances are
* equal to stored $expected instances.
* Serialization formats might change in backwards compatible ways
* (in particular, php 8.1 orders protected instance variables differently
* than earlier php), so do the comparision on the deserialized version.
* @dataProvider provideSerialization
*/
public function testSerialization( callable $serializer, callable $deserializer, string $expected, object $testInstance ) {
$serTestInstance = $serializer( $testInstance );
$deserExpected = $deserializer( $expected );
$this->assertNotEmpty( $deserExpected );
$deserTestInstance = $deserializer( $serTestInstance );
$this->assertNotEmpty( $deserTestInstance );
$this->validateObjectEquality( $deserExpected, $deserTestInstance );
}
/**
* Data provider for serialization round trip test.
* - For each supported serialization format defined by ::getSupportedSerializationFormats
* - For each test instance defined by ::getTestInstances
* @return Generator for [ object $instance, callable $serializer, callable $deserializer ]
*/
public static function provideSerializationRoundTrip(): Generator {
$testCases = self::getTestInstancesAndAssertions();
$className = self::getClassToTest();
foreach ( self::getSupportedSerializationFormats() as $serializationFormat ) {
foreach ( $testCases as $testCaseName => [ 'instance' => $instance ] ) {
yield "{$className}:{$testCaseName}, " .
"serialized with {$serializationFormat['ext']}" => [
$instance,
$serializationFormat['serializer'],
$serializationFormat['deserializer']
];
}
}
}
/**
* Test that the $expected instance can be serialized and successfully be deserialized again.
*
* @dataProvider provideSerializationRoundTrip
*/
public function testSerializationRoundTrip(
object $instance,
callable $serializer,
callable $deserializer
) {
$blob = $serializer( $instance );
$this->assertNotEmpty( $blob );
$actual = $deserializer( $blob );
$this->assertNotEmpty( $actual );
$this->validateObjectEquality( $instance, $actual );
}
/**
* @param mixed $expected
* @param mixed $actual
* @param string|null $propName
*/
private function validateArrayEquality( $expected, $actual, ?string $propName = null ) {
$this->assertIsArray( $actual, "$propName: Expected array." );
$eKeys = array_keys( $expected );
$aKeys = array_keys( $actual );
$this->assertSame( count( $eKeys ), count( $aKeys ), "$propName: Expected equal-sized arrays." );
$i = 0;
foreach ( $expected as $k => $v ) {
$this->validateEquality( $k, $aKeys[$i], "$propName:$i" );
$this->validateEquality( $v, $actual[$k], $k );
$i++;
}
}
/**
* @param mixed $expected
* @param mixed $actual
* @param string|null $propName
*/
private function validateEquality( $expected, $actual, ?string $propName = null ) {
if ( is_array( $expected ) ) {
$this->validateArrayEquality( $expected, $actual, $propName );
} elseif ( is_object( $expected ) ) {
$this->assertIsObject( $actual, "Expected an object, but found: " . get_debug_type( $actual ) );
$this->validateObjectEquality( $expected, $actual );
} else {
$this->assertSame( $expected, $actual, $propName );
}
}
/**
* Asserts that all the fields across class hierarchy for
* provided objects are equal.
* @param object $expected
* @param object $actual
* @param ReflectionClass|null $class
*/
private function validateObjectEquality(
object $expected,
object $actual,
?ReflectionClass $class = null
) {
if ( !$class ) {
$class = new ReflectionClass( $expected );
}
foreach ( $class->getProperties() as $prop ) {
$prop->setAccessible( true );
$this->validateEquality(
$prop->getValue( $expected ),
$prop->getValue( $actual ),
$prop->getName()
);
}
$parent = $class->getParentClass();
if ( $parent ) {
$this->validateObjectEquality( $expected, $actual, $parent );
}
}
/**
* Data provider for acceptance testing, returning object instances created by current code.
* - For each acceptance test instance defined by ::getTestInstancesAndAssertions
* @return Generator for [ $instance which to run assertions on, $assertionsCallback ]
*/
public static function provideCurrentVersionTestObjects(): Generator {
$className = self::getClassToTest();
$testCases = self::getTestInstancesAndAssertions();
foreach ( $testCases as $testCaseName => $testCase ) {
yield "{$className}:{$testCaseName}, current" =>
[ $testCase['instance'], $testCase['assertions'] ];
}
}
/**
* Data provider for acceptance testing, returning instances deserialized
* from stored files for various MW versions.
* - For each supported serialization format defined by ::getSupportedSerializationFormats
* - For each object deserialized from stored file for a particular MW version
* @return Generator for [ $instance which to run assertions on, $assertionsCallback ]
*/
public function provideDeserializedTestObjects(): Generator {
// Creation of dynamic property is deprecated, can happen as backward-compatibility check
$this->markTestSkippedIfPhp( '>=', '8.2' );
$className = self::getClassToTest();
$testCases = self::getTestInstancesAndAssertions();
$testObjects = self::getTestInstances( $testCases );
foreach ( self::getSupportedSerializationFormats() as $serializationFormat ) {
$serializationUtils = new SerializationTestUtils(
self::getSerializedDataPath(),
$testObjects,
$serializationFormat['ext'],
$serializationFormat['serializer'],
$serializationFormat['deserializer']
);
foreach ( $testCases as $testCaseName => [ 'assertions' => $assertions ] ) {
$deserializedObjects = $serializationUtils->getDeserializedInstancesForTestCase(
$className,
$testCaseName
);
foreach ( $deserializedObjects as $deserializedObjectInfo ) {
yield "{$className}:{$testCaseName}, " .
"deserialized from {$deserializedObjectInfo->ext}, " .
"{$deserializedObjectInfo->version}" =>
[
$deserializedObjectInfo->object,
$assertions
];
}
}
}
}
/**
* Tests that assertions in $assertionsCallback succeed on $testInstance.
* @see self::getTestInstancesAndAssertions()
* @dataProvider provideDeserializedTestObjects
* @dataProvider provideCurrentVersionTestObjects
*/
public function testAcceptanceOfDeserializedInstances(
object $testInstance,
callable $assertionsCallback
) {
call_user_func( $assertionsCallback, $this, $testInstance );
}
/**
* Returns a map of $testCaseName to an instance to test.
* @param array[] $instancesAndAssertions
* @return array
*/
private static function getTestInstances( array $instancesAndAssertions ): array {
return array_map( static function ( $testCase ) {
return $testCase['instance'];
}, $instancesAndAssertions );
}
/**
* @return string the name of the class to test.
*/
abstract public static function getClassToTest(): string;
/**
* @return string the path to serialized data.
*/
abstract public static function getSerializedDataPath(): string;
/**
* @return array a map of $testCaseName to a map, containing the following keys:
* - 'instance' => an instance of the object to perform assertions on.
* - 'assertions' => a callable that performs assertions on the deserialized objects.
* Callable signature: ( MediaWikiIntegrationTestCase $testCase, object $instance )
*/
abstract public static function getTestInstancesAndAssertions(): array;
/**
* Get a list of serialization formats supported by the tested class.
* @return string[][] a list of supported serialization formats info map,
* containing the following keys:
* - 'ext' => string file extension for stored serializations
* - 'serializer' => callable to serialize objects
* - 'deserializer' => callable to deserialize objects
*/
abstract public static function getSupportedSerializationFormats(): array;
}
|