Skip to content

Commit df1d9ee

Browse files
authored
Merge branch '2.1.0-develop' into models-generation
2 parents cf1161e + 9046134 commit df1d9ee

File tree

16 files changed

+163
-38
lines changed

16 files changed

+163
-38
lines changed

Diff for: resources/fileTemplates/code/Magento Module DI Xml Plugin.xml.ft

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
<type name="${TYPE}">
33
#end
44
<plugin name="${NAME}"
5-
type="${PLUGIN_TYPE}" sortOrder="${SORT_ORDER}" />
5+
#if (${SORT_ORDER})
6+
type="${PLUGIN_TYPE}"
7+
sortOrder="${SORT_ORDER}" />
8+
#else
9+
type="${PLUGIN_TYPE}" />
10+
#end
611
#if (${TYPE})
712
</type>
813
#end

Diff for: src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.form

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@
131131
</grid>
132132
</constraints>
133133
<properties>
134-
<text value="10"/>
135134
<toolTipText value="Plugin sort order in di.xml"/>
136135
</properties>
137136
<clientProperties>

Diff for: src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.java

-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ public class CreateAPluginDialog extends AbstractDialog {
8282
message = {DirectoryRule.MESSAGE, DIRECTORY})
8383
private JTextField pluginDirectory;
8484

85-
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
86-
message = {NotEmptyRule.MESSAGE, SORT_ORDER})
8785
@FieldValidation(rule = RuleRegistry.NUMERIC,
8886
message = {NumericRule.MESSAGE, SORT_ORDER})
8987
private JTextField pluginSortOrder;

Diff for: src/com/magento/idea/magento2plugin/actions/generation/generator/PluginDiXmlGenerator.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ protected void fillAttributes(final Properties attributes) {
161161
attributes.setProperty("NAME", pluginFileData.getPluginName());
162162
attributes.setProperty("PLUGIN_TYPE", pluginFileData.getPluginFqn());
163163
attributes.setProperty("PLUGIN_NAME", pluginFileData.getPluginName());
164-
attributes.setProperty("SORT_ORDER", pluginFileData.getSortOrder());
164+
final String sortOrder = pluginFileData.getSortOrder();
165+
if (!sortOrder.isEmpty()) {
166+
attributes.setProperty("SORT_ORDER", sortOrder);
167+
}
165168
}
166169
}

Diff for: src/com/magento/idea/magento2plugin/reference/provider/EventNameReferenceProvider.java

+33-21
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;
@@ -15,52 +16,63 @@
1516
import com.intellij.util.indexing.FileBasedIndex;
1617
import com.jetbrains.php.lang.PhpFileType;
1718
import com.jetbrains.php.lang.psi.PhpFile;
18-
import com.magento.idea.magento2plugin.util.php.PhpPatternsHelper;
1919
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
2020
import com.magento.idea.magento2plugin.stubs.indexes.EventNameIndex;
21-
import org.jetbrains.annotations.NotNull;
22-
21+
import com.magento.idea.magento2plugin.util.php.PhpPatternsHelper;
2322
import java.util.ArrayList;
2423
import java.util.Collection;
2524
import java.util.List;
25+
import org.jetbrains.annotations.NotNull;
2626

2727
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()
34-
.getContainingFiles(EventNameIndex.KEY, value,
35-
GlobalSearchScope.getScopeRestrictedByFileTypes(
36-
GlobalSearchScope.allScope(element.getProject()),
37-
PhpFileType.INSTANCE
38-
)
39-
);
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()
38+
.getContainingFiles(
39+
EventNameIndex.KEY,
40+
value,
41+
GlobalSearchScope.getScopeRestrictedByFileTypes(
42+
GlobalSearchScope.allScope(element.getProject()),
43+
PhpFileType.INSTANCE
44+
)
45+
);
4046

41-
PsiManager psiManager = PsiManager.getInstance(element.getProject());
42-
for (VirtualFile virtualFile: containingFiles) {
43-
PhpFile phpFile = (PhpFile) psiManager.findFile(virtualFile);
47+
final PsiManager psiManager = PsiManager.getInstance(element.getProject());
48+
final List<PsiElement> psiElements = new ArrayList<>();
49+
for (final VirtualFile virtualFile: containingFiles) {
50+
final PhpFile phpFile = (PhpFile) psiManager.findFile(virtualFile);
4451
if (phpFile != null) {
45-
List<PsiElement> psiElements = new ArrayList<>();
4652
recursiveFill(psiElements, phpFile, value);
47-
if (psiElements.size() > 0) {
48-
return new PsiReference[] {new PolyVariantReferenceBase(element, psiElements)};
53+
if (!psiElements.isEmpty()) {
54+
psiReferences.add(new PolyVariantReferenceBase(element, psiElements));//NOPMD
55+
break;
4956
}
5057
}
5158
}
52-
return PsiReference.EMPTY_ARRAY;
59+
60+
return psiReferences.toArray(new PsiReference[0]);
5361
}
5462

55-
private void recursiveFill(List<PsiElement> psiElements, PsiElement psiElement, String typeName) {
63+
private void recursiveFill(
64+
final List<PsiElement> psiElements,
65+
final PsiElement psiElement,
66+
final String typeName
67+
) {
5668
if (PhpPatternsHelper.STRING_METHOD_ARGUMENT.accepts(psiElement)) {
5769
if (StringUtil.unquoteString(psiElement.getText()).equals(typeName)) {
5870
psiElements.add(psiElement);
5971
}
6072
return;
6173
}
6274

63-
for (PsiElement child: psiElement.getChildren()) {
75+
for (final PsiElement child: psiElement.getChildren()) {
6476
recursiveFill(psiElements, child, typeName);
6577
}
6678
}

Diff for: testData/actions/generation/generator/PluginDiXmlGenerator/addTwoPluginsToOneDiXml/di.xml

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
44
<type name="Foo\Bar\Model\PluginTargetClassOne">
55
<plugin name="test_plugin_name_1"
6-
type="Foo\Bar\Plugin\TestOnePlugin" sortOrder="10"/>
6+
type="Foo\Bar\Plugin\TestOnePlugin"
7+
sortOrder="10"/>
78
</type>
89
<type name="Foo\Bar\Model\PluginTargetClassTwo">
910
<plugin name="test_plugin_name_2"
10-
type="Foo\Bar\Plugin\TestTwoPlugin" sortOrder="20"/>
11+
type="Foo\Bar\Plugin\TestTwoPlugin"
12+
sortOrder="20"/>
1113
</type>
1214
</config>

Diff for: testData/actions/generation/generator/PluginDiXmlGenerator/addTwoPluginsToOneTargetClass/di.xml

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
44
<type name="Foo\Bar\Model\PluginTargetClassOne">
55
<plugin name="test_plugin_name_1"
6-
type="Foo\Bar\Plugin\TestOnePlugin" sortOrder="10"/>
6+
type="Foo\Bar\Plugin\TestOnePlugin"
7+
sortOrder="10"/>
78
<plugin name="test_plugin_name_2"
8-
type="Foo\Bar\Plugin\TestTwoPlugin" sortOrder="20"/>
9+
type="Foo\Bar\Plugin\TestTwoPlugin"
10+
sortOrder="20"/>
911
</type>
1012
</config>

Diff for: testData/actions/generation/generator/PluginDiXmlGenerator/generatePluginDiXmlFileForAdminhtmlArea/di.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
44
<type name="Foo\Bar\Model\PluginTargetClassTwo">
55
<plugin name="test_plugin_name_2"
6-
type="Foo\Bar\Plugin\TestTwoPlugin" sortOrder="20"/>
6+
type="Foo\Bar\Plugin\TestTwoPlugin"
7+
sortOrder="20"/>
78
</type>
89
</config>

Diff for: testData/actions/generation/generator/PluginDiXmlGenerator/generatePluginDiXmlFileForBaseArea/di.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
44
<type name="Foo\Bar\Model\PluginTargetClassOne">
55
<plugin name="test_plugin_name_1"
6-
type="Foo\Bar\Plugin\TestOnePlugin" sortOrder="10"/>
6+
type="Foo\Bar\Plugin\TestOnePlugin"
7+
sortOrder="10"/>
78
</type>
89
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
4+
<type name="Foo\Bar\Model\PluginTargetClassOne">
5+
<plugin name="test_plugin_name_1"
6+
type="Foo\Bar\Plugin\TestOnePlugin"/>
7+
</type>
8+
</config>
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="test_event_in_block<caret>">
11+
<observer name="test_observer" instance="Magento\Catalog\Observer\TestObserver" />
12+
</event>
13+
</config>
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>

Diff for: tests/com/magento/idea/magento2plugin/actions/generation/generator/PluginDiXmlGeneratorTest.java

+28-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@ public class PluginDiXmlGeneratorTest extends BaseGeneratorTestCase {
2424
private static final String PLUGIN_CLASS_TWO_FNQ = "Foo\\Bar\\Plugin\\TestTwoPlugin";
2525
private static final String MODULE = "Foo_Bar";
2626
private static final String MODULE_DIR = "src/app/code/Foo/Bar/";
27+
private static final String TEST_PLUGIN_NAME = "test_plugin_name_1";
28+
29+
/**
30+
* Test checks whether di.xml is generated correctly for the base area
31+
*/
32+
public void testGeneratePluginDiXmlFileWithoutSortOrder() {
33+
final PsiFile expectedFile = myFixture.configureByFile(
34+
this.getFixturePath(ModuleDiXml.FILE_NAME)
35+
);
36+
final String area = Areas.base.toString();
37+
38+
final PsiFile diXml = addPluginDiXml(
39+
PLUGIN_TARGET_CLASS_ONE_FNQ,
40+
area,
41+
"",
42+
TEST_PLUGIN_NAME,
43+
PLUGIN_CLASS_ONE_FNQ
44+
);
45+
46+
assertGeneratedFileIsCorrect(
47+
expectedFile,
48+
getExpectedDirectory(area),
49+
diXml
50+
);
51+
}
2752

2853
/**
2954
* Test checks whether di.xml is generated correctly for the base area
@@ -37,7 +62,7 @@ public void testGeneratePluginDiXmlFileForBaseArea() {
3762
PLUGIN_TARGET_CLASS_ONE_FNQ,
3863
area,
3964
"10",
40-
"test_plugin_name_1",
65+
TEST_PLUGIN_NAME,
4166
PLUGIN_CLASS_ONE_FNQ
4267
);
4368

@@ -82,7 +107,7 @@ public void testAddTwoPluginsToOneDiXml() {
82107
PLUGIN_TARGET_CLASS_ONE_FNQ,
83108
area,
84109
"10",
85-
"test_plugin_name_1",
110+
TEST_PLUGIN_NAME,
86111
PLUGIN_CLASS_ONE_FNQ
87112
);
88113
final PsiFile diXml = addPluginDiXml(
@@ -111,7 +136,7 @@ public void testAddTwoPluginsToOneTargetClass() {
111136
PLUGIN_TARGET_CLASS_ONE_FNQ,
112137
area,
113138
"10",
114-
"test_plugin_name_1",
139+
TEST_PLUGIN_NAME,
115140
PLUGIN_CLASS_ONE_FNQ
116141
);
117142
final PsiFile diXml = addPluginDiXml(

Diff for: tests/com/magento/idea/magento2plugin/reference/BaseReferenceTestCase.java

+22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package com.magento.idea.magento2plugin.reference;
77

8+
import com.intellij.openapi.util.text.StringUtil;
89
import com.intellij.psi.PsiElement;
910
import com.intellij.psi.PsiFile;
1011
import com.intellij.psi.PsiReference;
@@ -15,12 +16,16 @@
1516
import com.intellij.psi.xml.XmlTag;
1617
import com.jetbrains.php.lang.psi.elements.Method;
1718
import com.jetbrains.php.lang.psi.elements.Parameter;
19+
import com.jetbrains.php.lang.psi.elements.ParameterList;
1820
import com.jetbrains.php.lang.psi.elements.PhpClass;
1921
import com.magento.idea.magento2plugin.inspections.BaseInspectionsTestCase;
2022
import com.magento.idea.magento2plugin.magento.packages.File;
2123
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
2224
import org.jetbrains.annotations.Nullable;
2325

26+
@SuppressWarnings({
27+
"PMD.TooManyMethods",
28+
})
2429
public abstract class BaseReferenceTestCase extends BaseInspectionsTestCase {
2530
private static final String testDataFolderPath = "testData" + File.separator//NOPMD
2631
+ "reference" + File.separator;
@@ -204,6 +209,23 @@ protected void assertHasReferenceToClassMethod(
204209
);
205210
}
206211

212+
protected void assertHasReferenceToMethodArgument(final String argument) {
213+
final PsiReference[] references = getElementFromCaret().getReferences();
214+
final String actualArgument
215+
= StringUtil.unquoteString(references[references.length - 1].resolve().getText());
216+
final PsiElement parameterList = references[0].resolve().getParent();
217+
218+
if (!(parameterList instanceof ParameterList)) {
219+
fail("Element doesn't have a reference to a method parameter");
220+
}
221+
222+
assertEquals(
223+
"Event dispatch argument",
224+
argument,
225+
actualArgument
226+
);
227+
}
228+
207229
protected void assertEmptyReference() {
208230
final PsiElement element = getElementFromCaret();
209231
assertEmpty(element.getReferences());

Diff for: tests/com/magento/idea/magento2plugin/reference/xml/ObserverReferenceRegistrarTest.java

+24-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,37 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.reference.xml;
67

78
import com.magento.idea.magento2plugin.magento.files.ModuleEventsXml;
89

910
public class ObserverReferenceRegistrarTest extends ReferenceXmlFixtureTestCase {
1011

11-
public void testEventsXmlMustHaveReference() {
12-
String filePath = this.getFixturePath(ModuleEventsXml.FILE_NAME);
13-
myFixture.configureByFile(filePath);
12+
/**
13+
* Tests for observer instance reference in events.xml.
14+
*/
15+
public void testObserverInstanceMustHaveReference() {
16+
myFixture.configureByFile(this.getFixturePath(ModuleEventsXml.FILE_NAME));
1417

1518
assertHasReferencePhpClass("Magento\\Catalog\\Observer\\TestObserver");
1619
}
20+
21+
/**
22+
* Tests for event name reference in events.xml.
23+
*/
24+
public void testEventNameMustHaveReference() {
25+
myFixture.configureByFile(this.getFixturePath(ModuleEventsXml.FILE_NAME));
26+
27+
assertHasReferenceToMethodArgument("test_event_in_block");
28+
}
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+
}
1738
}

0 commit comments

Comments
 (0)