wiki.techinc.nl/tests/phpunit/unit/includes/libs/ParamValidator/Util/UploadedFileTestBase.php
Timo Tijhof 128debb64b tests: Change use of AtEase to at operator
Follows-up I361fde0de7f4406bce6ed075ed397effa5be3359.

Per T253461, not mass-changing source code, but the use of the native
error silencing operator (@) is especially useful in tests because:

1. It requires any/all statements to be explicitly marked. The
   suppressWarnings/restoreWarnings sections encourage developers to
   be "lazy" and thus encapsulate more than needed if there are multiple
   ones near each other, which would ignore potentially important
   warnings in a test case, which is generally exactly the time when
   it is really useful to get warnings etc.

2. It avoids leaking state, for example in LBFactoryTest the
   assertFalse call would throw a PHPUnit assertion error (not meant
   to be caught by the local catch), and thus won't reach
   AtEase::restoreWarnings. This then causes later code to end up
   in a mismatching state and creates a confusing error_reporting
   state.

See .phpcs.xml, where the at operator is allowed for all test code.

Change-Id: I68d1725d685e0a7586468bc9de6dc29ceea31b8a
2022-02-24 21:29:51 +00:00

80 lines
2 KiB
PHP

<?php
namespace Wikimedia\ParamValidator\Util;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
abstract class UploadedFileTestBase extends \PHPUnit\Framework\TestCase {
/** @var string|null */
protected static $tmpdir;
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
// Create a temporary directory for this test's files.
self::$tmpdir = null;
$base = sys_get_temp_dir() . DIRECTORY_SEPARATOR .
'phpunit-ParamValidator-UploadedFileTest-' . time() . '-' . getmypid() . '-';
for ( $i = 0; $i < 10000; $i++ ) {
$dir = $base . sprintf( '%04d', $i );
if ( @mkdir( $dir, 0700, false ) === true ) {
self::$tmpdir = $dir;
break;
}
}
if ( self::$tmpdir === null ) {
self::fail( "Could not create temporary directory '{$base}XXXX'" );
}
}
public static function tearDownAfterClass(): void {
// Clean up temporary directory.
if ( self::$tmpdir !== null ) {
$iter = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( self::$tmpdir, RecursiveDirectoryIterator::SKIP_DOTS ),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ( $iter as $file ) {
if ( $file->isDir() ) {
rmdir( $file->getRealPath() );
} else {
unlink( $file->getRealPath() );
}
}
rmdir( self::$tmpdir );
self::$tmpdir = null;
}
parent::tearDownAfterClass();
}
protected static function assertTmpdir() {
if ( self::$tmpdir === null || !is_dir( self::$tmpdir ) ) {
self::fail( 'No temporary directory for ' . static::class );
}
}
/**
* @param string $prefix For tempnam()
* @param string $content Contents of the file
* @return string Filename
*/
protected function makeTemp( $prefix, $content = 'foobar' ) {
self::assertTmpdir();
$filename = tempnam( self::$tmpdir, $prefix );
if ( $filename === false ) {
self::fail( 'Failed to create temporary file' );
}
self::assertSame(
strlen( $content ),
file_put_contents( $filename, $content ),
'Writing test temporary file'
);
return $filename;
}
}