Debian Codegraph @

tests/phpunit/includes/content/validateContentTestData.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Content;
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 ValidateContentTestData 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 $tests = [
35 WikitextContentTest::class,
36 JavaScriptContentTest::class,
37 CssContentTest::class,
38 ];
39 foreach ( $tests as $testClass ) {
40 $objClass = $testClass::getClassToTest();
41 $this->validateSerialization(
42 $objClass,
43 $testClass::getSerializedDataPath(),
44 $testClass::getSupportedSerializationFormats(),
45 array_map( static function ( $testCase ) {
46 return $testCase['instance'];
47 }, $testClass::getTestInstancesAndAssertions() )
48 );
49 }
50 }
51
52 /**
53 * Ensures that objects will serialize into the form expected for the given version.
54 * If the respective options are set in the constructor, this will create missing files or
55 * update mismatching files.
56 *
57 * @param string $className
58 * @param string $defaultDirectory
59 * @param array $supportedFormats
60 * @param array $testInstances
61 */
62 public function validateSerialization(
63 string $className,
64 string $defaultDirectory,
65 array $supportedFormats,
66 array $testInstances
67 ) {
68 $ok = true;
69 foreach ( $supportedFormats as $serializationFormat ) {
70 $serializationUtils = new SerializationTestUtils(
71 $this->getArg( 1 ) ?: $defaultDirectory,
72 $testInstances,
73 $serializationFormat['ext'],
74 $serializationFormat['serializer'],
75 $serializationFormat['deserializer']
76 );
77 $serializationUtils->setLogger( new ConsoleLogger( 'validator' ) );
78 foreach ( $serializationUtils->getSerializedInstances() as $testCaseName => $currentSerialized ) {
79 $expected = $serializationUtils
80 ->getStoredSerializedInstance( $className, $testCaseName, $this->getOption( 'version' ) );
81 $ok = $this->validateSerializationData( $currentSerialized, $expected ) && $ok;
82 }
83 }
84 if ( !$ok ) {
85 $this->output( "\n\n" );
86 $this->fatalError( "Serialization data mismatch! "
87 . "If this was expected, rerun the script with the --update option "
88 . "to update the expected serialization. WARNING: make sure "
89 . "a forward compatible version of the code is live before deploying a "
90 . "serialization change!\n"
91 );
92 }
93 }
94
95 private function validateSerializationData( string $data, \stdClass $fileInfo ): bool {
96 if ( !$fileInfo->data ) {
97 if ( $this->hasOption( 'create' ) ) {
98 $this->output( 'Creating file: ' . $fileInfo->path . "\n" );
99 file_put_contents( $fileInfo->path, $data );
100 } else {
101 $this->fatalError( "File not found: {$fileInfo->path}. "
102 . "Rerun the script with the --create option set to create it."
103 );
104 }
105 } else {
106 if ( $data !== $fileInfo->data ) {
107 if ( $this->hasOption( 'update' ) ) {
108 $this->output( 'Data mismatch, updating file: ' . $fileInfo->currentVersionPath . "\n" );
109 file_put_contents( $fileInfo->currentVersionPath, $data );
110 } else {
111 $this->output( 'Serialization MISMATCH: ' . $fileInfo->path . "\n" );
112 return false;
113 }
114 } else {
115 $this->output( "Serialization OK: " . $fileInfo->path . "\n" );
116 }
117 }
118 return true;
119 }
120 }
121
122 return ValidateContentTestData::class;