com.jetbrains.php.lang.psi.PhpPsiElementFactory Java Examples

The following examples show how to use com.jetbrains.php.lang.psi.PhpPsiElementFactory. 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: 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 #2
Source File: RoutingRemoteFileStorage.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
public void build(@NotNull Project project, @NotNull Collection<FileObject> fileObjects) {
    Map<String, Route> routeMap = new HashMap<>();

    for (FileObject file : fileObjects) {

        String content;
        try {
            content = StreamUtil.readText(file.getContent().getInputStream(), "UTF-8");
        } catch (IOException e) {
            continue;
        }

        if(StringUtils.isBlank(content)) {
            continue;
        }

        routeMap.putAll(RouteHelper.getRoutesInsideUrlGeneratorFile(
            PhpPsiElementFactory.createPsiFileFromText(project, content)
        ));
    }

    this.routeMap = routeMap;
}
 
Example #3
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForVariableWithInvalidTemplateNameString() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$test = 'foo.html'\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render($test, $var += ['foobar1' => $myVar]);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertFalse(vars.containsKey("foobar"));
}
 
Example #4
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForVariable() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$test = 'foo.html.twig'\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render($test, $var += ['foobar1' => $myVar]);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
    assertContainsElements(vars.keySet(), "foobar1");
}
 
Example #5
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForCoalesce() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$test = 'foo.html.twig'\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render($foobar ?? $test, $var += ['foobar1' => $myVar]);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
    assertContainsElements(vars.keySet(), "foobar1");
}
 
Example #6
Source File: PhpElementsUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static void replaceElementWithClassConstant(@NotNull PhpClass phpClass, @NotNull PsiElement originElement) throws Exception{
    String fqn = phpClass.getFQN();
    if(!fqn.startsWith("\\")) {
        fqn = "\\" + fqn;
    }

    PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(originElement);
    if(scopeForUseOperator == null) {
        throw new Exception("Class fqn error");
    }

    if(!PhpCodeInsightUtil.getAliasesInScope(scopeForUseOperator).values().contains(fqn)) {
        PhpAliasImporter.insertUseStatement(fqn, scopeForUseOperator);
    }

    originElement.replace(PhpPsiElementFactory.createPhpPsiFromText(
        originElement.getProject(),
        ClassConstantReference.class,
        "<?php " + phpClass.getName() + "::class"
    ));
}
 
Example #7
Source File: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
public void testGetClassAnnotationAsUnknown() {
    Collection<Object[]> dataProvider = new ArrayList<Object[]>() {{
        add(new Object[] {"@Target()", AnnotationTarget.UNKNOWN});
        add(new Object[] {"@Target", AnnotationTarget.UNKNOWN});
        add(new Object[] {"", AnnotationTarget.UNDEFINED});
    }};

    for (Object[] objects: dataProvider) {
        PhpClass phpClass = PhpPsiElementFactory.createFromText(getProject(), PhpClass.class, "<?php\n" +
            "/**\n" +
            "* @Annotation\n" +
            "* "+ objects[0] +"\n" +
            "*/\n" +
            "class Foo {}\n"
        );

        PhpAnnotation classAnnotation = AnnotationUtil.getClassAnnotation(phpClass);
        List<AnnotationTarget> targets = classAnnotation.getTargets();

        assertContainsElements(targets, objects[1]);
    }
}
 
Example #8
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForTernary() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$myVar = new \\MyVars\\MyVar();\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render(true === true ? 'foo.html.twig' : 'foo', $var += ['foobar1' => $myVar]);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
    assertContainsElements(vars.keySet(), "foobar1");
}
 
Example #9
Source File: LegacyClassesForIdeQuickFix.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {

    PsiElement psiElement = descriptor.getPsiElement();
    if (DumbService.isDumb(project)) {
        showIsInDumpModeMessage(project, psiElement);
        return;
    }

    if (psiElement instanceof ClassReference) {
        ClassReference classReference = (ClassReference) psiElement;
        String fqn = classReference.getFQN();
        if (fqn != null) {
            String replacementFQN = LegacyClassesForIDEIndex.findReplacementClass(project, fqn);
            if (replacementFQN != null) {
                try {
                    classReference.replace(PhpPsiElementFactory.createClassReference(project, replacementFQN));
                } catch (IncorrectOperationException e) {
                    showErrorMessage(project, "Could not replace class reference", psiElement);
                }
            }
        }
    }
}
 
Example #10
Source File: ServiceLineMarkerProviderTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public void testXmlServiceLineMarker() {
    myFixture.configureByText(XmlFileType.INSTANCE,
        "<container>\n" +
        "  <services>\n" +
        "      <service class=\"Service\\Bar\" id=\"service_bar\"/>\n" +
        "  </services>\n" +
        "</container>"
    );

    assertLineMarker(PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
            "namespace Service{\n" +
            "    class Bar{}\n" +
            "}"
    ), new LineMarker.ToolTipEqualsAssert("Navigate to definition"));

    assertLineMarker(PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
            "namespace Service{\n" +
            "    class Bar{}\n" +
            "}"
    ), new LineMarker.TargetAcceptsPattern("Navigate to definition", XmlPatterns.xmlTag().withName("service").withAttributeValue("id", "service_bar")));
}
 
Example #11
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 #12
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 #13
Source File: ServiceLineMarkerProviderTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public void testDoctrineRepositoryDefinitionLineMarker() {
    myFixture.configureByText(XmlFileType.INSTANCE, "" +
            "<doctrine-mapping>\n" +
            "    <document name=\"Foo\" repository-class=\"Entity\\Bar\"/>\n" +
            "</doctrine-mapping>"
    );

    myFixture.configureByText(PhpFileType.INSTANCE, "<?php\n" +
            "namespace Entity{\n" +
            "    class Bar{}\n" +
            "}"
    );

    assertLineMarker(PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
            "namespace Entity{\n" +
            "    class Bar{}\n" +
            "}"
    ), new LineMarker.ToolTipEqualsAssert("Navigate to metadata"));
}
 
Example #14
Source File: ServiceLineMarkerProviderTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public void testXmlServiceLineMarkerForClassName() {
    myFixture.configureByText(XmlFileType.INSTANCE,
        "<container>\n" +
            "  <services>\n" +
            "      <service id=\"Service\\Bar\"/>\n" +
            "  </services>\n" +
            "</container>"
    );

    assertLineMarker(PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
        "namespace Service{\n" +
        "    class Bar{}\n" +
        "}"
    ), new LineMarker.ToolTipEqualsAssert("Navigate to definition"));

    assertLineMarker(PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
        "namespace Service{\n" +
        "    class Bar{}\n" +
        "}"
    ), new LineMarker.TargetAcceptsPattern("Navigate to definition", XmlPatterns.xmlTag().withName("service").withAttributeValue("id", "Service\\Bar")));
}
 
Example #15
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForOperatorSelfAssignment() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$myVar = new \\MyVars\\MyVar();\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render('foo.html.twig', $var += ['foobar1' => $myVar]);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
    assertContainsElements(vars.keySet(), "foobar1");
}
 
Example #16
Source File: DoctrineUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see DoctrineUtil#getClassRepositoryPair
 */
public void testGetClassRepositoryPairForClassConstant() {
    myFixture.configureByText(PhpFileType.INSTANCE, "<?php class Foobar {};");

    PsiFile psiFileFromText = PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
        "\n" +
        "namespace Foo;\n" +
        "\n" +
        "use Doctrine\\ORM\\Mapping as ORM;\n" +
        "use Foobar;\n" +
        "\n" +
        "/**\n" +
        " * @ORM\\Entity(repositoryClass=Foobar::class)\n" +
        " */\n" +
        "class Apple {\n" +
        "}\n"
    );

    Collection<Pair<String, String>> classRepositoryPair = DoctrineUtil.getClassRepositoryPair(psiFileFromText);

    Pair<String, String> next = classRepositoryPair.iterator().next();

    assertEquals("Foo\\Apple", next.getFirst());
    assertEquals("Foobar", next.getSecond());
}
 
Example #17
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForBinaryExpression() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$myVar = new \\MyVars\\MyVar();\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render('foo.html.twig', $var + ['foobar1' => $myVar]);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
    assertContainsElements(vars.keySet(), "foobar1");
}
 
Example #18
Source File: FormUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@SuppressWarnings({"ConstantConditions"})
public void testGetFormTypeClassOnParameter() {
    assertEquals("\\Form\\FormType\\Foo", FormUtil.getFormTypeClassOnParameter(
        PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpTypedElementImpl.class, "<?php new \\Form\\FormType\\Foo();")
    ).getFQN());

    assertEquals("\\Form\\FormType\\Foo", FormUtil.getFormTypeClassOnParameter(
        PhpPsiElementFactory.createPhpPsiFromText(getProject(), StringLiteralExpressionImpl.class, "<?php '\\Form\\FormType\\Foo'")
    ).getFQN());

    assertEquals("\\Form\\FormType\\Foo", FormUtil.getFormTypeClassOnParameter(
        PhpPsiElementFactory.createPhpPsiFromText(getProject(), StringLiteralExpressionImpl.class, "<?php 'Form\\FormType\\Foo'")
    ).getFQN());

    assertEquals("\\Form\\FormType\\Foo", FormUtil.getFormTypeClassOnParameter(
        PhpPsiElementFactory.createPhpPsiFromText(getProject(), ClassConstantReferenceImpl.class, "<?php Form\\FormType\\Foo::class")
    ).getFQN());
}
 
Example #19
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForArrayMerge() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$myVar = new \\MyVars\\MyVar();\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render('foo.html.twig', array_merge($var, ['foobar1' => $myVar]));\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
    assertContainsElements(vars.keySet(), "foobar1");
}
 
Example #20
Source File: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
public void testGetUseImportMap() {
    PhpDocTag phpDocTag = PhpPsiElementFactory.createFromText(getProject(), PhpDocTag.class, "<?php\n" +
        "use Foobar;\n" +
        "use Bar as MyFoo" +
        "\n" +
        "/**\n" +
        " * @Foo()\n" +
        " **/\n" +
        "class Foo() {}\n"
    );

    Map<String, String> propertyForEnum = AnnotationUtil.getUseImportMap((PsiElement) phpDocTag);

    assertEquals("\\Foobar", propertyForEnum.get("Foobar"));
    assertEquals("\\Bar", propertyForEnum.get("MyFoo"));
}
 
Example #21
Source File: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
public void testIsAnnotationClass() {
    assertTrue(AnnotationUtil.isAnnotationClass(
        PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
        "/**\n" +
        "* @Annotation\n" +
        "*/\n" +
        "class Foo() {}\n"
    )));

    assertTrue(AnnotationUtil.isAnnotationClass(
        PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
        "/**\n" +
        "* @Annotation()\n" +
        "*/\n" +
        "class Foo() {}\n"
    )));

    assertFalse(AnnotationUtil.isAnnotationClass(
        PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
        "/**\n" +
        "* @Foo\n" +
        "*/\n" +
        "class Foo() {}\n"
    )));
}
 
Example #22
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForArrayCreation() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$myVar = new \\MyVars\\MyVar();\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render('foo.html.twig', ['foobar' => $myVar]);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
}
 
Example #23
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForVariablesReferences() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$myVar = new \\MyVars\\MyVar();\n" +
        "$var['foobar'] = $myVar;\n" +
        "$var = ['foobar1' => $myVar];\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render('foo.html.twig', $var);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
    assertContainsElements(vars.keySet(), "foobar1");
}
 
Example #24
Source File: ServiceLineMarkerProviderTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public void testYamlServiceLineMarker() {
    myFixture.configureByText(YAMLFileType.YML,
        "services:\n" +
            "  foo:\n" +
            "    class: Service\\YamlBar"
    );

    assertLineMarker(PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
        "namespace Service{\n" +
        "    class YamlBar{}\n" +
        "}"
    ), new LineMarker.TargetAcceptsPattern("Navigate to definition", PlatformPatterns.psiElement(YAMLKeyValue.class).with(new PatternCondition<YAMLKeyValue>("KeyText") {
        @Override
        public boolean accepts(@NotNull YAMLKeyValue yamlKeyValue, ProcessingContext processingContext) {
            return yamlKeyValue.getKeyText().equals("foo");
        }
    })));
}
 
Example #25
Source File: ServiceLineMarkerProviderTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public void testYamlServiceLineMarkerForClassName() {
    myFixture.configureByText(YAMLFileType.YML,
        "services:\n" +
            "  Service\\YamlBar: ~\n"
    );

    assertLineMarker(PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
        "namespace Service{\n" +
        "    class YamlBar{}\n" +
        "}"
    ), new LineMarker.TargetAcceptsPattern("Navigate to definition", PlatformPatterns.psiElement(YAMLKeyValue.class).with(new PatternCondition<YAMLKeyValue>("KeyText") {
        @Override
        public boolean accepts(@NotNull YAMLKeyValue yamlKeyValue, ProcessingContext processingContext) {
            return yamlKeyValue.getKeyText().equals("Service\\YamlBar");
        }
    })));
}
 
Example #26
Source File: PhpElementsUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil#insertUseIfNecessary
 */
public void testUseImports() {
    assertEquals("Bar", PhpElementsUtil.insertUseIfNecessary(PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
        "namespace Foo;\n" +
        "class Foo{}\n"
    ), "\\Foo\\Bar"));

    assertEquals("Bar", PhpElementsUtil.insertUseIfNecessary(PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
        "namespace Foo;\n" +
        "use Foo\\Bar;\n" +
        "class Foo{}\n"
    ), "\\Foo\\Bar"));

    assertEquals("Apple", PhpElementsUtil.insertUseIfNecessary(PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
        "namespace Bar\\Bar;\n" +
        "use Foo as Car;\n" +
        "class Foo{}\n"
    ), "Foo\\Cool\\Apple"));
}
 
Example #27
Source File: DoctrineUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see DoctrineUtil#getClassRepositoryPair
 */
public void testGetClassRepositoryPairForStringValue() {
    PsiFile psiFileFromText = PhpPsiElementFactory.createPsiFileFromText(getProject(), "" +
        "<?php\n" +
        "\n" +
        "namespace Foo;\n" +
        "\n" +
        "use Doctrine\\ORM\\Mapping as ORM;\n" +
        "\n" +
        "/**\n" +
        " * @ORM\\Entity(repositoryClass=\"MyBundle\\Entity\\Repository\\AddressRepository\")\n" +
        " */\n" +
        "class Apple {\n" +
        "}\n"
    );

    Collection<Pair<String, String>> classRepositoryPair = DoctrineUtil.getClassRepositoryPair(psiFileFromText);

    Pair<String, String> next = classRepositoryPair.iterator().next();

    assertEquals("Foo\\Apple", next.getFirst());
    assertEquals("MyBundle\\Entity\\Repository\\AddressRepository", next.getSecond());
}
 
Example #28
Source File: AnnotationUsageLineMarkerProviderTest.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void testThatLineMarkerIsProvidedForAnnotationClass() {
    assertLineMarker(PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
        "namespace Doctrine\\ORM\\Mapping;\n" +
        "" +
        "{\n" +
        "   /**\n" +
        "   * @Annotation\n" +
        "   */\n" +
        "   class Embedded\n" +
        "   {\n" +
        "   }\n" +
        "}"
    ), new LineMarker.ToolTipEqualsAssert("Navigate to implementations"));
}
 
Example #29
Source File: ServiceLineMarkerProviderTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void testThatAutowireConstructorIsGivenALineMarker() {
    myFixture.configureByText(YAMLFileType.YML,
        "services:\n" +
            "  Service\\YamlBar: " +
            "       autowire: true\n\n"
    );

    assertLineMarker(PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
        "namespace Service {\n" +
        "    class YamlBar {\n" +
        "       function __construct() {}\n" +
        "   }\n" +
        "}"
    ), markerInfo -> markerInfo.getLineMarkerTooltip() != null && markerInfo.getLineMarkerTooltip().toLowerCase().contains("autowire"));

    myFixture.configureByText(YAMLFileType.YML,
        "services:\n" +
            "  _defaults:\n" +
            "    autowire: true\n" +
            "" +
            "  Service\\YamlBarDefault: ~\n"
    );

    assertLineMarker(PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
        "namespace Service {\n" +
        "    class YamlBarDefault {\n" +
        "       function __construct() {}\n" +
        "   }\n" +
        "}"
    ), markerInfo -> markerInfo.getLineMarkerTooltip() != null && markerInfo.getLineMarkerTooltip().toLowerCase().contains("autowire"));
}
 
Example #30
Source File: PhpElementsUtilTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil#getMethodParameterTypeHint
 */
public void testGetMethodParameterClassHint() {
    assertEquals("\\DateTime", PhpElementsUtil.getMethodParameterTypeHint(
        PhpPsiElementFactory.createMethod(getProject(), "function foo(\\DateTime $e) {}")
    ));

    assertEquals("\\Iterator", PhpElementsUtil.getMethodParameterTypeHint(
        PhpPsiElementFactory.createMethod(getProject(), "function foo(/* foo */ \\Iterator $a, \\DateTime $b")
    ));

    assertNull(PhpElementsUtil.getMethodParameterTypeHint(
        PhpPsiElementFactory.createMethod(getProject(), "function foo(/* foo */ $a, \\DateTime $b")
    ));
}