Skip to content

[Test] Unit test for LineLengthSniff #6

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 Jan 11, 2019 · 8 comments
Closed

[Test] Unit test for LineLengthSniff #6

lenaorobei opened this issue Jan 11, 2019 · 8 comments
Assignees
Labels
accepted New rule is accepted enhancement Improvements to existing rules Progress: good first issue Issues is easy to get started with unit test Unit test for the rule

Comments

@lenaorobei
Copy link
Contributor

lenaorobei commented Jan 11, 2019

Description

LineLengthUnitTest needs to be covered with unit test.

Acceptance Criteria

  1. Test is added to the Magento/Tests/Files directory and extend AbstractSniffUnitTest.
  2. Positive as well as negative scenarios are covered by unit test. In other words, LineLengthUnitTest.inc should contain "error" code as well as example of "good" code.

Additional information

See already implemented unit tests under Magento/Tests directory.
Check your code by running

$ bin/phpunit
@lenaorobei lenaorobei added enhancement Improvements to existing rules unit test Unit test for the rule labels Jan 11, 2019
@lenaorobei lenaorobei changed the title Unit Test for LineLengthSniff Unit test for LineLengthSniff Jan 11, 2019
@lenaorobei lenaorobei added the accepted New rule is accepted label Jan 14, 2019
@lenaorobei lenaorobei added the Progress: good first issue Issues is easy to get started with label Jan 29, 2019
@lenaorobei lenaorobei changed the title Unit test for LineLengthSniff [Test] Unit test for LineLengthSniff Jan 29, 2019
@mzeis
Copy link
Contributor

mzeis commented Feb 9, 2019

Hi! I'd like to work on this.

@mzeis
Copy link
Contributor

mzeis commented Feb 12, 2019

As discussed with @lenaorobei, we need to fix #36 so that the unit test can work as expected.

@mzeis
Copy link
Contributor

mzeis commented Mar 2, 2019

@lenaorobei With PR #50 (that fixes issue #36), we have a unit test covering the basic sniff.

While writing the test, I found that the code for ignoring "long lines in case they contain strings intended for translation" is broken. The reason is that there is a wrong assumption in \Magento\Sniffs\Files\LineLengthSniff which parameters are passed to the checkLineLength method.

If you compare the Magento method and the method that it is overwriting:

  • Magento\Sniffs\Files\LineLengthSniff::checkLineLength($phpcsFile, $stackPtr, $lineContent)
  • PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff::checkLineLength($phpcsFile, $tokens, $stackPtr)

you will notice that the Magento sniff expects the complete content of the line in the last parameter but only the stack pointer to the first token on the next line will be passed. To fix this check, one would have to implement the logic to assemble line and do the check as intended originally.

Should this be done as part of this issue or would you like to create another issue for this? In the latter case, we could either close #6 when #50 is merged or the new issue woulde be a blocker for #6.

@mzeis
Copy link
Contributor

mzeis commented Mar 3, 2019

I had some time today, so I went ahead and created an implementation in my fork (compare with develop branch).

This implementation should fix the functionality. However, I found that the regular expression that is in use doesn't always work. An error wouldn't be triggered if "Phrase('xy')" or "__('xy')" was used in another context, for example:

$test = "If a string exceeds 120 characters and a part of the string is Phrase('xy'), this would not be caught by the regular expression because it matches any line containing Phrase('xy') or __('xy').";

But then, the question is if fixing this would be within the scope of this issue as it might be more complex to fix this for all possible contexts.

@mzeis
Copy link
Contributor

mzeis commented Mar 12, 2019

@lenaorobei Sorry, I forgot to mention you here. Please could you check my last comment?

@lenaorobei
Copy link
Contributor Author

@mzeis thanks for the investigation. Could you please create separate issue for this use case?

@mzeis
Copy link
Contributor

mzeis commented Mar 24, 2019

@lenaorobei This is the issue for this use case: #75

lenaorobei added a commit that referenced this issue Mar 25, 2019
#6: Fix line length sniff when using translations
@lenaorobei
Copy link
Contributor Author

Done in #74.

mmansoor-magento pushed a commit that referenced this issue Dec 3, 2020
…coding-standard-200

[Imported] Version 6
fredden added a commit to fredden/magento-coding-standard that referenced this issue May 24, 2021
    Fatal error: Uncaught TypeError: vsprintf(): Argument magento#2 ($values) must be of type array, int given in /srv/www/vendor/squizlabs/php_codesniffer/src/Files/File.php:1056
    Stack trace:
    #0 /srv/www/vendor/squizlabs/php_codesniffer/src/Files/File.php(1056): vsprintf('Direct throw of...', 531)
    magento#1 /srv/www/vendor/squizlabs/php_codesniffer/src/Files/File.php(706): PHP_CodeSniffer\Files\File->addMessage(false, 'Direct throw of...', 67, 4, 'FoundDirectThro...', 531, 8, false)
    magento#2 /srv/www/vendor/magento/magento-coding-standard/Magento2/Sniffs/Exceptions/DirectThrowSniff.php(48): PHP_CodeSniffer\Files\File->addWarning('Direct throw of...', 526, 'FoundDirectThro...', 531)
    magento#3 /srv/www/vendor/squizlabs/php_codesniffer/src/Files/File.php(498): Magento2\Sniffs\Exceptions\DirectThrowSniff->process(Object(PHP_CodeSniffer\Files\LocalFile), 526)
    magento#4 /srv/www/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php(92): PHP_CodeSniffer\Files\File->process()
    #5 /srv/www/vendor/squizlabs/php_codesniffer/src/Runner.php(630): PHP_CodeSniffer\Files\LocalFile->process()
    magento#6 /srv/www/vendor/squizlabs/php_codesniffer/src/Runner.php(434): PHP_CodeSniffer\Runner->processFile(Object(PHP_CodeSniffer\Files\LocalFile))
    magento#7 /srv/www/vendor/squizlabs/php_codesniffer/src/Runner.php(114): PHP_CodeSniffer\Runner->run()
    magento#8 /srv/www/vendor/squizlabs/php_codesniffer/bin/phpcs(18): PHP_CodeSniffer\Runner->runPHPCS()
    magento#9 {main}
      thrown in /srv/www/vendor/squizlabs/php_codesniffer/src/Files/File.php on line 1056
fredden added a commit to fredden/magento-coding-standard that referenced this issue Sep 2, 2021
  PHP Fatal error:  Uncaught TypeError: strpos(): Argument magento#2 ($needle) must be of type string, null given in /srv/www/vendor/magento/magento-coding-standard/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php:629
  Stack trace:
  #0 /srv/www/vendor/magento/magento-coding-standard/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php(629): strpos('\\VendorName\\Mod...', NULL)
  magento#1 /srv/www/vendor/magento/magento-coding-standard/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php(515): Magento2\Sniffs\Annotation\MethodArgumentsSniff->validateFormattingConsistency(Array, Array, Object(PHP_CodeSniffer\Files\LocalFile), Array)
  magento#2 /srv/www/vendor/magento/magento-coding-standard/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php(600): Magento2\Sniffs\Annotation\MethodArgumentsSniff->validateMethodParameterAnnotations(385, Array, Array, Object(PHP_CodeSniffer\Files\LocalFile), Array, 356, 380)
  magento#3 /srv/www/vendor/squizlabs/php_codesniffer/src/Files/File.php(498): Magento2\Sniffs\Annotation\MethodArgumentsSniff->process(Object(PHP_CodeSniffer\Files\LocalFile), 385)
  magento#4 /srv/www/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php(92): PHP_CodeSniffer\Files\File->process()
  #5 /srv/www/vendor/squizlabs/php_codesniffer/src/Runner.php(630): PHP_CodeSniffer\Files\LocalFile->process()
  magento#6 /srv/www/vendor/squizlabs/php_codesniffer/src/Runner.php(434): PHP_CodeSniffer\Runner->processFile(Object(PHP_CodeSniffer\Files\LocalFile))
  magento#7 /srv/www/vendor/squizlabs/php_codesniffer/src/Runner.php(114): PHP_CodeSniffer\Runner->run()
  magento#8 /srv/www/vendor/squizlabs/php_codesniffer/bin/phpcs(18): PHP_CodeSniffer\Runner->runPHPCS()
  magento#9 {main}
    thrown in /srv/www/vendor/magento/magento-coding-standard/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php on line 629
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted New rule is accepted enhancement Improvements to existing rules Progress: good first issue Issues is easy to get started with unit test Unit test for the rule
Projects
None yet
Development

No branches or pull requests

2 participants