Skip to content
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

Add PhpUnitMethodCasingFixer #3886

Merged
merged 1 commit into from Aug 22, 2018

Conversation

Slamdunk
Copy link
Contributor

@Slamdunk Slamdunk commented Jul 6, 2018

Closes #3302

 class MyTest extends \PhpUnit\FrameWork\TestCase
 {
-    public function test_my_code() {}
+    public function testMyCode() {}
 }
  • Implement base fixer
  • Take care of @depends
  • Run after PhpUnitTestAnnotationFixer
  • Deprecate PhpUnitTestAnnotationFixer case config

@Slamdunk Slamdunk changed the title Add PhpUnitMethodCasingFixer [WIP] Add PhpUnitMethodCasingFixer Jul 6, 2018
@Slamdunk Slamdunk changed the title [WIP] Add PhpUnitMethodCasingFixer Add PhpUnitMethodCasingFixer Jul 6, 2018
@@ -108,6 +108,7 @@ protected function createConfigurationDefinition()
(new FixerOptionBuilder('case', 'Whether to camel or snake case when adding the test prefix'))
->setAllowedValues(['camel', 'snake'])
->setDefault('camel')
->setDeprecationMessage('Use `php_unit_method_casing` fixer instead.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can php_unit_method_casing do same thing as this option ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, theoretically no because someone could want to transform /** @test */ function fooBar in function test_fooBar and only with this option you can do this.
But does this example make sense?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then, if deprecated feature is not fully represented by new feature, the difference shall be mentioned as addition to "use X instead"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds right, but I have no idea what could I wrote more to explain the intentions.
If someone used to use case option for Annotation fixer, the only possibility that comes into my mind is that the developer is taking care of method casing in PHPUnit classes.
The name of the new fixer is self explanatory, as this is the exact purpose.

Copy link
Member

@keradus keradus Jul 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, actually, when I saw the new rule name, i thought it's about proper casing of assertsame or ExPeCtEdExCePtIoN...

if (self::CAMEL_CASE === $this->configuration['case']) {
$newFunctionName = $functionName;
$newFunctionName = str_replace('_', ' ', $newFunctionName);
$newFunctionName = ucwords($newFunctionName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ucwords takes delimiter as 2nd param, so you can put there _ and avoid remove str_replace above.


/**
* @param Tokens $tokens
* @param int$index
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like you have found a bug in phpdoc_align - shouldn't this line fail style checking?

return false;
}

// if the function name starts with test its a test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its -> it's or it is

if ($this->startsWith('test', $functionName)) {
return true;
}
// If the function doesn't have test in its name, and no doc block, its not a test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*/
private function startsWith($needle, $haystack)
{
$len = strlen($needle);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Personal) I'd drop $len here and put strlen directly in substr to have one-liner.

@keradus keradus added this to the 2.13.0 milestone Jul 8, 2018
@Slamdunk
Copy link
Contributor Author

Slamdunk commented Jul 9, 2018

@kubawerlos thank you very much for the review.
Just note that I stole almost 90% of the code from PhpUnitTestAnnotationFixer so all your feedback should be addressed there too, and I don't know if I should fix there too or open another PR.

@kubawerlos
Copy link
Contributor

@Slamdunk I think no need to fix it here - maybe in separate PR it's worth to give try to initialize this fixer in PhpUnitTestAnnotationFixer and proxy the fixing.

@keradus keradus added RTM Ready To Merge and removed RTM Ready To Merge labels Aug 22, 2018
@keradus
Copy link
Member

keradus commented Aug 22, 2018

Thank you @Slamdunk.

@keradus keradus merged commit b87f254 into PHP-CS-Fixer:master Aug 22, 2018
keradus added a commit that referenced this pull request Aug 22, 2018
This PR was squashed before being merged into the 2.13-dev branch (closes #3886).

Discussion
----------

Add PhpUnitMethodCasingFixer

Closes #3302
```diff
 class MyTest extends \PhpUnit\FrameWork\TestCase
 {
-    public function test_my_code() {}
+    public function testMyCode() {}
 }
```

- [x] Implement base fixer
- [x] Take care of `@depends`
- [x] Run after `PhpUnitTestAnnotationFixer`
- [x] Deprecate `PhpUnitTestAnnotationFixer` *case* config

Commits
-------

b87f254 Add PhpUnitMethodCasingFixer
@SpacePossum
Copy link
Contributor

this tripped https://travis-ci.org/FriendsOfPHP/PHP-CS-Fixer/jobs/419018045#L1863
should be an easy fix

@keradus
Copy link
Member

keradus commented Aug 22, 2018

i will handle it

@keradus
Copy link
Member

keradus commented Aug 22, 2018

ref #3979

keradus added a commit that referenced this pull request Aug 22, 2018
This PR was squashed before being merged into the 2.13-dev branch (closes #3979).

Discussion
----------

DX: enable php_unit_method_casing

Appendix for #3886

Commits
-------

3fff21a DX: enable php_unit_method_casing
@Slamdunk Slamdunk deleted the phpunit_method_casing branch April 17, 2019 06:41
@kaiwa
Copy link

kaiwa commented Mar 21, 2024

Thanks for your work @Slamdunk, exactly what I needed. Snake case in tests ftw ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Snake or camel casing for phpunit tests
5 participants