Debian Codegraph @

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