-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Standards/AbstractSniffUnitTest: order test files using natural sort #3775
Conversation
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorting 'naturally' makes sense over sorting strictly alphabetically, however I do wonder if we should be using shuffle($testFiles);
here instead.
No, we should not. See my comment about property reset testing above. |
Yes, I read that before reviewing the code. What I'm wondering about is if those tests should be rewritten so that the order in which they are run no longer matters. Anyway, this pull request seems like a good change as-is (hence the approval above). |
That's not possible/desirable. To give you a little more context/explain this a little more: Take for instance the Another example would be a sniff examining code which may behave differently depending on a
Now, you may say that sniffs should use the first or second way of sniffing, but depending on how large the files being examined are and how often the situation the sniff is looking for occurs, this can severely impact the runtime of PHPCS. I'm not talking seconds, this can make minutes of difference. Does that make things a little clearer ? |
Thanks very much for taking the time to explain this. Yes, this makes sense and helps me understand why it's important to keep the test files in a predefined order. The code comment simply says "Put them in order." which doesn't give any of the context that you've provided here. Both of these two test-cases make sense to me. I agree that having the test files in a predefined order is the most sensible approach here. |
Closing as replaced by PHPCSStandards/PHP_CodeSniffer#55 |
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 ( file11
would be run before file2
- 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.