Skip to content

Commit bd32aeb

Browse files
author
Vitaliy
authored
Merge pull request #388 from drpayyne/tests-02
Added test for PhpServiceMethodReferenceProvider
2 parents 49294e2 + 50da30a commit bd32aeb

File tree

6 files changed

+118
-14
lines changed

6 files changed

+118
-14
lines changed

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

Lines changed: 14 additions & 14 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;
@@ -14,32 +15,31 @@
1415
import com.jetbrains.php.lang.psi.elements.PhpClass;
1516
import com.magento.idea.magento2plugin.indexes.DiIndex;
1617
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
17-
import org.jetbrains.annotations.NotNull;
18-
1918
import java.util.ArrayList;
2019
import java.util.Collection;
2120
import java.util.List;
21+
import org.jetbrains.annotations.NotNull;
2222

2323
public class PhpServiceMethodReferenceProvider extends PsiReferenceProvider {
2424

2525
@NotNull
2626
@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());
27+
public PsiReference[] getReferencesByElement(
28+
@NotNull final PsiElement element,
29+
@NotNull final ProcessingContext context
30+
) {
31+
final List<PsiReference> psiReferences = new ArrayList<>();
32+
final String methodName = StringUtil.unquoteString(element.getText());
33+
final PhpClass phpClass = DiIndex.getPhpClassOfServiceMethod((XmlElement) element);
3534

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

43-
return psiReferences.toArray(new PsiReference[psiReferences.size()]);
43+
return psiReferences.toArray(new PsiReference[0]);
4444
}
4545
}
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)