Skip to content

Commit ded1983

Browse files
committed
Added test case
1 parent b8cc523 commit ded1983

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

src/com/magento/idea/magento2plugin/reference/provider/EventNameReferenceProvider.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,49 @@ public class EventNameReferenceProvider extends PsiReferenceProvider {
2828

2929
@NotNull
3030
@Override
31-
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
32-
String value = StringUtil.unquoteString(element.getText());
33-
Collection<VirtualFile> containingFiles = FileBasedIndex.getInstance()
31+
public PsiReference[] getReferencesByElement(
32+
@NotNull final PsiElement element,
33+
@NotNull final ProcessingContext context
34+
) {
35+
final String value = StringUtil.unquoteString(element.getText());
36+
final List<PsiReference> psiReferences = new ArrayList<>();
37+
final Collection<VirtualFile> containingFiles = FileBasedIndex.getInstance()
3438
.getContainingFiles(EventNameIndex.KEY, value,
3539
GlobalSearchScope.getScopeRestrictedByFileTypes(
3640
GlobalSearchScope.allScope(element.getProject()),
3741
PhpFileType.INSTANCE
3842
)
3943
);
4044

41-
PsiManager psiManager = PsiManager.getInstance(element.getProject());
42-
for (VirtualFile virtualFile: containingFiles) {
43-
PhpFile phpFile = (PhpFile) psiManager.findFile(virtualFile);
45+
final PsiManager psiManager = PsiManager.getInstance(element.getProject());
46+
final List<PsiElement> psiElements = new ArrayList<>();
47+
for (final VirtualFile virtualFile: containingFiles) {
48+
final PhpFile phpFile = (PhpFile) psiManager.findFile(virtualFile);
4449
if (phpFile != null) {
45-
List<PsiElement> psiElements = new ArrayList<>();
4650
recursiveFill(psiElements, phpFile, value);
47-
if (psiElements.size() > 0) {
48-
return new PsiReference[] {new PolyVariantReferenceBase(element, psiElements)};
51+
if (!psiElements.isEmpty()) {
52+
psiReferences.add(new PolyVariantReferenceBase(element, psiElements));
53+
break;
4954
}
5055
}
5156
}
52-
return PsiReference.EMPTY_ARRAY;
57+
58+
return psiReferences.toArray(new PsiReference[0]);
5359
}
5460

55-
private void recursiveFill(List<PsiElement> psiElements, PsiElement psiElement, String typeName) {
61+
private void recursiveFill(
62+
final List<PsiElement> psiElements,
63+
final PsiElement psiElement,
64+
final String typeName
65+
) {
5666
if (PhpPatternsHelper.STRING_METHOD_ARGUMENT.accepts(psiElement)) {
5767
if (StringUtil.unquoteString(psiElement.getText()).equals(typeName)) {
5868
psiElements.add(psiElement);
5969
}
6070
return;
6171
}
6272

63-
for (PsiElement child: psiElement.getChildren()) {
73+
for (final PsiElement child: psiElement.getChildren()) {
6474
recursiveFill(psiElements, child, typeName);
6575
}
6676
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
10+
<event name="invalid_event<caret>">
11+
<observer name="test_observer" instance="Magento\Catalog\Observer\TestObserver" />
12+
</event>
13+
</config>

tests/com/magento/idea/magento2plugin/reference/BaseReferenceTestCase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
2424
import org.jetbrains.annotations.Nullable;
2525

26+
@SuppressWarnings({
27+
"PMD.TooManyMethods",
28+
})
2629
public abstract class BaseReferenceTestCase extends BaseInspectionsTestCase {
2730
private static final String testDataFolderPath = "testData" + File.separator//NOPMD
2831
+ "reference" + File.separator;

tests/com/magento/idea/magento2plugin/reference/xml/ObserverReferenceRegistrarTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class ObserverReferenceRegistrarTest extends ReferenceXmlFixtureTestCase {
1111

1212
/**
13-
* Tests for observer instance reference in events.xml
13+
* Tests for observer instance reference in events.xml.
1414
*/
1515
public void testObserverInstanceMustHaveReference() {
1616
myFixture.configureByFile(this.getFixturePath(ModuleEventsXml.FILE_NAME));
@@ -19,11 +19,20 @@ public void testObserverInstanceMustHaveReference() {
1919
}
2020

2121
/**
22-
* Tests for event name reference in events.xml
22+
* Tests for event name reference in events.xml.
2323
*/
2424
public void testEventNameMustHaveReference() {
2525
myFixture.configureByFile(this.getFixturePath(ModuleEventsXml.FILE_NAME));
2626

2727
assertHasReferenceToMethodArgument("test_event_in_block");
2828
}
29+
30+
/**
31+
* Tests for no event name reference in events.xml.
32+
*/
33+
public void testEventNameMustNotHaveReference() {
34+
myFixture.configureByFile(this.getFixturePath(ModuleEventsXml.FILE_NAME));
35+
36+
assertEmptyReference();
37+
}
2938
}

0 commit comments

Comments
 (0)