Skip to content

Commit

Permalink
Standards/AbstractSniffUnitTest: order test files using natural sort
Browse files Browse the repository at this point in the history
When a sniff has multiple test case files, the `AbstractSniffUnitTest::getTestFiles()` sorts the file names before passing them off to be run.

While in most cases, the order of the test case files should not really matter, but in rare cases, like when testing that a property is being reset correctly between files, it does.

As things were, the `sort()` function without flags was being used, making the file order unnatural to work with ( file `11` would be run before file `2` - see: https://3v4l.org/VPO3Z ).

As the `SORT_NATURAL` flag is available since PHP 5.4, adding that flag will resolve this and will ensure the test case files are run in their natural order.

Optionally, the flag could be combined with the `SORT_FLAG_CASE` flag to make the sort case-insensitive.
  • Loading branch information
jrfnl committed Dec 4, 2023
1 parent 11d5c66 commit d675c75
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ The file documents changes to the PHP_CodeSniffer project.
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- The -e (explain) command will now list sniffs in natural order
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Tests using the PHPCS native test framework with multiple test case files will now run the test case files in numeric order.
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch

### Removed
- Removed support for installing via PEAR
Expand Down
2 changes: 1 addition & 1 deletion tests/Standards/AbstractSniffUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function getTestFiles($testFileBase)
}

// Put them in order.
sort($testFiles);
sort($testFiles, SORT_NATURAL);

return $testFiles;

Expand Down

0 comments on commit d675c75

Please sign in to comment.