Skip to content

Commit 75e97d0

Browse files
committed
added test coverage
1 parent 1742009 commit 75e97d0

File tree

7 files changed

+84
-10
lines changed

7 files changed

+84
-10
lines changed

resources/magento2/inspection.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ inspection.warning.method.does.not.exist=The method "{0}" does not exist in the
4040
inspection.warning.method.should.have.public.access=The method "{0}" should have public access
4141
inspection.warning.method.should.have.public.access.fix=Change the method access
4242
inspection.displayName.ModuleScopeInspection=Module Configuration Scope Inspection
43-
inspection.config.wrong.area = The config file area is wrong. Please check the spelling of the parent directory, it should be equal to the following: adminhtml, frontend, crontab, webapi_rest, webapi_soap, graphql.
43+
inspection.config.wrong.area = The area of this config file is wrong. Please check the spelling of the parent directory, it should be equal to one of the following: adminhtml, frontend, crontab, webapi_rest, webapi_soap, graphql.

src/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspection.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@
77

88
import com.intellij.codeInspection.ProblemHighlightType;
99
import com.intellij.codeInspection.ProblemsHolder;
10+
import com.intellij.codeInspection.XmlSuppressableInspectionTool;
1011
import com.intellij.openapi.vfs.VirtualFile;
11-
import com.intellij.psi.*;
12+
import com.intellij.psi.PsiDirectory;
13+
import com.intellij.psi.PsiElementVisitor;
14+
import com.intellij.psi.PsiFile;
15+
import com.intellij.psi.XmlElementVisitor;
1216
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
13-
import com.magento.idea.magento2plugin.magento.files.ModuleEventsXml;
1417
import com.magento.idea.magento2plugin.magento.packages.Areas;
15-
import com.jetbrains.php.lang.inspections.PhpInspection;
1618
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
1719
import com.magento.idea.magento2plugin.magento.packages.Package;
1820
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
1921
import org.jetbrains.annotations.NotNull;
2022
import java.util.*;
2123

22-
public class ModuleScopeInspection extends PhpInspection {
24+
public class ModuleScopeInspection extends XmlSuppressableInspectionTool {
2325

2426
@NotNull
2527
@SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops", "PMD.CognitiveComplexity"})
@@ -34,10 +36,6 @@ public PsiElementVisitor buildVisitor(
3436

3537
@Override
3638
public void visitFile(final @NotNull PsiFile file) {
37-
if (!ModuleEventsXml.FILE_NAME.equals(file.getName())) {
38-
return;
39-
}
40-
4139
final PsiDirectory targetDirectory = file.getParent();
4240
if (targetDirectory == null) {
4341
return;
@@ -61,7 +59,7 @@ public void visitFile(final @NotNull PsiFile file) {
6159

6260
final String directoryName = targetDirectory.getName();
6361
final Areas area = Areas.getAreaByString(targetDirectory.getName());
64-
if (area == null || directoryName.equals(Areas.base)) {
62+
if (area == null || directoryName.equals(Areas.base.toString())) {
6563
problemsHolder.registerProblem(
6664
file,
6765
inspectionBundle.message(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<warning descr="The area of this config file is wrong. Please check the spelling of the parent directory, it should be equal to one of the following: adminhtml, frontend, crontab, webapi_rest, webapi_soap, graphql." />
2+
<?xml version="1.0"?>
3+
<!--
4+
/*
5+
* Copyright © Magento, Inc. All rights reserved.
6+
* See COPYING.txt for license details.
7+
*/
8+
-->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use Magento\Framework\Component\ComponentRegistrar;
4+
5+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Test_TestModule', __DIR__);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<warning descr="The area of this config file is wrong. Please check the spelling of the parent directory, it should be equal to one of the following: adminhtml, frontend, crontab, webapi_rest, webapi_soap, graphql." />
2+
<?xml version="1.0"?>
3+
<!--
4+
/*
5+
* Copyright © Magento, Inc. All rights reserved.
6+
* See COPYING.txt for license details.
7+
*/
8+
-->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use Magento\Framework\Component\ComponentRegistrar;
4+
5+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Test_TestModule', __DIR__);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.inspections.xml;
7+
8+
public class ModuleScopeInspectionTest extends InspectionXmlFixtureTestCase {
9+
10+
private static final String WRONG_AREA =
11+
"inspection.config.wrong.area";
12+
13+
@Override
14+
public void setUp() throws Exception {
15+
super.setUp();
16+
myFixture.enableInspections(ModuleScopeInspection.class);
17+
}
18+
19+
/**
20+
* Inspection highlights warning if the area of a config file is wrong
21+
*/
22+
public void testIncorrectArea() {
23+
configureFixture("app/code/Test/TestModule/etc/adminhtmltypo/di.xml");
24+
25+
final String errorMessage = inspectionBundle.message(
26+
WRONG_AREA
27+
);
28+
29+
assertHasHighlighting(errorMessage);
30+
}
31+
32+
/**
33+
* Inspection skips warning if the area is correct.
34+
*/
35+
public void testCorrectArea() {
36+
configureFixture("app/code/Test/TestModule/etc/adminhtml/di.xml");
37+
38+
final String errorMessage = inspectionBundle.message(
39+
WRONG_AREA
40+
41+
);
42+
43+
assertHasNoHighlighting(errorMessage);
44+
}
45+
46+
private void configureFixture(String fixturePath) {
47+
myFixture.copyFileToProject(getFixturePath("app/code/Test/TestModule/registration.php"));
48+
myFixture.configureByFile(getFixturePath(fixturePath));
49+
}
50+
}

0 commit comments

Comments
 (0)