-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathEntityHelperTest.java
123 lines (101 loc) · 5.7 KB
/
EntityHelperTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package fr.adrienbrault.idea.symfony2plugin.tests.doctrine;
import com.intellij.psi.PsiElement;
import com.intellij.util.containers.ContainerUtil;
import fr.adrienbrault.idea.symfony2plugin.doctrine.EntityHelper;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import fr.adrienbrault.idea.symfony2plugin.util.dict.DoctrineModel;
import org.jetbrains.yaml.psi.YAMLKeyValue;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* @author Daniel Espendiller <daniel@espendiller.net>
* @see fr.adrienbrault.idea.symfony2plugin.doctrine.EntityHelper
*/
public class EntityHelperTest extends SymfonyLightCodeInsightFixtureTestCase {
public void setUp() throws Exception {
super.setUp();
myFixture.configureFromExistingVirtualFile(myFixture.copyFileToProject("entity_helper.php"));
myFixture.configureFromExistingVirtualFile(myFixture.copyFileToProject("doctrine.orm.yml"));
}
public String getTestDataPath() {
return "src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/doctrine/fixtures";
}
/**
* @see fr.adrienbrault.idea.symfony2plugin.doctrine.EntityHelper#getEntityRepositoryClass
*/
public void testGetEntityRepositoryClass() {
assertEquals("FooBundle\\Entity\\BarRepository", EntityHelper.getEntityRepositoryClass(getProject(), "FooBundle:Bar").getPresentableFQN());
assertEquals("FooBundle\\Entity\\BarRepository", EntityHelper.getEntityRepositoryClass(getProject(), "FooBundle\\Entity\\Bar").getPresentableFQN());
}
/**
* @see fr.adrienbrault.idea.symfony2plugin.doctrine.EntityHelper#getEntityRepositoryClass
*/
public void testGetEntityRepositoryClassInSameNamespaceFallback() {
assertEquals("FooBundle\\Entity\\Car\\BarRepository", EntityHelper.getEntityRepositoryClass(getProject(), "FooBundle:Car\\Bar").getPresentableFQN());
}
/**
* @see fr.adrienbrault.idea.symfony2plugin.doctrine.EntityHelper#getEntityRepositoryClass
*/
public void testGetEntityRepositoryClassAaClassConstant() {
assertEquals(
"FooBundle\\Entity\\BarRepository",
EntityHelper.getEntityRepositoryClass(getProject(), "FooBundle\\Entity\\Bar2").getPresentableFQN()
);
}
/**
* @see fr.adrienbrault.idea.symfony2plugin.doctrine.EntityHelper#getEntityRepositoryClass
*/
public void testGetEntityRepositoryClassAaClassConstantWithAlias() {
assertEquals(
"FooBundle\\Entity\\BarRepository",
EntityHelper.getEntityRepositoryClass(getProject(), "FooBundle\\Entity\\Bar3").getPresentableFQN()
);
}
/**
* @see fr.adrienbrault.idea.symfony2plugin.doctrine.EntityHelper#resolveShortcutName
*/
public void testResolveShortcutName() {
assertEquals("FooBundle\\Entity\\Bar", EntityHelper.resolveShortcutName(getProject(), "FooBundle:Bar").getPresentableFQN());
assertEquals("FooBundle\\Document\\Doc", EntityHelper.resolveShortcutName(getProject(), "FooBundle:Doc").getPresentableFQN());
assertEquals("FooBundle\\Entity\\Car\\Bar", EntityHelper.resolveShortcutName(getProject(), "FooBundle:Car\\Bar").getPresentableFQN());
assertEquals("FooBundle\\Entity\\Bar", EntityHelper.resolveShortcutName(getProject(), "FooBundle\\Entity\\Bar").getPresentableFQN());
assertEquals("FooBundle\\Document\\Doc", EntityHelper.resolveShortcutName(getProject(), "FooBundle\\Document\\Doc").getPresentableFQN());
assertEquals("FooBundle\\Entity\\Car\\Bar", EntityHelper.resolveShortcutName(getProject(), "FooBundle\\Entity\\Car\\Bar").getPresentableFQN());
assertEquals("FooBundle\\Entity\\Bar", EntityHelper.resolveShortcutName(getProject(), "\\FooBundle\\Entity\\Bar").getPresentableFQN());
assertEquals("FooBundle\\Document\\Doc", EntityHelper.resolveShortcutName(getProject(), "\\FooBundle\\Document\\Doc").getPresentableFQN());
assertNull("FooBundle\\Document\\Doc", EntityHelper.resolveShortcutName(getProject(), "FooBundle:Bike"));
assertNull("FooBundle\\Document\\Doc", EntityHelper.resolveShortcutName(getProject(), "BarCarBundle:Bar"));
}
/**
* @see fr.adrienbrault.idea.symfony2plugin.doctrine.EntityHelper#getModelClasses
*/
public void testGetModelClasses() {
Collection<DoctrineModel> modelClasses = EntityHelper.getModelClasses(getProject());
Map<String, String> map = new HashMap<>();
for (DoctrineModel modelClass : modelClasses) {
map.put(modelClass.getRepositoryName(), modelClass.getDoctrineNamespace());
}
// @TODO: Fix namespace collision
//assertContainsElements(map.keySet(), "FooBundle:Doc");
//assertContainsElements(map.values(), "\\FooBundle\\Document");
assertContainsElements(map.keySet(), "FooBundle:Couch");
assertContainsElements(map.values(), "\\FooBundle\\CouchDocument");
// class fallback
assertContainsElements(map.keySet(), "FooBundle\\Entity\\Bar");
// interface; instance blacklist
assertFalse(map.values().contains("FooBundle:BarRepository"));
assertFalse(map.values().contains("FooBundle:BarInterface"));
}
/**
* @see fr.adrienbrault.idea.symfony2plugin.doctrine.EntityHelper#getModelFieldTargets
*/
public void testGetModelFieldTargets() {
PsiElement[] names = EntityHelper.getModelFieldTargets(PhpElementsUtil.getClass(getProject(), "FooBundle\\Entity\\Yaml"), "name");
assertNotNull(ContainerUtil.find(
names,
psiElement -> psiElement instanceof YAMLKeyValue && ((YAMLKeyValue) psiElement).getKeyText().equals("name")
));
}
}