|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CodeMessDetector\Test\Unit\Rule\UnusedCode; |
| 9 | + |
| 10 | +use Magento\CodeMessDetector\Rule\UnusedCode\UnusedFormalParameter; |
| 11 | +use PHPMD\Node\ASTNode; |
| 12 | +use PHPMD\Node\MethodNode; |
| 13 | +use PHPMD\Report; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject as MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 19 | + */ |
| 20 | +class UnusedFormalParameterTest extends TestCase |
| 21 | +{ |
| 22 | + private const FAKE_PLUGIN_NAMESPACE = 'Magento\CodeMessDetector\Test\UnusedCode\Plugin'; |
| 23 | + private const FAKE_NAMESPACE = 'Magento\CodeMessDetector\Test\UnusedCode'; |
| 24 | + |
| 25 | + /** |
| 26 | + * |
| 27 | + * @dataProvider getCases |
| 28 | + */ |
| 29 | + public function testApply($methodName, $methodParams, $namespace, $expectViolation) |
| 30 | + { |
| 31 | + $node = $this->createMethodNodeMock($methodName, $methodParams, $namespace); |
| 32 | + $rule = new UnusedFormalParameter(); |
| 33 | + $this->expectsRuleViolation($rule, $expectViolation); |
| 34 | + $rule->apply($node); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Prepare method node mock |
| 39 | + * |
| 40 | + * @param $methodName |
| 41 | + * @param $methodParams |
| 42 | + * @param $namespace |
| 43 | + * @return MethodNode|MockObject |
| 44 | + */ |
| 45 | + private function createMethodNodeMock($methodName, $methodParams, $namespace) |
| 46 | + { |
| 47 | + $methodNode = $this->createConfiguredMock( |
| 48 | + MethodNode::class, |
| 49 | + [ |
| 50 | + 'getName' => $methodName, |
| 51 | + 'getImage' => $methodName, |
| 52 | + 'isAbstract' => false, |
| 53 | + 'isDeclaration' => true |
| 54 | + ] |
| 55 | + ); |
| 56 | + |
| 57 | + /*$methodNode->expects($this->once()) |
| 58 | + ->method('getDocComment') |
| 59 | + ->willReturn('');*/ |
| 60 | + |
| 61 | + $variableDeclarators = []; |
| 62 | + foreach ($methodParams as $methodParam) { |
| 63 | + $variableDeclarator = $this->createASTNodeMock(); |
| 64 | + $variableDeclarator->method('getImage') |
| 65 | + ->willReturn($methodParam); |
| 66 | + |
| 67 | + $variableDeclarators[] = $variableDeclarator; |
| 68 | + } |
| 69 | + $parametersMock = $this->createASTNodeMock(); |
| 70 | + $parametersMock->expects($this->once()) |
| 71 | + ->method('findChildrenOfType') |
| 72 | + ->with('VariableDeclarator') |
| 73 | + ->willReturn($variableDeclarators); |
| 74 | + |
| 75 | + /** |
| 76 | + * Declare mock result for findChildrenOfType |
| 77 | + * with Dummy for removeCompoundVariables and removeVariablesUsedByFuncGetArgs |
| 78 | + */ |
| 79 | + $methodNode->expects($this->atLeastOnce()) |
| 80 | + ->method('findChildrenOfType') |
| 81 | + ->withConsecutive(['FormalParameters'], ['CompoundVariable'], ['FunctionPostfix']) |
| 82 | + ->willReturnOnConsecutiveCalls([$parametersMock], [], []); |
| 83 | + |
| 84 | + // Dummy result for removeRegularVariables |
| 85 | + $methodNode->expects($this->once()) |
| 86 | + ->method('findChildrenOfTypeVariable') |
| 87 | + ->willReturn([]); |
| 88 | + |
| 89 | + $classNode = $this->createASTNodeMock(); |
| 90 | + $classNode->expects($this->once()) |
| 91 | + ->method('getNamespaceName') |
| 92 | + ->willReturn($namespace); |
| 93 | + $methodNode->expects($this->once()) |
| 94 | + ->method('getParentType') |
| 95 | + ->willReturn($classNode); |
| 96 | + |
| 97 | + return $methodNode; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Create ASTNode mock |
| 102 | + * |
| 103 | + * @return ASTNode|MockObject |
| 104 | + */ |
| 105 | + private function createASTNodeMock() |
| 106 | + { |
| 107 | + return $this->createMock(ASTNode::class); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param UnusedFormalParameter $rule |
| 112 | + * @param bool $expects |
| 113 | + */ |
| 114 | + private function expectsRuleViolation(UnusedFormalParameter $rule, bool $expects) |
| 115 | + { |
| 116 | + /** @var Report|MockObject $reportMock */ |
| 117 | + $reportMock = $this->createMock(Report::class); |
| 118 | + if ($expects) { |
| 119 | + $violationExpectation = $this->atLeastOnce(); |
| 120 | + } else { |
| 121 | + $violationExpectation = $this->never(); |
| 122 | + } |
| 123 | + $reportMock->expects($violationExpectation) |
| 124 | + ->method('addRuleViolation'); |
| 125 | + $rule->setReport($reportMock); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @return array |
| 130 | + */ |
| 131 | + public function getCases(): array |
| 132 | + { |
| 133 | + return [ |
| 134 | + // Plugin methods |
| 135 | + [ |
| 136 | + 'beforePluginMethod', |
| 137 | + [ |
| 138 | + 'subject' |
| 139 | + ], |
| 140 | + self::FAKE_PLUGIN_NAMESPACE, |
| 141 | + false |
| 142 | + ], |
| 143 | + [ |
| 144 | + 'aroundPluginMethod', |
| 145 | + [ |
| 146 | + 'subject', |
| 147 | + 'proceed' |
| 148 | + ], |
| 149 | + self::FAKE_PLUGIN_NAMESPACE, |
| 150 | + false |
| 151 | + ], |
| 152 | + [ |
| 153 | + 'aroundPluginMethod', |
| 154 | + [ |
| 155 | + 'subject', |
| 156 | + 'result' |
| 157 | + ], |
| 158 | + self::FAKE_PLUGIN_NAMESPACE, |
| 159 | + false |
| 160 | + ], |
| 161 | + // Plugin method that contain unused parameter |
| 162 | + [ |
| 163 | + 'someMethod', |
| 164 | + [ |
| 165 | + 'unusedParameter' |
| 166 | + ], |
| 167 | + self::FAKE_PLUGIN_NAMESPACE, |
| 168 | + true |
| 169 | + ], |
| 170 | + // Non plugin method |
| 171 | + [ |
| 172 | + 'someMethod', |
| 173 | + [ |
| 174 | + 'subject', |
| 175 | + 'result' |
| 176 | + ], |
| 177 | + self::FAKE_NAMESPACE, |
| 178 | + true |
| 179 | + ] |
| 180 | + ]; |
| 181 | + } |
| 182 | +} |
0 commit comments