Debian Codegraph @

tests/phpunit/integration/includes/edit/validateSelserContextTestData.php

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