Skip to content

Commit

Permalink
Merge pull request #257 from JeroenDeDauw/CamelTest
Browse files Browse the repository at this point in the history
Add option to allow having an undercore in test methods
  • Loading branch information
Manuel Pichler committed Sep 21, 2015
2 parents 8227ae2 + 3cbe76d commit af47957
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/main/php/PHPMD/Rule/Controversial/CamelCaseMethodName.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,7 @@ public function apply(AbstractNode $node)
{
$methodName = $node->getName();
if (!in_array($methodName, $this->ignoredMethods)) {
$pattern = '/^[a-z][a-zA-Z0-9]*$/';
$allowUnderscore = $this->getBooleanProperty('allow-underscore');

if ($allowUnderscore == true) {
$pattern = '/^[_]?[a-z][a-zA-Z0-9]*$/';
}

if (!preg_match($pattern, $methodName)) {
if (!$this->isValid($methodName)) {
$this->addViolation(
$node,
array(
Expand All @@ -101,4 +94,18 @@ public function apply(AbstractNode $node)
}
}
}

private function isValid($methodName)
{
if ($this->getBooleanProperty('allow-underscore-test') && strpos($methodName, 'test') === 0) {
return preg_match('/^test[a-zA-Z0-9]*([_][a-z][a-zA-Z0-9]*)?$/', $methodName);
}

if ($this->getBooleanProperty('allow-underscore')) {
return preg_match('/^[_]?[a-z][a-zA-Z0-9]*$/', $methodName);
}

return preg_match('/^[a-z][a-zA-Z0-9]*$/', $methodName);
}

}
77 changes: 77 additions & 0 deletions src/test/php/PHPMD/Rule/Controversial/CamelCaseMethodNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function testRuleDoesNotApplyForValidMethodName()
$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'false');
$rule->apply($this->getMethod());
}

Expand All @@ -90,6 +91,7 @@ public function testRuleDoesApplyForMethodNameWithCapital()
$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'false');
$rule->apply($method);
}

Expand All @@ -108,6 +110,7 @@ public function testRuleDoesApplyForMethodNameWithUnderscores()
$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'false');
$rule->apply($method);
}

Expand All @@ -125,6 +128,7 @@ public function testRuleDoesApplyForValidMethodNameWithUnderscoreWhenNotAllowed(
$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'false');
$rule->apply($method);
}

Expand All @@ -142,6 +146,79 @@ public function testRuleDoesNotApplyForValidMethodNameWithUnderscoreWhenAllowed(
$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'true');
$rule->addProperty('allow-underscore-test', 'false');
$rule->apply($method);
}

/**
* Tests that the rule does apply for a valid test method name
* with an underscore.
*
* @return void
*/
public function testRuleDoesApplyForTestMethodWithUnderscoreWhenNotAllowed()
{
$method = $this->getMethod();
$report = $this->getReportMock(1);

$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'false');
$rule->apply($method);
}

/**
* Tests that the rule does not apply for a valid test method name
* with an underscore when an single underscore is allowed.
*
* @return void
*/
public function testRuleDoesNotApplyForTestMethodWithUnderscoreWhenAllowed()
{
$method = $this->getMethod();
$report = $this->getReportMock(0);

$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'true');
$rule->apply($method);
}

/**
* Tests that the rule does apply for a test method name
* with multiple underscores even when one is allowed.
*
* @return void
*/
public function testRuleAppliesToTestMethodWithTwoUnderscoresEvenWhenOneIsAllowed()
{
$method = $this->getMethod();
$report = $this->getReportMock(1);

$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'true');
$rule->apply($method);
}

/**
* Tests that the rule does apply to for test method names that
* have a capital after their single allowed underscore.
*
* @return void
*/
public function testRuleAppliesToTestMethodWithUnderscoreFollowedByCapital()
{
$method = $this->getMethod();
$report = $this->getReportMock(1);

$rule = new CamelCaseMethodName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->addProperty('allow-underscore-test', 'true');
$rule->apply($method);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
class testRuleAppliesToTestMethodWithTwoUnderscoresEvenWhenOneIsAllowed
{
public function testGivenSomeValue_expectSome_niceResult()
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
class testRuleAppliesToTestMethodWithUnderscoreFollowedByCapital
{
public function testGivenSomeValue_ExpectSomeResult()
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
class testRuleDoesApplyForTestMethodWithUnderscoreWhenNotAllowed
{
public function testGivenSomeValue_expectSomeResult()
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
class testRuleDoesNotApplyForTestMethodWithUnderscoreWhenAllowed
{
public function testGivenSomeValue_expectSomeResult()
{

}
}

0 comments on commit af47957

Please sign in to comment.