Skip to content

Commit 1347346

Browse files
committed
Impl. Expression Language Injections for PHP (tests)
1 parent de0dff9 commit 1347346

File tree

2 files changed

+471
-0
lines changed

2 files changed

+471
-0
lines changed
Lines changed: 334 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,334 @@
1+
package fr.adrienbrault.idea.symfony2plugin.tests.lang;
2+
3+
import com.intellij.testFramework.fixtures.InjectionTestFixture;
4+
import com.jetbrains.php.lang.PhpFileType;
5+
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
6+
import org.jetbrains.annotations.NotNull;
7+
8+
import static fr.adrienbrault.idea.symfony2plugin.lang.StringLiteralLanguageInjector.LANGUAGE_ID_EXPRESSION_LANGUAGE;
9+
10+
public class StringLiteralLanguageInjectorTest extends SymfonyLightCodeInsightFixtureTestCase {
11+
12+
private InjectionTestFixture injectionTestFixture;
13+
14+
@Override
15+
public void setUp() throws Exception {
16+
super.setUp();
17+
myFixture.copyFileToProject("classes.php");
18+
injectionTestFixture = new InjectionTestFixture(myFixture);
19+
}
20+
21+
public String getTestDataPath() {
22+
return "src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/lang/fixtures";
23+
}
24+
25+
public void testExpressionLanguageLanguageInjections() {
26+
assertInjectedLangAtCaret(
27+
"<?php $expr = new \\Symfony\\Component\\ExpressionLanguage\\Expression('<caret>');",
28+
LANGUAGE_ID_EXPRESSION_LANGUAGE
29+
);
30+
31+
assertInjectedLangAtCaret(
32+
"<?php \n" +
33+
"$expr = new \\Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage(); \n" +
34+
"$expr->evaluate('<caret>');\n",
35+
LANGUAGE_ID_EXPRESSION_LANGUAGE
36+
);
37+
38+
assertInjectedLangAtCaret(
39+
"<?php \n" +
40+
"$expr = new \\Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage(); \n" +
41+
"$expr->compile('<caret>');\n",
42+
LANGUAGE_ID_EXPRESSION_LANGUAGE
43+
);
44+
45+
assertInjectedLangAtCaret(
46+
"<?php \n" +
47+
"$expr = new \\Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage(); \n" +
48+
"$expr->parse('<caret>');\n",
49+
LANGUAGE_ID_EXPRESSION_LANGUAGE
50+
);
51+
52+
assertInjectedLangAtCaret(
53+
"<?php \n" +
54+
"$routes = new \\Symfony\\Component\\Routing\\Loader\\Configurator\\RoutingConfigurator();\n" +
55+
"$routes->condition('<caret>');\n",
56+
LANGUAGE_ID_EXPRESSION_LANGUAGE
57+
);
58+
59+
assertInjectedLangAtCaret(
60+
"<?php \n" +
61+
"$routes = new \\Symfony\\Component\\Routing\\Loader\\Configurator\\RoutingConfigurator();\n" +
62+
"$routes->condition(condition: '<caret>');\n",
63+
LANGUAGE_ID_EXPRESSION_LANGUAGE
64+
);
65+
66+
assertInjectedLangAtCaret(
67+
"<?php\n" +
68+
"\n" +
69+
"use Symfony\\Component\\Routing\\Annotation\\Route;\n" +
70+
"\n" +
71+
"class ExampleController\n" +
72+
"{\n" +
73+
" #[Route(\"/contact\", condition: \"<caret>\")]\n" +
74+
" public function contact() {}\n" +
75+
"}\n",
76+
LANGUAGE_ID_EXPRESSION_LANGUAGE
77+
);
78+
79+
assertInjectedLangAtCaret(
80+
"<?php\n" +
81+
"\n" +
82+
"use Symfony\\Component\\Routing\\Annotation\\Route;\n" +
83+
"\n" +
84+
"class ExampleController\n" +
85+
"{\n" +
86+
" #[Route([], \"/contact\", \"contact\", [], [], [], null, [], [], '<caret>')]\n" +
87+
" public function contact() {}\n" +
88+
"}",
89+
LANGUAGE_ID_EXPRESSION_LANGUAGE
90+
);
91+
92+
assertInjectedLangAtCaret(
93+
"<?php\n" +
94+
"\n" +
95+
"use Symfony\\Component\\Routing\\Annotation\\Route;\n" +
96+
"\n" +
97+
"class ExampleController\n" +
98+
"{\n" +
99+
" /**\n" +
100+
" * @Route(\"/contact\", name=\"contact\", condition=\"<caret>\")\n" +
101+
" */\n" +
102+
" public function contact() {}\n" +
103+
"}",
104+
LANGUAGE_ID_EXPRESSION_LANGUAGE
105+
);
106+
107+
assertInjectedLangAtCaret(
108+
"<?php\n" +
109+
"\n" +
110+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Route;\n" +
111+
"\n" +
112+
"class ExampleController\n" +
113+
"{\n" +
114+
" /**\n" +
115+
" * @Route(\"/contact\", name=\"contact\", condition=\"<caret>\")\n" +
116+
" */\n" +
117+
" public function contact() {}\n" +
118+
"}",
119+
LANGUAGE_ID_EXPRESSION_LANGUAGE
120+
);
121+
122+
assertInjectedLangAtCaret(
123+
"<?php\n" +
124+
"\n" +
125+
"use Symfony\\Component\\Validator\\Constraints\\Expression;\n" +
126+
"\n" +
127+
"#[Expression(\"<caret>\")]\n" +
128+
"class BlogPost {}\n",
129+
LANGUAGE_ID_EXPRESSION_LANGUAGE
130+
);
131+
132+
assertInjectedLangAtCaret(
133+
"<?php\n" +
134+
"\n" +
135+
"use Symfony\\Component\\Validator\\Constraints\\Expression;\n" +
136+
"\n" +
137+
"/**\n" +
138+
" * @Expression(\"<caret>\")\n" +
139+
" */\n" +
140+
"class BlogPost {}\n",
141+
LANGUAGE_ID_EXPRESSION_LANGUAGE
142+
);
143+
144+
assertInjectedLangAtCaret(
145+
"<?php\n" +
146+
"\n" +
147+
"use Symfony\\Component\\Validator\\Constraints\\Expression;\n" +
148+
"\n" +
149+
"/**\n" +
150+
" * @Expression(expression: \"<caret>\")\n" +
151+
" */\n" +
152+
"class BlogPost {}\n",
153+
LANGUAGE_ID_EXPRESSION_LANGUAGE
154+
);
155+
156+
assertInjectedLangAtCaret(
157+
"<?php\n" +
158+
"\n" +
159+
"use Symfony\\Component\\Validator\\Constraints\\Expression;\n" +
160+
"\n" +
161+
"#[Expression(expression: \"<caret>\")]\n" +
162+
"class BlogPost {}\n",
163+
LANGUAGE_ID_EXPRESSION_LANGUAGE
164+
);
165+
166+
assertInjectedLangAtCaret(
167+
"<?php\n" +
168+
"\n" +
169+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Security;\n" +
170+
"\n" +
171+
"class ExampleController\n" +
172+
"{\n" +
173+
" #[Security(\"<caret>\")]\n" +
174+
" public function contact() {}\n" +
175+
"}",
176+
LANGUAGE_ID_EXPRESSION_LANGUAGE
177+
);
178+
179+
assertInjectedLangAtCaret(
180+
"<?php\n" +
181+
"\n" +
182+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Security;\n" +
183+
"\n" +
184+
"class ExampleController\n" +
185+
"{\n" +
186+
" #[Security(data: \"<caret>\")]\n" +
187+
" public function contact() {}\n" +
188+
"}",
189+
LANGUAGE_ID_EXPRESSION_LANGUAGE
190+
);
191+
192+
assertInjectedLangAtCaret(
193+
"<?php\n" +
194+
"\n" +
195+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Security;\n" +
196+
"\n" +
197+
"class ExampleController\n" +
198+
"{\n" +
199+
" /**\n" +
200+
" * @Security(\"<caret>\")\n" +
201+
" */\n" +
202+
" public function contact() {}\n" +
203+
"}",
204+
LANGUAGE_ID_EXPRESSION_LANGUAGE
205+
);
206+
207+
assertInjectedLangAtCaret(
208+
"<?php\n" +
209+
"\n" +
210+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Security;\n" +
211+
"\n" +
212+
"class ExampleController\n" +
213+
"{\n" +
214+
" /**\n" +
215+
" * @Security(expression=\"<caret>\")\n" +
216+
" */\n" +
217+
" public function contact() {}\n" +
218+
"}",
219+
LANGUAGE_ID_EXPRESSION_LANGUAGE
220+
);
221+
222+
assertInjectedLangAtCaret(
223+
"<?php\n" +
224+
"\n" +
225+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Cache;\n" +
226+
"\n" +
227+
"class ExampleController\n" +
228+
"{\n" +
229+
" #[Cache(lastModified: '<caret>')]\n" +
230+
" public function contact() {}\n" +
231+
"}",
232+
LANGUAGE_ID_EXPRESSION_LANGUAGE
233+
);
234+
235+
assertInjectedLangAtCaret(
236+
"<?php\n" +
237+
"\n" +
238+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Cache;\n" +
239+
"\n" +
240+
"class ExampleController\n" +
241+
"{\n" +
242+
" #[Cache(ETag: '<caret>')]\n" +
243+
" public function contact() {}\n" +
244+
"}",
245+
LANGUAGE_ID_EXPRESSION_LANGUAGE
246+
);
247+
248+
assertInjectedLangAtCaret(
249+
"<?php\n" +
250+
"\n" +
251+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Cache;\n" +
252+
"\n" +
253+
"class ExampleController\n" +
254+
"{\n" +
255+
" /**\n" +
256+
" * @Cache(lastModified=\"<caret>\")\n" +
257+
" */\n" +
258+
" public function contact() {}\n" +
259+
"}",
260+
LANGUAGE_ID_EXPRESSION_LANGUAGE
261+
);
262+
263+
assertInjectedLangAtCaret(
264+
"<?php\n" +
265+
"\n" +
266+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Cache;\n" +
267+
"\n" +
268+
"class ExampleController\n" +
269+
"{\n" +
270+
" /**\n" +
271+
" * @Cache(Etag=\"<caret>\")\n" +
272+
" */\n" +
273+
" public function contact() {}\n" +
274+
"}",
275+
LANGUAGE_ID_EXPRESSION_LANGUAGE
276+
);
277+
278+
assertInjectedLangAtCaret(
279+
"<?php\n" +
280+
"\n" +
281+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Entity;\n" +
282+
"\n" +
283+
"class ExampleController\n" +
284+
"{\n" +
285+
" #[Entity('post', expr: '<caret>')]\n" +
286+
" public function contact($post) {}\n" +
287+
"}",
288+
LANGUAGE_ID_EXPRESSION_LANGUAGE
289+
);
290+
291+
assertInjectedLangAtCaret(
292+
"<?php\n" +
293+
"\n" +
294+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Entity;\n" +
295+
"\n" +
296+
"class ExampleController\n" +
297+
"{\n" +
298+
" #[Entity('post', '<caret>')]\n" +
299+
" public function contact($post) {}\n" +
300+
"}",
301+
LANGUAGE_ID_EXPRESSION_LANGUAGE
302+
);
303+
304+
assertInjectedLangAtCaret(
305+
"<?php\n" +
306+
"\n" +
307+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Entity;\n" +
308+
"\n" +
309+
"class ExampleController\n" +
310+
"{\n" +
311+
" /**\n" +
312+
" * @Entity('post', expr=\"<caret>\")\n" +
313+
" */\n" +
314+
" public function contact($post) {}\n" +
315+
"}",
316+
LANGUAGE_ID_EXPRESSION_LANGUAGE
317+
);
318+
319+
assertInjectedLangAtCaret(
320+
"<?php $expr = \\Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\expr(\"<caret>\");\n",
321+
LANGUAGE_ID_EXPRESSION_LANGUAGE
322+
);
323+
324+
assertInjectedLangAtCaret(
325+
"<?php $expr = \\Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\expr(expression: \"<caret>\");",
326+
LANGUAGE_ID_EXPRESSION_LANGUAGE
327+
);
328+
}
329+
330+
private void assertInjectedLangAtCaret(@NotNull String configureByText, @NotNull String lang) {
331+
myFixture.configureByText(PhpFileType.INSTANCE, configureByText);
332+
injectionTestFixture.assertInjectedLangAtCaret(lang);
333+
}
334+
}

0 commit comments

Comments
 (0)