Debian Codegraph @

tests/phpunit/includes/parser/validateParserCacheSerializationTestData.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Parser;
4
5 use MediaWiki\Logger\ConsoleLogger;
6 use MediaWiki\Maintenance\Maintenance;
7 use Wikimedia\Tests\SerializationTestUtils;
8
9 define( 'MW_AUTOLOAD_TEST_CLASSES', true );
10 define( 'MW_PHPUNIT_TEST', true );
11
12 require_once __DIR__ . '/../../../../maintenance/Maintenance.php';
13
14 // phpcs:disable MediaWiki.Files.ClassMatchesFilename.WrongCase
15 class ValidateParserCacheSerializationTestData extends Maintenance {
16
17 public function __construct() {
18 parent::__construct();
19
20 $this->addArg(
21 'path',
22 'Path of serialization files.',
23 false
24 );
25 $this->addOption( 'create', 'Create missing serialization' );
26 $this->addOption( 'update', 'Update mismatching serialization files' );
27 $this->addOption( 'version', 'Specify version for which to check serialization. '
28 . 'Also determines which files may be created or updated if '
29 . 'the respective options are set.'
30 . 'Unserialization is always checked against all versions. ', false, true );
31 }
32
33 public function execute() {
34 $testClasses = [ CacheTimeTest::class, ParserOutputTest::class ];
35 foreach ( $testClasses as $testClass ) {
36 $this->validateSerialization( $testClass,
37 array_map( static function ( $testCase ) {
38 return $testCase['instance'];
39 }, $testClass::getTestInstancesAndAssertions() ) );
40 }
41 }
42
43 /**
44 * Ensures that objects will serialize into the form expected for the given version.
45 * If the respective options are set in the constructor, this will create missing files or
46 * update mismatching files.
47 *
48 * @param string $testClassName
49 * @param array $testInstances
50 */
51 public function validateSerialization( string $testClassName, array $testInstances ) {
52 $className = $testClassName::getClassToTest();
53 $supportedFormats = $testClassName::getSupportedSerializationFormats();
54 $ok = true;
55 foreach ( $supportedFormats as $serializationFormat ) {
56 $serializationUtils = new SerializationTestUtils(
57 $this->getArg( 1 ) ?: $testClassName::getSerializedDataPath(),
58 $testInstances,
59 $serializationFormat['ext'],
60 $serializationFormat['serializer'],
61 $serializationFormat['deserializer']
62 );
63 $serializationUtils->setLogger( new ConsoleLogger( 'validator' ) );
64 foreach ( $serializationUtils->getSerializedInstances() as $testCaseName => $currentSerialized ) {
65 $expected = $serializationUtils
66 ->getStoredSerializedInstance( $className, $testCaseName, $this->getOption( 'version' ) );
67 $ok = $this->validateSerializationData( $currentSerialized, $expected ) && $ok;
68 }
69 }
70 if ( !$ok ) {
71 $this->output( "\n\n" );
72 $this->fatalError( "Serialization data mismatch! "
73 . "If this was expected, rerun the script with the --update option "
74 . "to update the expected serialization. WARNING: make sure "
75 . "a forward compatible version of the code is live before deploying a "
76 . "serialization change!\n"
77 );
78 }
79 }
80
81 private function validateSerializationData( $data, $fileInfo ): bool {
82 if ( !$fileInfo->data ) {
83 if ( $this->hasOption( 'create' ) ) {
84 $this->output( 'Creating file: ' . $fileInfo->path . "\n" );
85 file_put_contents( $fileInfo->path, $data );
86 } else {
87 $this->fatalError( "File not found: {$fileInfo->path}. "
88 . "Rerun the script with the --create option set to create it."
89 );
90 }
91 } else {
92 if ( $data !== $fileInfo->data ) {
93 if ( $this->hasOption( 'update' ) ) {
94 $this->output( 'Data mismatch, updating file: ' . $fileInfo->currentVersionPath . "\n" );
95 file_put_contents( $fileInfo->currentVersionPath, $data );
96 } else {
97 $this->output( 'Serialization MISMATCH: ' . $fileInfo->path . "\n" );
98 return false;
99 }
100 } else {
101 $this->output( "Serialization OK: " . $fileInfo->path . "\n" );
102 }
103 }
104 return true;
105 }
106 }
107
108 return ValidateParserCacheSerializationTestData::class;