Skip to content

Commit

Permalink
Covered the new allow-unused-foreach-variables setting in UnusedLocal…
Browse files Browse the repository at this point in the history
…Variable with tests
  • Loading branch information
ravage84 committed Oct 8, 2014
1 parent 755fbd8 commit 98bed5a
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/test/php/PHPMD/Rule/UnusedLocalVariableTest.php
Expand Up @@ -70,6 +70,7 @@ class UnusedLocalVariableTest extends AbstractTest
public function testRuleAppliesToUnusedLocalVariable()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(1));
$rule->apply($this->getMethod());
}
Expand All @@ -82,6 +83,7 @@ public function testRuleAppliesToUnusedLocalVariable()
public function testInnerFunctionParametersDoNotHideUnusedVariables()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(1));
$rule->apply($this->getFunction());
}
Expand All @@ -104,6 +106,7 @@ public function testInnerFunctionParametersDoNotHideUnusedVariables()
public function testRuleAppliesToLocalVariableWithSameNameAsStaticProperty()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(1));
$rule->apply($this->getMethod());
}
Expand All @@ -126,6 +129,7 @@ public function testRuleAppliesToLocalVariableWithSameNameAsStaticProperty()
public function testRuleAppliesToLocalVariableWithSameNameAsStaticArrayProperty()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(1));
$rule->apply($this->getMethod());
}
Expand All @@ -149,6 +153,7 @@ public function testRuleAppliesToLocalVariableWithSameNameAsStaticArrayProperty(
public function testRuleDoesNotApplyToLocalVariableUsedInCompoundVariable()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -161,6 +166,7 @@ public function testRuleDoesNotApplyToLocalVariableUsedInCompoundVariable()
public function testRuleDoesNotApplyToThisVariable()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -173,6 +179,7 @@ public function testRuleDoesNotApplyToThisVariable()
public function testRuleDoesNotApplyToStaticProperty()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -185,6 +192,7 @@ public function testRuleDoesNotApplyToStaticProperty()
public function testRuleDoesNotApplyToStaticArrayProperty()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -206,6 +214,7 @@ public function testRuleDoesNotApplyToStaticArrayProperty()
public function testRuleDoesNotApplyToMethodArgument()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -218,6 +227,7 @@ public function testRuleDoesNotApplyToMethodArgument()
public function testRuleDoesNotApplyToStaticObjectProperty()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -230,6 +240,7 @@ public function testRuleDoesNotApplyToStaticObjectProperty()
public function testRuleDoesNotApplyToDynamicProperty()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -242,6 +253,7 @@ public function testRuleDoesNotApplyToDynamicProperty()
public function testRuleDoesNotApplyToUnusedParameters()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -254,6 +266,7 @@ public function testRuleDoesNotApplyToUnusedParameters()
public function testRuleDoesNotApplyToArgcSuperGlobal()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -266,6 +279,7 @@ public function testRuleDoesNotApplyToArgcSuperGlobal()
public function testRuleDoesNotApplyToArgvSuperGlobal()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -278,6 +292,7 @@ public function testRuleDoesNotApplyToArgvSuperGlobal()
public function testRuleDoesNotApplyToGlobalsSuperGlobal()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -290,6 +305,7 @@ public function testRuleDoesNotApplyToGlobalsSuperGlobal()
public function testRuleDoesNotApplyToCookieSuperGlobal()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -302,6 +318,7 @@ public function testRuleDoesNotApplyToCookieSuperGlobal()
public function testRuleDoesNotApplyToEnvSuperGlobal()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -314,6 +331,7 @@ public function testRuleDoesNotApplyToEnvSuperGlobal()
public function testRuleDoesNotApplyToFilesSuperGlobal()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -326,6 +344,7 @@ public function testRuleDoesNotApplyToFilesSuperGlobal()
public function testRuleDoesNotApplyToGetSuperGlobal()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -338,6 +357,7 @@ public function testRuleDoesNotApplyToGetSuperGlobal()
public function testRuleDoesNotApplyToPostSuperGlobal()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -350,6 +370,7 @@ public function testRuleDoesNotApplyToPostSuperGlobal()
public function testRuleDoesNotApplyToRequestSuperGlobal()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -362,6 +383,7 @@ public function testRuleDoesNotApplyToRequestSuperGlobal()
public function testRuleDoesNotApplyToSessionSuperGlobal()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -374,6 +396,7 @@ public function testRuleDoesNotApplyToSessionSuperGlobal()
public function testRuleDoesNotApplyToServerSuperGlobal()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -386,6 +409,7 @@ public function testRuleDoesNotApplyToServerSuperGlobal()
public function testRuleDoesNotApplyToHttpRawPostDataSuperGlobal()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -398,6 +422,7 @@ public function testRuleDoesNotApplyToHttpRawPostDataSuperGlobal()
public function testRuleDoesNotApplyToUnusedLocalVariableInFunction()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getFunction());
}
Expand All @@ -410,6 +435,7 @@ public function testRuleDoesNotApplyToUnusedLocalVariableInFunction()
public function testRuleDoesNotApplyToUnusedLocalVariableInMethod()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -432,10 +458,66 @@ public function testRuleDoesNotApplyToUnusedLocalVariableInMethod()
public function testRuleDoesNotApplyToLocalVariableUsedAsArrayIndex()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}

/**
* testRuleAppliesToUnusedForeachKeyWhenNotIgnored
*
* @return void
*/
public function testRuleAppliesToUnusedForeachKeyWhenNotIgnored()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(1));
$rule->apply($this->getMethod());
}


/**
* testRuleAppliesToUnusedForeachValueWhenNotIgnored
*
* @return void
*/
public function testRuleAppliesToUnusedForeachValueWhenNotIgnored()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(1));
$rule->apply($this->getMethod());
}


/**
* testRuleDoesNotApplyToUnusedForeachKeyWhenIgnored
*
* @return void
*/
public function testRuleDoesNotApplyToUnusedForeachKeyWhenIgnored()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'true');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}


/**
* testRuleDoesNotApplyToUnusedForeachValueWhenIgnored
*
* @return void
*/
public function testRuleDoesNotApplyToUnusedForeachValueWhenIgnored()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'true');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}

/**
* testRuleDoesNotApplyToLocalVariableUsedAsStringIndex
*
Expand All @@ -454,6 +536,7 @@ public function testRuleDoesNotApplyToLocalVariableUsedAsArrayIndex()
public function testRuleDoesNotApplyToLocalVariableUsedAsStringIndex()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -476,6 +559,7 @@ public function testRuleDoesNotApplyToLocalVariableUsedAsStringIndex()
public function testRuleDoesNotApplyToCatchStatement()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -497,6 +581,7 @@ public function testRuleDoesNotApplyToCatchStatement()
public function testRuleDoesNotApplyToCompactFunction()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -509,6 +594,7 @@ public function testRuleDoesNotApplyToCompactFunction()
public function test_compact_function_rule_works_case_insensitive()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -532,6 +618,7 @@ public function test_compact_function_rule_works_case_insensitive()
public function testRuleDoesNotApplyToNamespacedCompactFunction()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -544,6 +631,7 @@ public function testRuleDoesNotApplyToNamespacedCompactFunction()
public function test_namespaced_compact_function_rule_works_case_insensitive()
{
$rule = new UnusedLocalVariable();
$rule->addProperty('allow-unused-foreach-variables', 'false');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand Down
@@ -0,0 +1,10 @@
<?php
class testRuleAppliesToUnusedForeachKeyWhenNotIgnored
{
public function testRuleAppliesToUnusedForeachKeyWhenNotIgnored()
{
foreach ($this->index as $key => $value) {
self::$string{$value} = 'a';
}
}
}
@@ -0,0 +1,10 @@
<?php
class testRuleAppliesToUnusedForeachValueWhenNotIgnored
{
public function testRuleAppliesToUnusedForeachValueWhenNotIgnored()
{
foreach ($this->index as $key => $value) {
self::$string{$key} = 'a';
}
}
}
@@ -0,0 +1,10 @@
<?php
class testRuleDoesNotApplyToUnusedForeachKeyWhenIgnored
{
public function testRuleDoesNotApplyToUnusedForeachKeyWhenIgnored()
{
foreach ($this->index as $key => $value) {
self::$string{$value} = 'a';
}
}
}
@@ -0,0 +1,10 @@
<?php
class testRuleDoesNotApplyToUnusedForeachValueWhenIgnored
{
public function testRuleDoesNotApplyToUnusedForeachValueWhenIgnored()
{
foreach ($this->index as $key => $value) {
self::$string{$key} = 'a';
}
}
}

0 comments on commit 98bed5a

Please sign in to comment.