Skip to content

Commit eb260d5

Browse files
committed
Extract service map parsing logic to a class and test it
1 parent 59f6b2a commit eb260d5

File tree

7 files changed

+99
-27
lines changed

7 files changed

+99
-27
lines changed

.idea/libraries/junit_junit_4_9.xml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/hamcrest-core-1.1.jar

74.8 KB
Binary file not shown.

lib/junit-4.9.jar

243 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package fr.adrienbrault.idea.symfony2plugin;
2+
3+
import org.w3c.dom.*;
4+
import org.xml.sax.SAXException;
5+
6+
import javax.xml.parsers.*;
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
/**
14+
* @author Adrien Brault <adrien.brault@gmail.com>
15+
*/
16+
public class ServiceMapParser {
17+
18+
private DocumentBuilder documentBuilder;
19+
20+
public ServiceMapParser() throws ParserConfigurationException {
21+
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
22+
documentBuilder = dbFactory.newDocumentBuilder();
23+
}
24+
25+
public Map<String, String> parse(InputStream stream) throws IOException, SAXException {
26+
return parse(documentBuilder.parse(stream));
27+
}
28+
29+
public Map<String, String> parse(File file) throws IOException, SAXException {
30+
return parse(documentBuilder.parse(file));
31+
}
32+
33+
public Map<String, String> parse(Document document) {
34+
Map<String, String> map = new HashMap<String, String>();
35+
36+
NodeList servicesNodes = document.getElementsByTagName("service");
37+
for (int i = 0; i < servicesNodes.getLength(); i++) {
38+
Element node = (Element) servicesNodes.item(i);
39+
if (node.hasAttribute("class") && node.hasAttribute("id")) {
40+
map.put(node.getAttribute("id"), "\\" + node.getAttribute("class"));
41+
}
42+
}
43+
44+
// Support services whose class isn't specified
45+
map.put("request", "\\Symfony\\Component\\HttpFoundation\\Request");
46+
map.put("service_container", "\\Symfony\\Component\\DependencyInjection\\ContainerInterface");
47+
map.put("kernel", "\\Symfony\\Component\\HttpKernel\\KernelInterface");
48+
49+
return map;
50+
}
51+
52+
}

src/fr/adrienbrault/idea/symfony2plugin/SymfonyContainerTypeProvider.java

+4-27
Original file line numberDiff line numberDiff line change
@@ -102,42 +102,19 @@ private String getServiceId(MethodReferenceImpl e) {
102102
return cachedServiceMap;
103103
}
104104

105-
Document doc = null;
106105
try {
107-
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
108-
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
109-
doc = dBuilder.parse(xmlFile);
106+
ServiceMapParser serviceMapParser = new ServiceMapParser();
107+
cachedServiceMap = serviceMapParser.parse(xmlFile);
108+
cachedServiceMapLastModified = xmlFileLastModified;
110109
} catch (SAXException e) {
111-
e.printStackTrace();
112-
113110
return map;
114111
} catch (IOException e) {
115-
e.printStackTrace();
116-
117112
return map;
118113
} catch (ParserConfigurationException e) {
119-
e.printStackTrace();
120-
121114
return map;
122115
}
123116

124-
NodeList servicesNodes = doc.getElementsByTagName("service");
125-
for (int i = 0; i < servicesNodes.getLength(); i++) {
126-
Element node = (Element) servicesNodes.item(i);
127-
if (node.hasAttribute("class") && node.hasAttribute("id")) {
128-
map.put(node.getAttribute("id"), "\\" + node.getAttribute("class"));
129-
}
130-
}
131-
132-
// Support services whose class isn't specified
133-
map.put("request", "\\Symfony\\Component\\HttpFoundation\\Request");
134-
map.put("service_container", "\\Symfony\\Component\\DependencyInjection\\ContainerInterface");
135-
map.put("kernel", "\\Symfony\\Component\\HttpKernel\\KernelInterface");
136-
137-
cachedServiceMap = map;
138-
cachedServiceMapLastModified = xmlFileLastModified;
139-
140-
return map;
117+
return cachedServiceMap;
141118
}
142119

143120
}

symfony2-plugin.iml

+2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
<exclude-output />
66
<content url="file://$MODULE_DIR$">
77
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
89
</content>
910
<orderEntry type="inheritedJdk" />
1011
<orderEntry type="sourceFolder" forTests="false" />
1112
<orderEntry type="library" scope="PROVIDED" name="php-openapi" level="project" />
1213
<orderEntry type="library" scope="PROVIDED" name="php" level="project" />
14+
<orderEntry type="library" scope="TEST" name="junit:junit:4.9" level="project" />
1315
</component>
1416
</module>
1517

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package fr.adrienbrault.idea.symfony2plugin.tests;
2+
3+
import fr.adrienbrault.idea.symfony2plugin.ServiceMapParser;
4+
import org.junit.Test;
5+
import org.junit.Assert;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.util.Map;
9+
10+
/**
11+
* @author Adrien Brault <adrien.brault@gmail.com>
12+
*/
13+
public class ServiceMapParserTest extends Assert {
14+
15+
@Test
16+
public void testParse() throws Exception {
17+
ServiceMapParser serviceMapParser = new ServiceMapParser();
18+
19+
String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
20+
"<container><service id=\"adrienbrault\" class=\"AdrienBrault\\Awesome\"/></container>";
21+
Map<String, String> serviceMap = serviceMapParser.parse(new ByteArrayInputStream(xmlString.getBytes()));
22+
23+
assertEquals("\\AdrienBrault\\Awesome", serviceMap.get("adrienbrault"));
24+
25+
assertEquals("\\Symfony\\Component\\HttpFoundation\\Request", serviceMap.get("request"));
26+
assertEquals("\\Symfony\\Component\\DependencyInjection\\ContainerInterface", serviceMap.get("service_container"));
27+
assertEquals("\\Symfony\\Component\\HttpKernel\\KernelInterface", serviceMap.get("kernel"));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)