package fr.adrienbrault.idea.symfony2plugin.tests.doctrine;

import com.intellij.patterns.PlatformPatterns;
import com.jetbrains.php.lang.PhpFileType;
import com.jetbrains.php.lang.psi.elements.Method;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;

import java.io.File;

/**
 * @author Daniel Espendiller <daniel@espendiller.net>
 */
public class ObjectRepositoryTypeProviderTest extends SymfonyLightCodeInsightFixtureTestCase {

    public void setUp() throws Exception {
        super.setUp();
        myFixture.configureFromExistingVirtualFile(myFixture.copyFileToProject("classes.php"));
    }

    public String getTestDataPath() {
        return new File(this.getClass().getResource("fixtures").getFile()).getAbsolutePath();
    }

    /**
     * @see fr.adrienbrault.idea.symfony2plugin.doctrine.ObjectRepositoryTypeProvider
     */
    public void testGetRepositoryResolveByRepository() {
        assertPhpReferenceResolveTo(PhpFileType.INSTANCE,
            "<?php" +
                "/** @var \\Doctrine\\Common\\Persistence\\ObjectManager $em */\n" +
                "$em->getRepository('\\Foo\\Bar')->b<caret>ar();",
            PlatformPatterns.psiElement(Method.class).withName("bar")
        );

        assertPhpReferenceSignatureEquals(PhpFileType.INSTANCE,
            "<?php" +
                "/** @var \\Doctrine\\Common\\Persistence\\ObjectManager $em */\n" +
                "$em->getRepository('\\Foo\\Bar')->b<caret>ar();",
            "#M#" + '\u0151' + "#M#C\\Doctrine\\Common\\Persistence\\ObjectManager.getRepository" + '\u0180' + "\\Foo\\Bar.bar"
        );

        assertPhpReferenceResolveTo(PhpFileType.INSTANCE,
            "<?php" +
                "/** @var \\Doctrine\\Common\\Persistence\\ObjectManager $em */\n" +
                "$em->getRepository(\\Foo\\Bar::class)->b<caret>ar();",
            PlatformPatterns.psiElement(Method.class).withName("bar")
        );

        assertPhpReferenceSignatureEquals(PhpFileType.INSTANCE,
            "<?php" +
                "/** @var \\Doctrine\\Common\\Persistence\\ObjectManager $em */\n" +
                "$em->getRepository(\\Foo\\Bar::class)->b<caret>ar();",
            "#M#" + '\u0151' + "#M#C\\Doctrine\\Common\\Persistence\\ObjectManager.getRepository" + '\u0180' + "#K#C\\Foo\\Bar..bar"
        );
    }
}

@@ -50,24 +50,120 @@
 .doctrine;%0A%0A
+import com.intellij.ide.plugins.PluginManager;%0Aimport com.intellij.openapi.extensions.PluginId;%0A
 import com.i
@@ -2032,32 +2032,690 @@
 r%22)%0A        );%0A%0A
+    %7D%0A%0A    public void testGetRepositoryResolveByRepositoryApiClassConstantCompatibility() %7B%0A%0A        // Default api level%0A        // PhpStorm9 api: 141.2462%0A        String result = %22#M#%22 + '%5Cu0151' + %22#M#C%5C%5CDoctrine%5C%5CCommon%5C%5CPersistence%5C%5CObjectManager.getRepository%22 + '%5Cu0180' + %22#K#C%5C%5CFoo%5C%5CBar.class.bar%22;%0A%0A        // Old api issue%0A        // PhpStorm8 api: 141.1534%0A        if(Integer.parseInt(PluginManager.getPlugin(PluginId.getId(%22com.jetbrains.php%22)).getVersion().replace(%22.%22, %22%22)) %3C= 1411534) %7B%0A            result = %22#M#%22 + '%5Cu0151' + %22#M#C%5C%5CDoctrine%5C%5CCommon%5C%5CPersistence%5C%5CObjectManager.getRepository%22 + '%5Cu0180' + %22#K#C%5C%5CFoo%5C%5CBar..bar%22;%0A        %7D%0A%0A
         assertPh
@@ -2757,36 +2757,24 @@
 pe.INSTANCE,
-%0A           
  %22%3C?php%22 +%0A 
@@ -2945,128 +2945,14 @@
     
-%22#M#%22 + '%5Cu0151' + %22#M#C%5C%5CDoctrine%5C%5CCommon%5C%5CPersistence%5C%5CObjectManager.getRepository%22 + '%5Cu0180' + %22#K#C%5C%5CFoo%5C%5CBar..bar%22
+result
 %0A   

add api "class constant" conditional testing #541