Java Code Examples for com.jetbrains.php.lang.psi.PhpPsiElementFactory#createPhpPsiFromText()

The following examples show how to use com.jetbrains.php.lang.psi.PhpPsiElementFactory#createPhpPsiFromText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenericsUtilTest.java    From idea-php-generics-plugin with MIT License 6 votes vote down vote up
public void testThatParamArrayElementsAreExtracted() {
    PhpDocComment phpDocComment = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpDocComment.class, "" +
        "/**\n" +
        "* @psalm-param array{foo: Foo, ?bar: int | string} $foobar\n" +
        "*/\n" +
        "function test($foobar) {}\n"
    );

    PhpDocTag[] tagElementsByName = phpDocComment.getTagElementsByName("@psalm-param");
    String tagValue = tagElementsByName[0].getTagValue();

    Collection<ParameterArrayType> vars = GenericsUtil.getParameterArrayTypes(tagValue, "foobar", tagElementsByName[0]);

    ParameterArrayType foo = Objects.requireNonNull(vars).stream().filter(parameterArrayType -> parameterArrayType.getKey().equalsIgnoreCase("foo")).findFirst().get();
    assertFalse(foo.isOptional());
    assertContainsElements(Collections.singletonList("Foo"), foo.getValues());

    ParameterArrayType foobar = Objects.requireNonNull(vars).stream().filter(parameterArrayType -> parameterArrayType.getKey().equalsIgnoreCase("bar")).findFirst().get();
    assertTrue(foobar.isOptional());
    assertContainsElements(Arrays.asList("string", "int"), foobar.getValues());
}
 
Example 2
Source File: GenericsUtilTest.java    From idea-php-generics-plugin with MIT License 6 votes vote down vote up
public void testThatReturnElementsAreExtracted() {
    Function function = PhpPsiElementFactory.createPhpPsiFromText(getProject(), Function.class, "" +
        "/**\n" +
        "* @psalm-return array{foo: Foo, ?bar: int | string}\n" +
        "* @return array{foo2: Foo, ?bar2: int | string}\n" +
        "*/" +
        "function test() {}\n"
    );

    Collection<ParameterArrayType> vars = GenericsUtil.getReturnArrayTypes(function);

    ParameterArrayType bar = Objects.requireNonNull(vars).stream().filter(parameterArrayType -> parameterArrayType.getKey().equalsIgnoreCase("bar")).findFirst().get();
    assertTrue(bar.isOptional());
    assertContainsElements(Arrays.asList("string", "int"), bar.getValues());

    ParameterArrayType bar2 = Objects.requireNonNull(vars).stream().filter(parameterArrayType -> parameterArrayType.getKey().equalsIgnoreCase("bar2")).findFirst().get();
    assertTrue(bar2.isOptional());
    assertContainsElements(Arrays.asList("string", "int"), bar2.getValues());
}
 
Example 3
Source File: RouteHelperTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper#convertMethodToRouteShortcutControllerName
 */
public void testConvertMethodToRouteShortcutControllerForInvoke()
{
    myFixture.configureFromExistingVirtualFile(myFixture.copyFileToProject("GetRoutesOnControllerAction.php"));

    PhpClass phpClass = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
        "namespace FooBar\\FooBundle\\Controller" +
        "{\n" +
        "  class FooBarController\n" +
        "  {\n" +
        "     function __invoke() {}\n" +
        "  }\n" +
        "}"
    );

    Method fooAction = phpClass.findMethodByName("__invoke");
    assertNotNull(fooAction);

    assertEquals("FooBar\\FooBundle\\Controller\\FooBarController", RouteHelper.convertMethodToRouteShortcutControllerName(fooAction));
}
 
Example 4
Source File: FormUtilTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void testGetFormAliases() {
    PhpClass phpClass = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
            "class Foo implements \\Symfony\\Component\\Form\\FormTypeInterface {\n" +
            "  public function getName()" +
            "  {\n" +
            "    return 'bar';\n" +
            "    return 'foo';\n" +
            "  }\n" +
            "}"
    );

    assertContainsElements(Arrays.asList("bar", "foo"), FormUtil.getFormAliases(phpClass));
}
 
Example 5
Source File: FormUtilTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void testGetFormAliasesPhpClassNotImplementsInterfaceAndShouldBeEmpty() {
    PhpClass phpClass = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
            "class Foo {\n" +
            "  public function getName()" +
            "  {\n" +
            "    return 'bar';\n" +
            "  }\n" +
            "}"
    );

    assertSize(0, FormUtil.getFormAliases(phpClass));
}
 
Example 6
Source File: RouteHelperTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper#convertMethodToRouteShortcutControllerName
 */
public void testConvertMethodToRouteShortcutControllerName() {
    myFixture.configureFromExistingVirtualFile(myFixture.copyFileToProject("GetRoutesOnControllerAction.php"));

    PhpClass phpClass = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
        "namespace FooBar\\FooBundle\\Controller" +
        "{\n" +
        "  class FooBarController\n" +
        "  {\n" +
        "     function fooAction() {}\n" +
        "  }\n" +
        "}"
    );

    Method fooAction = phpClass.findMethodByName("fooAction");
    assertNotNull(fooAction);

    assertEquals("FooBarFooBundle:FooBar:foo", RouteHelper.convertMethodToRouteShortcutControllerName(fooAction));

    phpClass = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
        "namespace FooBar\\FooBundle\\Controller\\SubFolder" +
        "{\n" +
        "  class SubController\n" +
        "  {\n" +
        "     function fooAction() {}\n" +
        "  }\n" +
        "}"
    );

    fooAction = phpClass.findMethodByName("fooAction");
    assertNotNull(fooAction);

    assertEquals("FooBarFooBundle:SubFolder\\Sub:foo", RouteHelper.convertMethodToRouteShortcutControllerName(fooAction));
}
 
Example 7
Source File: TwigUtilTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void testGetTemplateAnnotationFilesWithSiblingMethod() {
    PhpDocTag phpDocTag = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpDocTag.class, "<?php\n" +
        "class Foo\n" +
        "{\n" +
        "   /**" +
        "   * @Template(\"foo.html.twig\")" +
        "   */" +
        "   function fooAction()\n" +
        "   {\n" +
        "   }\n" +
        "}\n"
    );

    assertContainsElements(TwigUtil.getTemplateAnnotationFilesWithSiblingMethod(phpDocTag).keySet(), "foo.html.twig");
}
 
Example 8
Source File: TwigUtilTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void testGetTemplateAnnotationFiles() {
    PhpDocTag phpPsiFromText = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpDocTag.class, "/** @Template(\"foo.html.twig\") */");
    assertEquals("foo.html.twig", TwigUtil.getTemplateAnnotationFiles(phpPsiFromText).getFirst());

    phpPsiFromText = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpDocTag.class, "/** @Template(template=\"foo.html.twig\") */");
    assertEquals("foo.html.twig", TwigUtil.getTemplateAnnotationFiles(phpPsiFromText).getFirst());

    phpPsiFromText = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpDocTag.class, "/** @Template(template=\"foo\\foo.html.twig\") */");
    assertEquals("foo/foo.html.twig", TwigUtil.getTemplateAnnotationFiles(phpPsiFromText).getFirst());
}
 
Example 9
Source File: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void testGetPropertyValueOrDefault() {
    Collection<String[]> dataProvider = new ArrayList<String[]>() {{
        add(new String[] {"/** @Template(\"Foobar\") */", "property", "Foobar"});
        add(new String[] {"/** @Template(name=\"Foo\") */", "name", "Foo"});
        add(new String[] {"/** @Template(\"Foobar\", name=\"Foo\") */", "name", "Foo"});
        add(new String[] {"/** @Template(\"Foobar\", foo=\"Foo\") */", "name", "Foobar"});
        add(new String[] {"/** @Template() */", "property", null});
    }};

    for (String[] strings : dataProvider) {
        PhpDocTag phpDocTag = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpDocTag.class, "<?php\n" + strings[0]);
        assertEquals(strings[2], AnnotationUtil.getPropertyValueOrDefault(phpDocTag, strings[1]));
    }
}
 
Example 10
Source File: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void testGetPropertyValue() {
    Collection<String[]> dataProvider = new ArrayList<String[]>() {{
        add(new String[] {"/** @Template(\"Foobar\") */", "property", null});
        add(new String[] {"/** @Template(name=\"Foo\") */", "name", "Foo"});
        add(new String[] {"/** @Template(\"Foobar\", name=\"Foo\") */", "name", "Foo"});
        add(new String[] {"/** @Template(\"Foobar\", foo=\"Foo\") */", "name", null});
        add(new String[] {"/** @Template(\"Foobar\", foo=FOO::class) */", "foo", null});
    }};

    for (String[] strings : dataProvider) {
        PhpDocTag phpDocTag = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpDocTag.class, "<?php\n" + strings[0]);
        assertEquals(strings[2], AnnotationUtil.getPropertyValue(phpDocTag, strings[1]));
    }
}
 
Example 11
Source File: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void testGetPropertyValueAsPsiElement() {
    PhpDocTag phpDocTag = PhpPsiElementFactory.createPhpPsiFromText(
        getProject(),
        PhpDocTag.class, "<?php\n /** @Template(\"Foobar\", name=\"Foo\") */"
    );

    assertEquals("Foo", AnnotationUtil.getPropertyValueAsPsiElement(phpDocTag, "name").getContents());
}
 
Example 12
Source File: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void testGetPropertyValueAsElement() {
    PhpDocTag phpDocTag = PhpPsiElementFactory.createPhpPsiFromText(
        getProject(),
        PhpDocTag.class, "<?php\n /** @Template(\"Foobar\", name=\"Foo\") */"
    );

    assertEquals("Foo", ((StringLiteralExpression) AnnotationUtil.getPropertyValueAsElement(phpDocTag, "name")).getContents());
}
 
Example 13
Source File: FormUtilTest.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public void testGetFormParentOfPhpClass() {
    PhpClass phpClass = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
            "class Foo {\n" +
            "  public function getParent()" +
            "  {\n" +
            "    return 'bar';\n" +
            "  }\n" +
            "}"
    );

    assertContainsElements(FormUtil.getFormParentOfPhpClass(phpClass), "bar");

    phpClass = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
            "namespace My\\Bar {\n" +
            "  class Foo {\n" +
            "    public function getParent()" +
            "    {\n" +
            "      return __NAMESPACE__ . '\\Foo';\n" +
            "    }\n" +
            "  }\n" +
            "}"
    );

    assertContainsElements(FormUtil.getFormParentOfPhpClass(phpClass), "My\\Bar\\Foo");

    phpClass = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
            "namespace My\\Bar {\n" +
            "  class Bar() {}\n" +
            "  class Foo {\n" +
            "    public function getParent()" +
            "    {\n" +
            "      return Bar::class;\n" +
            "    }\n" +
            "  }\n" +
            "}"
    );

    assertContainsElements(FormUtil.getFormParentOfPhpClass(phpClass), "My\\Bar\\Bar");

    phpClass = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
        "namespace My\\Bar {\n" +
        "  class Bar() {}\n" +
        "  class Foo {\n" +
        "    public function getParent()" +
        "    {\n" +
        "      return true ? Bar::class : 'foobar';\n" +
        "    }\n" +
        "  }\n" +
        "}"
    );

    assertContainsElements(FormUtil.getFormParentOfPhpClass(phpClass), "My\\Bar\\Bar", "foobar");
}
 
Example 14
Source File: RouteHelperTest.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper#getRoutesOnControllerAction
 */
public void testGetRoutesOnControllerAction() {
    myFixture.copyFileToProject("GetRoutesOnControllerAction.php");
    myFixture.copyFileToProject("GetRoutesOnControllerAction.routing.xml");
    myFixture.copyFileToProject("GetRoutesOnControllerAction.services.xml");

    PhpClass phpClass = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
        "namespace FooBar\\FooBundle\\Controller\\SubFolder" +
        "{\n" +
        "  class FooBarController\n" +
        "  {\n" +
        "     function fooAction() {}\n" +
        "  }\n" +
        "}"
    );

    Method fooAction = phpClass.findMethodByName("fooAction");
    assertNotNull(fooAction);

    List<Route> routesOnControllerAction = RouteHelper.getRoutesOnControllerAction(fooAction);

    assertNotNull(ContainerUtil.find(routesOnControllerAction, route ->
        "xml_route_subfolder_backslash".equals(route.getName())
    ));

    assertNotNull(ContainerUtil.find(routesOnControllerAction, route ->
        "xml_route_subfolder_slash".equals(route.getName())
    ));

    assertNotNull(ContainerUtil.find(routesOnControllerAction, route ->
        "xml_route_subfolder_class_syntax".equals(route.getName())
    ));

    // controller as service
    Method indexAction = PhpElementsUtil.getClassMethod(getProject(), "Service\\Controller\\FooController", "indexAction");
    assertNotNull(indexAction);

    assertNotNull(ContainerUtil.find(RouteHelper.getRoutesOnControllerAction(indexAction), route ->
        "xml_route_as_service".equals(route.getName())
    ));
}