|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | +package com.magento.idea.magento2plugin.actions.generation.generator; |
| 6 | + |
| 7 | +import com.intellij.openapi.project.Project; |
| 8 | +import com.intellij.psi.PsiFile; |
| 9 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 10 | +import com.magento.idea.magento2plugin.actions.generation.data.PluginDiXmlData; |
| 11 | +import com.magento.idea.magento2plugin.magento.files.ModuleDiXml; |
| 12 | +import com.magento.idea.magento2plugin.magento.packages.File; |
| 13 | +import com.magento.idea.magento2plugin.magento.packages.Package; |
| 14 | +import com.magento.idea.magento2plugin.util.GetPhpClassByFQN; |
| 15 | + |
| 16 | +public class PluginDiXmlGeneratorTest extends BaseGeneratorTestCase { |
| 17 | + private static final String pluginTargetClassOneFnq = "Foo\\Bar\\Model\\PluginTargetClassOne"; |
| 18 | + private static final String pluginTargetClassTwoFnq = "Foo\\Bar\\Model\\PluginTargetClassTwo"; |
| 19 | + private static final String pluginClassOneFnq = "Foo\\Bar\\Plugin\\TestOnePlugin"; |
| 20 | + private static final String pluginClassTwoFnq = "Foo\\Bar\\Plugin\\TestTwoPlugin"; |
| 21 | + private static final String module = "Foo_Bar"; |
| 22 | + private static final String moduleDir = "src/app/code/Foo/Bar/"; |
| 23 | + |
| 24 | + public void testGeneratePluginDiXmlFileForBaseArea() |
| 25 | + { |
| 26 | + String filePath = this.getFixturePath(ModuleDiXml.FILE_NAME); |
| 27 | + PsiFile expectedFile = myFixture.configureByFile(filePath); |
| 28 | + String area = Package.Areas.base.toString(); |
| 29 | + |
| 30 | + PsiFile diXml = addPluginDiXml( |
| 31 | + pluginTargetClassOneFnq, |
| 32 | + area, |
| 33 | + "10", |
| 34 | + "test_plugin_name_1", |
| 35 | + pluginClassOneFnq |
| 36 | + ); |
| 37 | + |
| 38 | + assertGeneratedFileIsCorrect( |
| 39 | + expectedFile, |
| 40 | + getExpectedDirectory(area), |
| 41 | + diXml |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + public void testGeneratePluginDiXmlFileForAdminhtmlArea() |
| 46 | + { |
| 47 | + String filePath = this.getFixturePath(ModuleDiXml.FILE_NAME); |
| 48 | + PsiFile expectedFile = myFixture.configureByFile(filePath); |
| 49 | + String area = Package.Areas.adminhtml.toString(); |
| 50 | + |
| 51 | + PsiFile diXml = addPluginDiXml( |
| 52 | + pluginTargetClassTwoFnq, |
| 53 | + area, |
| 54 | + "20", |
| 55 | + "test_plugin_name_2", |
| 56 | + pluginClassTwoFnq |
| 57 | + ); |
| 58 | + |
| 59 | + assertGeneratedFileIsCorrect( |
| 60 | + expectedFile, |
| 61 | + getExpectedDirectory(area), |
| 62 | + diXml |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public void testAddTwoPluginsToOneDiXml() |
| 67 | + { |
| 68 | + String filePath = this.getFixturePath(ModuleDiXml.FILE_NAME); |
| 69 | + PsiFile expectedFile = myFixture.configureByFile(filePath); |
| 70 | + String area = Package.Areas.frontend.toString(); |
| 71 | + addPluginDiXml( |
| 72 | + pluginTargetClassOneFnq, |
| 73 | + area, |
| 74 | + "10", |
| 75 | + "test_plugin_name_1", |
| 76 | + pluginClassOneFnq |
| 77 | + ); |
| 78 | + PsiFile diXml = addPluginDiXml( |
| 79 | + pluginTargetClassTwoFnq, |
| 80 | + area, |
| 81 | + "20", |
| 82 | + "test_plugin_name_2", |
| 83 | + pluginClassTwoFnq |
| 84 | + ); |
| 85 | + |
| 86 | + assertGeneratedFileIsCorrect( |
| 87 | + expectedFile, |
| 88 | + getExpectedDirectory(area), |
| 89 | + diXml |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + public void testAddTwoPluginsToOneTargetClass() |
| 94 | + { |
| 95 | + String filePath = this.getFixturePath(ModuleDiXml.FILE_NAME); |
| 96 | + PsiFile expectedFile = myFixture.configureByFile(filePath); |
| 97 | + String area = Package.Areas.frontend.toString(); |
| 98 | + addPluginDiXml( |
| 99 | + pluginTargetClassOneFnq, |
| 100 | + area, |
| 101 | + "10", |
| 102 | + "test_plugin_name_1", |
| 103 | + pluginClassOneFnq |
| 104 | + ); |
| 105 | + PsiFile diXml = addPluginDiXml( |
| 106 | + pluginTargetClassOneFnq, |
| 107 | + area, |
| 108 | + "20", |
| 109 | + "test_plugin_name_2", |
| 110 | + pluginClassTwoFnq |
| 111 | + ); |
| 112 | + |
| 113 | + assertGeneratedFileIsCorrect( |
| 114 | + expectedFile, |
| 115 | + getExpectedDirectory(area), |
| 116 | + diXml |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + private PsiFile addPluginDiXml( |
| 121 | + String targetClassFnq, |
| 122 | + String area, |
| 123 | + String SortOrder, |
| 124 | + String pluginName, |
| 125 | + String pluginClassFnq |
| 126 | + ) { |
| 127 | + Project project = myFixture.getProject(); |
| 128 | + PhpClass targetClass = GetPhpClassByFQN.getInstance(project).execute(targetClassFnq); |
| 129 | + PluginDiXmlData pluginDiXmlData = new PluginDiXmlData( |
| 130 | + area, |
| 131 | + module, |
| 132 | + targetClass, |
| 133 | + SortOrder, |
| 134 | + pluginName, |
| 135 | + pluginClassFnq |
| 136 | + ); |
| 137 | + PluginDiXmlGenerator moduleXmlGenerator = new PluginDiXmlGenerator(pluginDiXmlData, project); |
| 138 | + |
| 139 | + return moduleXmlGenerator.generate("test"); |
| 140 | + } |
| 141 | + |
| 142 | + private String getExpectedDirectory(String area) |
| 143 | + { |
| 144 | + if (area.equals(Package.Areas.base.toString())) { |
| 145 | + return moduleDir + Package.MODULE_BASE_AREA_DIR; |
| 146 | + } |
| 147 | + |
| 148 | + return moduleDir + Package.MODULE_BASE_AREA_DIR + File.separator + area; |
| 149 | + } |
| 150 | +} |
0 commit comments