wiki.techinc.nl/tests/phpunit/includes/api/generateRandomImages.php
Timo Tijhof 63ddc8c436 tests: Replace RandomImageGenerator dictionary logic with rand+time
The main purpose of this class is to generate filenames during
tests. The reason it doesn't use fixed filenames is not because
it wants to be a fuzzy test, but because it wants to allow
running against a development wiki (outside CI) multiple
times where one test may've failed and unable to clean up after
itself, or where you may've done something manually and thus
a conflicting file exists in the local wiki from before
the DB was cloned for testing.

Using dictionary based random names is needlessly complex,
and has the additional downside of actually not avoiding
conflicts very well.

Replace all this by using a timestamp instead, which is much
more stable over time and basically will never conflict with
itself unless two tests were run on the same machine and started
in the same second (or if the clock went backwards/broken).
That seems unlikely enough to not need to support magically.
But to counter-act it a bit still, also add a 3-char random hex
in front of it. This also helps with file listings so that
when you run it three times, it's easier to see the files generated
by the same run close to each other (by not having subsequent
tests start with a mostly identical prefix).

Bug: T222416
Change-Id: Iec1d89324ff2fa53d1712796157adaf4ed34bdfe
2019-10-25 02:07:21 +01:00

44 lines
960 B
PHP

<?php
/**
* Bootstrapping for test image file generation
*
* @file
*/
// Start up MediaWiki in command-line mode
require_once __DIR__ . "/../../../../maintenance/Maintenance.php";
require __DIR__ . "/RandomImageGenerator.php";
class GenerateRandomImages extends Maintenance {
public function getDbType() {
return Maintenance::DB_NONE;
}
public function execute() {
$getOptSpec = [
'minWidth::',
'maxWidth::',
'minHeight::',
'maxHeight::',
'shapesToDraw::',
'shape::',
'number::',
'format::'
];
$options = getopt( null, $getOptSpec );
$format = $options['format'] ?? 'jpg';
unset( $options['format'] );
$number = isset( $options['number'] ) ? intval( $options['number'] ) : 10;
unset( $options['number'] );
$randomImageGenerator = new RandomImageGenerator( $options );
$randomImageGenerator->writeImages( $number, $format );
}
}
$maintClass = 'GenerateRandomImages';
require RUN_MAINTENANCE_IF_MAIN;