Skip to content

Commit c94e6d0

Browse files
committed
Fix tests
1 parent 8804389 commit c94e6d0

File tree

7 files changed

+109
-59
lines changed

7 files changed

+109
-59
lines changed

src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleComposerJsonGenerator.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,22 @@ private Pair<String, String> getDependencyData(
157157
final String dependency
158158
) {
159159
String version = "*";
160-
String moduleName = camelCaseToHyphen.convert(dependency).replace("_-", "/");
160+
String moduleName = camelCaseToHyphen.convert(dependency).replace(
161+
"_-", "/"
162+
);
161163
try {
162164
final PsiFile virtualFile = moduleIndex.getModuleDirectoryByModuleName(dependency)
163165
.findFile(ComposerJson.FILE_NAME);
164166

165-
if (virtualFile != null) {
166-
final VirtualFile composerJsonFile = virtualFile.getVirtualFile();//NOPMD
167+
if (virtualFile != null) { //NOPMD
168+
final VirtualFile composerJsonFile = virtualFile.getVirtualFile();
167169
if (composerJsonFile.exists()) {
168170
final JsonElement jsonElement =
169-
new JsonParser().parse(new FileReader(composerJsonFile.getPath()));//NOPMD
170-
final JsonElement versionJsonElement = jsonElement.getAsJsonObject().get("version");
171+
new JsonParser().parse(
172+
new FileReader(composerJsonFile.getPath())//NOPMD
173+
);
174+
final JsonElement versionJsonElement =
175+
jsonElement.getAsJsonObject().get("version");
171176
final JsonElement nameJsonElement = jsonElement.getAsJsonObject().get("name");
172177
if (versionJsonElement != null) {
173178
version = versionJsonElement.getAsString();
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"registration.php"
1818
],
1919
"psr-4": {
20-
"Test\\Module\\": ""
20+
"TestWithDependencies\\Module\\": ""
2121
}
2222
}
2323
}
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"registration.php"
1818
],
1919
"psr-4": {
20-
"Test\\Module\\": ""
20+
"TestWithDependencies\\Module\\": ""
2121
}
2222
}
2323
}

testData/actions/generation/generator/ModuleComposerJsonGenerator/generateModuleFileWithoutDependencies/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"registration.php"
1616
],
1717
"psr-4": {
18-
"Test\\Module\\": ""
18+
"TestWithoutDependencies\\Module\\": ""
1919
}
2020
}
2121
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "test/module",
3+
"version": "1.0.0-dev",
4+
"description": "test-description",
5+
"type": "magento2-module",
6+
"require": {
7+
"magento/framework": "*"
8+
},
9+
"license": [
10+
"Test License 1",
11+
"Test License 2"
12+
],
13+
"autoload": {
14+
"files": [
15+
"registration.php"
16+
],
17+
"psr-4": {
18+
"Test\\Module\\": ""
19+
}
20+
}
21+
}

tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleComposerJsonGeneratorTest.java

+75-51
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.actions.generation.generator;
67

78
import com.intellij.openapi.project.Project;
@@ -15,38 +16,61 @@
1516

1617
public class ModuleComposerJsonGeneratorTest extends BaseGeneratorTestCase {
1718

18-
public void testGenerateModuleFileWithDependencies() {
19-
String filePath = this.getFixturePath(ComposerJson.FILE_NAME);
20-
PsiFile expectedFile = myFixture.configureByFile(filePath);
21-
PsiDirectory projectDir = getProjectDirectory();
19+
/**
20+
* Test for the module composer.json generation with dependencies.
21+
*/
22+
public void testGenerateModuleFile() {
23+
final String filePath = this.getFixturePath(ComposerJson.FILE_NAME);
24+
final PsiFile expectedFile = myFixture.configureByFile(filePath);
25+
final PsiDirectory projectDir = getProjectDirectory();
2226

23-
String expectedDirectory = projectDir.getVirtualFile().getPath() + "/Test/Module";
24-
PsiFile composerJson = generateComposerJson(true, projectDir, true);
27+
final String expectedDirectory =
28+
projectDir.getVirtualFile().getPath() + "/TestWithDependencies/Module";
29+
final PsiFile composerJson = generateComposerJson(
30+
true,
31+
projectDir,
32+
true,
33+
"TestWithDependencies");
2534

2635
assertGeneratedFileIsCorrect(
27-
expectedFile,
28-
expectedDirectory,
29-
composerJson
36+
expectedFile,
37+
expectedDirectory,
38+
composerJson
3039
);
3140
}
3241

33-
public void testGenerateFileInRootWithDependencies() {
34-
String filePath = this.getFixturePath(ComposerJson.FILE_NAME);
35-
PsiFile expectedFile = myFixture.configureByFile(filePath);
36-
PsiDirectory projectDir = getProjectDirectory();
42+
/**
43+
* Test for generation the composer.json with dependencies in the root directory.
44+
*/
45+
public void testGenerateFileInRoot() {
46+
final String filePath = this.getFixturePath(ComposerJson.FILE_NAME);
47+
final PsiFile expectedFile = myFixture.configureByFile(filePath);
48+
final PsiDirectory projectDir = getProjectDirectory();
3749

38-
String composerJsonDirPath = projectDir.getVirtualFile().getPath();
39-
PsiFile composerJson = generateComposerJson(false, projectDir, true);
50+
final String composerJsonDirPath = projectDir.getVirtualFile().getPath();
51+
final PsiFile composerJson = generateComposerJson(
52+
false,
53+
projectDir,
54+
true,
55+
"TestWithDependencies");
4056

4157
assertGeneratedFileIsCorrect(expectedFile, composerJsonDirPath, composerJson);
4258
}
4359

60+
/**
61+
* Test case for the composer.json generation without dependencies.
62+
*/
4463
public void testGenerateModuleFileWithoutDependencies() {
45-
String filePath = this.getFixturePath(ComposerJson.FILE_NAME);
46-
PsiFile expectedFile = myFixture.configureByFile(filePath);
47-
PsiDirectory projectDir = getProjectDirectory();
48-
String expectedDirectory = projectDir.getVirtualFile().getPath() + "/Test/Module";
49-
PsiFile composerJson = generateComposerJson(true, projectDir, false);
64+
final String filePath = this.getFixturePath(ComposerJson.FILE_NAME);
65+
final PsiFile expectedFile = myFixture.configureByFile(filePath);
66+
final PsiDirectory projectDir = getProjectDirectory();
67+
final String expectedDirectory = projectDir.getVirtualFile().getPath()
68+
+ "/TestWithoutDependencies/Module";
69+
final PsiFile composerJson = generateComposerJson(
70+
true,
71+
projectDir,
72+
false,
73+
"TestWithoutDependencies");
5074

5175
assertGeneratedFileIsCorrect(
5276
expectedFile,
@@ -55,40 +79,40 @@ public void testGenerateModuleFileWithoutDependencies() {
5579
);
5680
}
5781

58-
public void testGenerateFileInRootWithoutDependencies() {
59-
String filePath = this.getFixturePath(ComposerJson.FILE_NAME);
60-
PsiFile expectedFile = myFixture.configureByFile(filePath);
61-
PsiDirectory projectDir = getProjectDirectory();
62-
PsiFile composerJson = generateComposerJson(false, projectDir, false);
63-
64-
assertGeneratedFileIsCorrect(
65-
expectedFile,
66-
projectDir.getVirtualFile().getPath(),
67-
composerJson
68-
);
69-
}
70-
82+
/**
83+
* Generate composer.json file for tests.
84+
*
85+
* @param createModuleDirectories create module directory flag
86+
* @param projectDir project directory
87+
* @param withDependencies generate composer.json with dependencies or not
88+
* @param packageName the package name of the test module
89+
* @return PsiFile
90+
*/
7191
private PsiFile generateComposerJson(
72-
boolean createModuleDirectories,
73-
PsiDirectory projectDir,
74-
boolean withDependencies) {
75-
Project project = myFixture.getProject();
76-
List<String> dependencies = withDependencies ?
77-
new ArrayList<>(Arrays.asList("Foo_Bar", "Magento_Backend"))
92+
final boolean createModuleDirectories,
93+
final PsiDirectory projectDir,
94+
final boolean withDependencies,
95+
final String packageName) {
96+
final Project project = myFixture.getProject();
97+
final List<String> dependencies = withDependencies
98+
? new ArrayList<>(Arrays.asList("Foo_Bar", "Magento_Backend"))
7899
: new ArrayList<>(Arrays.asList("Foo_BarWithOutComposer"));
79-
List<String> licenses = new ArrayList<>(Arrays.asList("Test License 1", "Test License 2"));
80-
ModuleComposerJsonData composerJsonData = new ModuleComposerJsonData(
81-
"Test",
82-
"Module",
83-
projectDir,
84-
"test-description",
85-
"test/module",
86-
"1.0.0-dev",
87-
licenses,
88-
dependencies,
89-
createModuleDirectories
100+
final List<String> licenses = new ArrayList<>(
101+
Arrays.asList("Test License 1", "Test License 2")
102+
);
103+
final ModuleComposerJsonData composerJsonData = new ModuleComposerJsonData(
104+
packageName,
105+
"Module",
106+
projectDir,
107+
"test-description",
108+
"test/module",
109+
"1.0.0-dev",
110+
licenses,
111+
dependencies,
112+
createModuleDirectories
90113
);
91-
ModuleComposerJsonGenerator composerJsonGenerator = new ModuleComposerJsonGenerator(composerJsonData, project);
114+
final ModuleComposerJsonGenerator composerJsonGenerator =
115+
new ModuleComposerJsonGenerator(composerJsonData, project);
92116
return composerJsonGenerator.generate("test");
93117
}
94118
}

0 commit comments

Comments
 (0)