mirror of
git://develop.git.wordpress.org/
synced 2025-04-21 04:31:55 +02:00
Tests: Introduce assertion for comparing file paths independent of OS-specifics.
Introduces `WP_UnitTestCase_Base::assertSamePathIgnoringDirectorySeparators()` and an associated helper method `WP_UnitTestCase_Base::normalizeDirectorySeparatorsInPath()` to allow for comparing two file path strings independently of OS-specific differences. The normalization is done in a separate method to also allow this method to be used for path normalization within test methods themselves, like for normalizing a group of paths in an array. The pretty specific method name for the helper (`normalizeDirectorySeparatorsInPath()`) is an attempt to prevent naming conflicts with methods which may exist in plugin test suites build on top of the WP Core test suite. Props jrf, hellofromTonya. See #61530. git-svn-id: https://develop.svn.wordpress.org/trunk@59057 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
baff405508
commit
e6a8fdd754
@ -1070,6 +1070,40 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that two text strings representing file paths are the same, while ignoring
|
||||
* OS-specific differences in the directory separators.
|
||||
*
|
||||
* This allows for tests to be compatible for running on both *nix based as well as Windows OS.
|
||||
*
|
||||
* @since 6.7.0
|
||||
*
|
||||
* @param string $path_a File or directory path.
|
||||
* @param string $path_b File or directory path.
|
||||
*/
|
||||
public function assertSamePathIgnoringDirectorySeparators( $path_a, $path_b ) {
|
||||
$path_a = $this->normalizeDirectorySeparatorsInPath( $path_a );
|
||||
$path_b = $this->normalizeDirectorySeparatorsInPath( $path_b );
|
||||
|
||||
$this->assertSame( $path_a, $path_b );
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize directory separators in a file path to be a forward slash.
|
||||
*
|
||||
* @since 6.7.0
|
||||
*
|
||||
* @param string $path File or directory path.
|
||||
* @return string The normalized file or directory path.
|
||||
*/
|
||||
public function normalizeDirectorySeparatorsInPath( $path ) {
|
||||
if ( ! is_string( $path ) || PHP_OS_FAMILY !== 'Windows' ) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
return strtr( $path, '\\', '/' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks each of the WP_Query is_* functions/properties against expected boolean value.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user