Skip to content

Commit ad71264

Browse files
committed
Added test for PhpServiceMethodReferenceProvider
1 parent 1e3e171 commit ad71264

File tree

6 files changed

+117
-12
lines changed

6 files changed

+117
-12
lines changed

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.reference.provider;
67

78
import com.intellij.openapi.util.text.StringUtil;
@@ -24,22 +25,22 @@ public class PhpServiceMethodReferenceProvider extends PsiReferenceProvider {
2425

2526
@NotNull
2627
@Override
27-
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
28-
if (!(element instanceof XmlElement)) {
29-
return PsiReference.EMPTY_ARRAY;
30-
}
31-
32-
List<PsiReference> psiReferences = new ArrayList<>();
33-
34-
String methodName = StringUtil.unquoteString(element.getText());
28+
public PsiReference[] getReferencesByElement(
29+
@NotNull final PsiElement element,
30+
@NotNull final ProcessingContext context
31+
) {
32+
final List<PsiReference> psiReferences = new ArrayList<>();
33+
final String methodName = StringUtil.unquoteString(element.getText());
34+
final PhpClass phpClass = DiIndex.getPhpClassOfServiceMethod((XmlElement) element);
3535

36-
PhpClass phpClass = DiIndex.getPhpClassOfServiceMethod((XmlElement) element);
3736
if (phpClass != null) {
38-
Collection<Method> methods = phpClass.getMethods();
37+
final Collection<Method> methods = phpClass.getMethods();
3938
methods.removeIf(m -> !m.getName().equalsIgnoreCase(methodName));
40-
psiReferences.add(new PolyVariantReferenceBase(element, methods));
39+
if (!methods.isEmpty()) {
40+
psiReferences.add(new PolyVariantReferenceBase(element, methods));
41+
}
4142
}
4243

43-
return psiReferences.toArray(new PsiReference[psiReferences.size()]);
44+
return psiReferences.toArray(new PsiReference[0]);
4445
}
4546
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Api;
8+
9+
interface ProductRepositoryInterface
10+
{
11+
public function save();
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
10+
11+
<!-- Product Service -->
12+
<route url="/V1/products" method="POST">
13+
<service class="Magento\Catalog\Api\ProductRepositoryInterface" method="save<caret>"/>
14+
<resources>
15+
<resource ref="Magento_Catalog::products" />
16+
</resources>
17+
</route>
18+
19+
</routes>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
10+
11+
<!-- Product Service -->
12+
<route url="/V1/products" method="POST">
13+
<service class="Magento\Catalog\Api\ProductRepositoryInterface" method="get<caret>"/>
14+
<resources>
15+
<resource ref="Magento_Catalog::products" />
16+
</resources>
17+
</route>
18+
19+
</routes>

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.intellij.psi.xml.XmlAttributeValue;
1414
import com.intellij.psi.xml.XmlFile;
1515
import com.intellij.psi.xml.XmlTag;
16+
import com.jetbrains.php.lang.psi.elements.Method;
1617
import com.jetbrains.php.lang.psi.elements.PhpClass;
1718
import com.magento.idea.magento2plugin.inspections.BaseInspectionsTestCase;
1819
import com.magento.idea.magento2plugin.magento.packages.File;
@@ -153,6 +154,29 @@ protected void assertHasReferencePhpClass(final String phpClassFqn) {
153154
);
154155
}
155156

157+
protected void assertHasReferenceToClassMethod(
158+
final String className,
159+
final String methodName
160+
) {
161+
final PsiElement element = getElementFromCaret();
162+
final PsiReference[] references = element.getReferences();
163+
final String actualClassName = ((PhpClass) references[references.length - 1].resolve()
164+
.getParent()).getFQN();
165+
final String actualMethodName = ((Method) references[references.length - 1].resolve())
166+
.getName();
167+
168+
assertEquals(
169+
"Class name",
170+
className,
171+
actualClassName
172+
);
173+
assertEquals(
174+
"Method name",
175+
methodName,
176+
actualMethodName
177+
);
178+
}
179+
156180
protected void assertEmptyReference() {
157181
final PsiElement element = getElementFromCaret();
158182
assertEmpty(element.getReferences());
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.reference.xml;
7+
8+
public class WebApiMethodReferenceRegistrarTest extends ReferenceXmlFixtureTestCase {
9+
10+
/**
11+
* Tests for reference to valid PHP method in webapi.xml.
12+
*/
13+
public void testWebApiMethodMustHaveReference() {
14+
myFixture.configureByFile(this.getFixturePath("webapi.xml"));
15+
16+
assertHasReferenceToClassMethod(
17+
"\\Magento\\Catalog\\Api\\ProductRepositoryInterface",
18+
"save"
19+
);
20+
}
21+
22+
/**
23+
* Tests for no reference to invalid PHP method in webapi.xml.
24+
*/
25+
public void testWebApiMethodMustNotHaveReference() {
26+
myFixture.configureByFile(this.getFixturePath("webapi.xml"));
27+
28+
assertEmptyReference();
29+
}
30+
}

0 commit comments

Comments
 (0)