| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
namespace Wikimedia\Tests; |
| 26 |
|
| 27 |
use InvalidArgumentException; |
| 28 |
use Psr\Log\LoggerInterface; |
| 29 |
use Psr\Log\NullLogger; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class SerializationTestUtils { |
| 35 |
|
| 36 |
|
| 37 |
private $serializedDataPath; |
| 38 |
|
| 39 |
|
| 40 |
private $ext; |
| 41 |
|
| 42 |
|
| 43 |
private $serializer; |
| 44 |
|
| 45 |
|
| 46 |
private $deserializer; |
| 47 |
|
| 48 |
|
| 49 |
private $testInstances; |
| 50 |
|
| 51 |
|
| 52 |
private $logger; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function __construct( |
| 62 |
string $serializedDataPath, |
| 63 |
array $testInstances, |
| 64 |
string $ext, |
| 65 |
callable $serializer, |
| 66 |
callable $deserializer |
| 67 |
) { |
| 68 |
if ( !is_dir( $serializedDataPath ) ) { |
| 69 |
throw new InvalidArgumentException( "{$serializedDataPath} does not exist" ); |
| 70 |
} |
| 71 |
$this->serializedDataPath = $serializedDataPath; |
| 72 |
$this->ext = $ext; |
| 73 |
$this->serializer = $serializer; |
| 74 |
$this->deserializer = $deserializer; |
| 75 |
$this->testInstances = $testInstances; |
| 76 |
$this->logger = new NullLogger(); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function setLogger( LoggerInterface $logger ): void { |
| 83 |
$this->logger = $logger; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private function getMatchingFiles( string $class, string $ext ): array { |
| 93 |
$classFile = self::classToFile( $class ); |
| 94 |
$glob = $this->serializedDataPath . "/*-{$classFile}-*.$ext"; |
| 95 |
$matches = glob( $glob ); |
| 96 |
|
| 97 |
if ( !$matches ) { |
| 98 |
$this->log( 'No matches found for ' . $glob ); |
| 99 |
return []; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
$pattern = '!/([^/-]+)-' . preg_quote( $classFile, '!' ) . '-([^/-]+)\.[^/]+$!'; |
| 104 |
|
| 105 |
$files = []; |
| 106 |
foreach ( $matches as $path ) { |
| 107 |
if ( !preg_match( $pattern, $path, $m ) ) { |
| 108 |
$this->logger->warning( 'Skipping file with malformed name: ' . $path ); |
| 109 |
continue; |
| 110 |
} |
| 111 |
$version = $m[1]; |
| 112 |
$testCaseName = $m[2]; |
| 113 |
|
| 114 |
$files[] = $this->getStoredSerializedInstance( $class, $testCaseName, $version ); |
| 115 |
} |
| 116 |
|
| 117 |
return $files; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function getTestInstances(): array { |
| 125 |
return $this->testInstances; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function getTestInstanceForTestCase( string $testCaseName ): object { |
| 134 |
$instances = $this->getTestInstances(); |
| 135 |
if ( !array_key_exists( $testCaseName, $instances ) ) { |
| 136 |
throw new InvalidArgumentException( |
| 137 |
"Test instance not found for test case {$testCaseName}" |
| 138 |
); |
| 139 |
} |
| 140 |
return $instances[$testCaseName]; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
private function getDeserializedInstances( string $class ): array { |
| 150 |
return array_map( function ( $fileInfo ) { |
| 151 |
$fileInfo->object = call_user_func( $this->deserializer, $fileInfo->data ); |
| 152 |
return $fileInfo; |
| 153 |
}, $this->getMatchingFiles( $class, $this->ext ) ); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function getFixturesForTestCase( string $class, string $testCaseName ): array { |
| 164 |
return array_filter( |
| 165 |
$this->getMatchingFiles( $class, $this->ext ), |
| 166 |
static function ( $fileInfo ) use ( $testCaseName ) { |
| 167 |
return $fileInfo->testCaseName === $testCaseName; |
| 168 |
} ); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public function getDeserializedInstancesForTestCase( string $class, string $testCaseName ): array { |
| 179 |
return array_filter( $this->getDeserializedInstances( $class ), |
| 180 |
static function ( $fileInfo ) use ( $testCaseName ) { |
| 181 |
return $fileInfo->testCaseName === $testCaseName; |
| 182 |
} |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public function getSerializedInstances(): array { |
| 192 |
$instances = $this->getTestInstances(); |
| 193 |
return array_map( function ( $object ) { |
| 194 |
return call_user_func( $this->serializer, $object ); |
| 195 |
}, $instances ); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public function getStoredSerializedInstance( |
| 207 |
string $class, |
| 208 |
string $testCaseName, |
| 209 |
?string $version = null |
| 210 |
) { |
| 211 |
$classFile = self::classToFile( $class ); |
| 212 |
$curPath = "$this->serializedDataPath/{$this->getCurrentVersion()}-$classFile-$testCaseName.$this->ext"; |
| 213 |
if ( $version ) { |
| 214 |
$path = "$this->serializedDataPath/$version-$classFile-$testCaseName.$this->ext"; |
| 215 |
} else { |
| 216 |
|
| 217 |
$savedFiles = glob( "$this->serializedDataPath/?.??*-$classFile-$testCaseName.$this->ext" ); |
| 218 |
if ( count( $savedFiles ) > 0 ) { |
| 219 |
|
| 220 |
usort( |
| 221 |
$savedFiles, |
| 222 |
fn ( $a, $b ) => strtr( $a, '-_', '_-' ) <=> strtr( $b, '-_', '_-' ) |
| 223 |
); |
| 224 |
$path = end( $savedFiles ); |
| 225 |
} else { |
| 226 |
|
| 227 |
|
| 228 |
$path = $curPath; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
return (object)[ |
| 233 |
'version' => $version, |
| 234 |
'class' => $class, |
| 235 |
'testCaseName' => $testCaseName, |
| 236 |
'ext' => $this->ext, |
| 237 |
'path' => $path, |
| 238 |
'currentVersionPath' => $curPath, |
| 239 |
'data' => is_file( $path ) ? file_get_contents( $path ) : null, |
| 240 |
]; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
private function getCurrentVersion(): string { |
| 248 |
return preg_replace( '/^(\d\.\d+).*$/', '$1', MW_VERSION ); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
private static function classToFile( string $class ): string { |
| 262 |
$arr = explode( '\\', $class ); |
| 263 |
return end( $arr ); |
| 264 |
} |
| 265 |
|
| 266 |
private function log( $msg ) { |
| 267 |
$this->logger->info( $msg ); |
| 268 |
} |
| 269 |
} |