Skip to content

Commit 4f3703b

Browse files
committed
Covered PhpJobMethodReferenceProvider with tests
1 parent 305bfcd commit 4f3703b

File tree

6 files changed

+62
-23
lines changed

6 files changed

+62
-23
lines changed

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ public class PhpJobMethodReferenceProvider extends PsiReferenceProvider {
2828

2929
@NotNull
3030
@Override
31-
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
32-
if (!(element instanceof XmlElement)) {
33-
return PsiReference.EMPTY_ARRAY;
34-
}
35-
36-
List<PsiReference> psiReferences = new ArrayList<>();
37-
38-
String methodName = StringUtil.unquoteString(element.getText());
31+
public PsiReference[] getReferencesByElement(
32+
@NotNull final PsiElement element,
33+
@NotNull final ProcessingContext context
34+
) {
35+
final List<PsiReference> psiReferences = new ArrayList<>();
36+
final String methodName = StringUtil.unquoteString(element.getText());
37+
final PhpClass phpClass = DiIndex.getPhpClassOfJobMethod((XmlElement) element);
3938

40-
PhpClass phpClass = DiIndex.getPhpClassOfJobMethod((XmlElement) element);
4139
if (phpClass != null) {
42-
Collection<Method> methods = phpClass.getMethods();
43-
methods.removeIf(method -> !method.getName().contains(methodName));
44-
psiReferences.add(new PolyVariantReferenceBase(element, methods));
40+
final Collection<Method> methods = phpClass.getMethods();
41+
methods.removeIf(method -> !method.getName().matches(methodName));
42+
if (!methods.isEmpty()) {
43+
psiReferences.add(new PolyVariantReferenceBase(element, methods));
44+
}
4545
}
4646

47-
return psiReferences.toArray(new PsiReference[psiReferences.size()]);
47+
return psiReferences.toArray(new PsiReference[0]);
4848
}
4949
}

testData/reference/xml/CrontabReferenceRegistrar/crontabMethodMustHaveReference/crontab.xml

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
99
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
1010
<group id="default">
11-
<job name="test_catalog_index_refresh_price" instance="Magento\Catalog\Cron\RefreshSpecialPrices" method="execute<caret>">
12-
<schedule>0 * * * *</schedule>
11+
<job name="catalog_index_refresh_price"
12+
instance="Magento\Catalog\Cron\RefreshSpecialPrices"
13+
method="execute<caret>">
14+
<schedule>* * * * *</schedule>
1315
</job>
1416
</group>
1517
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
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:module:Magento_Cron:etc/crontab.xsd">
10+
<group id="default">
11+
<job name="catalog_index_refresh_price"
12+
instance="Magento\Catalog\Cron\RefreshSpecialPrices"
13+
method="notExecute<caret>">
14+
<schedule>* * * * *</schedule>
15+
</job>
16+
</group>
17+
</config>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ protected void assertHasReferenceToClassMethod(
188188
final PsiElement element = getElementFromCaret();
189189
final PsiReference[] references = element.getReferences();
190190
final String actualClassName = ((PhpClass) references[references.length - 1].resolve()
191-
.getParent()).getFQN();
191+
.getParent()).getPresentableFQN();
192192
final String actualMethodName = ((Method) references[references.length - 1].resolve())
193193
.getName();
194194

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

+26-6
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,36 @@
88
import com.magento.idea.magento2plugin.magento.files.CrontabXmlTemplate;
99

1010
public class CrontabReferenceRegistrarTest extends ReferenceXmlFixtureTestCase {
11-
private static final String EXPECTED_REFERENCE = "Magento\\Catalog\\Cron\\RefreshSpecialPrices";
11+
private static final String EXPECTED_CLASS = "Magento\\Catalog\\Cron\\RefreshSpecialPrices";
12+
private static final String EXPECTED_METHOD = "execute";
1213

1314
/**
14-
* Test instance attribute of the crontab.xml file
15-
* must have reference.
15+
* Test instance attribute of the crontab.xml file must have reference.
1616
*/
1717
public void testCrontabInstanceMustHaveReference() {
18-
final String filePath = this.getFixturePath(CrontabXmlTemplate.FILE_NAME);
19-
myFixture.configureByFile(filePath);
18+
myFixture.configureByFile(this.getFixturePath(CrontabXmlTemplate.FILE_NAME));
2019

21-
assertHasReferencePhpClass(EXPECTED_REFERENCE);
20+
assertHasReferencePhpClass(EXPECTED_CLASS);
21+
}
22+
23+
/**
24+
* Tests for reference to valid PHP method in crontab.xml.
25+
*/
26+
public void testCrontabMethodMustHaveReference() {
27+
myFixture.configureByFile(this.getFixturePath(CrontabXmlTemplate.FILE_NAME));
28+
29+
assertHasReferenceToClassMethod(
30+
EXPECTED_CLASS,
31+
EXPECTED_METHOD
32+
);
33+
}
34+
35+
/**
36+
* Tests for no reference to invalid PHP method in crontab.xml.
37+
*/
38+
public void testCrontabMethodMustNotHaveReference() {
39+
myFixture.configureByFile(this.getFixturePath(CrontabXmlTemplate.FILE_NAME));
40+
41+
assertEmptyReference();
2242
}
2343
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void testWebApiMethodMustHaveReference() {
1414
myFixture.configureByFile(this.getFixturePath("webapi.xml"));
1515

1616
assertHasReferenceToClassMethod(
17-
"\\Magento\\Catalog\\Api\\ProductRepositoryInterface",
17+
"Magento\\Catalog\\Api\\ProductRepositoryInterface",
1818
"save"
1919
);
2020
}

0 commit comments

Comments
 (0)