Skip to content

Commit

Permalink
WP_UnitTestCase_Base: introduce a new assertion and a new helper method
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Sep 18, 2024
1 parent 1324176 commit 0e965d9
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/phpunit/includes/abstract-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,40 @@ public function assertNonEmptyMultidimensionalArray( $actual, $message = '' ) {
}
}

/**
* 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.
*
Expand Down

0 comments on commit 0e965d9

Please sign in to comment.