-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
Generic/LowercasedFilename: improve code coverage #681
Generic/LowercasedFilename: improve code coverage #681
Conversation
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.
LGTM. Happy to accept this PR once the ignore annotation has been removed.
I have two "pro-tips" related to this PR to keep in mind for the future:
- When test case files can't comply with the naming conventions
For this PR only one extra file which couldn't comply with the test case file naming convention needed to be added. In which case, adding the file the way it is done in this PR is perfectly fine.
However, if you run across a sniff in the future, which needs multiple extra test case files which can't follow theSniffNameUnitTest.#.inc
test case file name convention, things can get messy.
So, my pro-tip for that case would be to create a sub-directory in the relevant test directory based on the sniff name and put all the additional test case files in that subdirectory.
In the test filegetTestFiles()
method, you can then use a call toglob()
to get a file list to return to the test runner.
Unfortunately, I couldn't find an example of such a setup in this GH org, but you can see an example of how this pattern can be applied in the WPCS standard for theirFileName
sniff.
Hope that helps. - Testing
STDIN
It is perfectly possible to test code handlingSTDIN
, so code blocks related to that should never be ignored for code coverage.
Yes, it does not fit within the "normal" testing framework for testing sniffs, but that doesn't mean, it can't be done.
What would be needed for this, is to introduce a new test method to the test file. This may not be common, but there is nothing technically blocking this and theDummyFile
class can be used to pass the input code to such tests.
The new test method would then look something like this:And such a test could even be set up with a data provider to test multiple different code snippets and expectations.use PHP_CodeSniffer\Files\DummyFile; use PHP_CodeSniffer\Ruleset; use PHP_CodeSniffer\Tests\ConfigDouble; /*...*/ /** * Test stdin. * * @return void */ public function testStdIn() { $config = new ConfigDouble(); $config->standards = ['Generic']; $config->sniffs = ['Generic.Files.LowercasedFilename']; $ruleset = new Ruleset($config); $content = '<?php '.PHP_EOL; $file = new DummyFile($content, $ruleset, $config); $file->process(); $this->assertSame(0, $file->getErrorCount()); $this->assertSame(0, $file->getWarningCount()); $this->assertCount(0, $file->getErrors()); }//end testStdIn()
For some examples of test methods using a similar pattern, have a look at theErrorSuppressionTest
.
Thanks. I will keep this in mind if I need to create tests with multiple files that don't follow the naming convention.
TIL! Thanks! As I mentioned in the inline comment, I removed the annotation and added the test that you suggested. This PR is ready for another review. |
I forgot to mention in the previous comment that when implementing the test, I removed |
All good, I just posted a rough example. Presumed you would customize it to what was needed. |
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.
Thanks @rodrigoprimo!
Includes adding a test for handling STDIN per suggestion during code review.
Description
This PR improves code coverage for the
Generic.Files.LowercasedFilename
sniff. I had to create a test file that does not follow the naming convention used by PHPCS to test how the sniff behaves when handling a file that only contains lowercase letters. To improve the readability of this file, I also used underscores.Related issues/external references
Part of #146
Issue found while working on this PR: #682
Types of changes
PR checklist