Skip to content

[Bug] Unit test for overridden properties in Magento.Files.LineLength doesn't work as expected #36

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

Closed
lenaorobei opened this issue Feb 11, 2019 · 2 comments
Assignees
Labels
accepted New rule is accepted bug Something isn't working

Comments

@lenaorobei
Copy link
Contributor

lenaorobei commented Feb 11, 2019

Rule from ruleset.xml

    <rule ref="Magento.Files.LineLength">
        <properties>
            <property name="lineLimit" value="120"/>
            <property name="absoluteLineLimit" value="120"/>
        </properties>
        <type>warning</type>
        <severity>6</severity>
    </rule>

Properties from parent Generic.Files.LineLength sniff are processed instead of changed in Magento.Files.LineLength values.

Steps to reproduce

  1. Create test fixture with line length = 101.

Expected result

  1. Unit test does not detect warning.

Actual result

[LINE 2] Expected 0 error(s) and 1 warning(s) in LineLengthUnitTest.inc but found 1 error(s) and 0 warning(s). The error(s) found were:
 -> Line exceeds maximum limit of 100 characters; contains 101 characters (Magento.Files.LineLength.MaxExceeded)

Discovered while implementing Unit test for LineLengthSniff.

@lenaorobei lenaorobei added bug Something isn't working accepted New rule is accepted labels Feb 11, 2019
@mzeis
Copy link
Contributor

mzeis commented Feb 24, 2019

I did some debugging.

When executing bin/phpcs --standard=Magento, the setting works as expected.

  • PHP_CodeSniffer\Runner initialises the ruleset and the processing of the Magento coding standard is triggered.
  • PHP_CodeSniffer\Ruleset::processRuleset() reads the complete ruleset.xml including the overridden property settings and the line limit is applied correctly.

When executing bin/phpunit, the code is executed differently.

  • The phpcs test suite bootstrap defines a constant PHP_CODESNIFFER_IN_TESTS.
  • PHP_CodeSniffer\Tests\Standards\AllSniffs::suite() gathers the tests for all installed, non-ignored coding standards and runs the tests.
  • When running the sniffs, the abstract sniff unit test class triggers the processing of the Magento coding standard.
  • But this time, the logic is different. When running the test suite, not the complete coding standard is interpreted but only the rules mentioned explicitly. Also, this logic is only executed for the first sniff of the coding standard.
  • I assume that the difference in the last step is the reason for this bug.

Do we know if this worked before for other sniffs / in other repositories?

@lenaorobei
Copy link
Contributor Author

@mzeis thanks for the investigation.
For me this CodeSniffer behaviour looks like expected. Since we override Generic.Files.LineLengthSniff we need to take care about its public properties. Unit tests cover not ruleset, but sniffs, so i believe the fix is to move property declaration from ruleset to the sniff itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted New rule is accepted bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants