All the other ways of doing it were ridiculous and much harder to read, and usually required repeating the needle expression (to get its length). I found these occurrences by grepping for various expressions, but I undoubtedly missed some. I didn't try replacing the many instances of strpos(...) === 0 with str_starts_with(...), because I think they're readable enough as-is (although less efficient). Likewise I didn't try porting strpos(...) !== false to str_contains(...). For case-insensitive comparisons, Tim Starling requested that we stick with substr_compare() because it's more efficient than calling strtolower(). On PHP < 8 these functions will be included with a polyfill via vendor/autoload.php. This is included at the beginning of includes/AutoLoader.php, so if our autoloader has been included the polyfill will be available. This means it should be safe to call these functions from any code that would not be usable without our autoloader. Three uses that Tim Starling identified as being performance-sensitive have been split out to a separate commit for porting after the switch to PHP 8. Change-Id: I113a8d052b6845852c15969a2f0e6fbbe3e9f8d9
54 lines
1.6 KiB
PHP
54 lines
1.6 KiB
PHP
<?php
|
|
|
|
use SebastianBergmann\FileIterator\Facade;
|
|
|
|
/**
|
|
* The tests here verify the structure of the code. This is for outright bugs,
|
|
* not just style issues.
|
|
*/
|
|
class StructureTest extends \PHPUnit\Framework\TestCase {
|
|
/**
|
|
* Verify all files that appear to be tests have file names ending in
|
|
* Test. If the file names do not end in Test, they will not be run.
|
|
* @group medium
|
|
*/
|
|
public function testUnitTestFileNamesEndWithTest() {
|
|
// realpath() also normalizes directory separator on windows for prefix compares
|
|
$rootPath = realpath( __DIR__ . '/..' );
|
|
$suitesPath = realpath( __DIR__ . '/../suites/' );
|
|
$testClassRegex = '/^(final )?class .* extends [\S]*(TestCase|TestBase)\\b/m';
|
|
|
|
$results = $this->recurseFiles( $rootPath );
|
|
|
|
$results = array_filter(
|
|
$results,
|
|
static function ( $filename ) use ( $testClassRegex, $suitesPath ) {
|
|
// Remove testUnitTestFileNamesEndWithTest false positives
|
|
if ( str_starts_with( $filename, $suitesPath ) ||
|
|
str_ends_with( $filename, 'Test.php' )
|
|
) {
|
|
return false;
|
|
}
|
|
$contents = file_get_contents( $filename );
|
|
return preg_match( $testClassRegex, $contents );
|
|
}
|
|
);
|
|
$strip = strlen( $rootPath ) + 1;
|
|
foreach ( $results as $k => $v ) {
|
|
$results[$k] = substr( $v, $strip );
|
|
}
|
|
|
|
// Normalize indexes to make failure output less confusing
|
|
$results = array_values( $results );
|
|
|
|
$this->assertEquals(
|
|
[],
|
|
$results,
|
|
"Unit test file in $rootPath must end with Test."
|
|
);
|
|
}
|
|
|
|
private function recurseFiles( $dir ) {
|
|
return ( new Facade() )->getFilesAsArray( $dir, [ '.php' ] );
|
|
}
|
|
}
|