-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from pestphp/feat-test-finder
feat(goto): Navigate between test and implementation
- Loading branch information
Showing
21 changed files
with
278 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.pestphp.pest.goto | ||
|
||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiManager | ||
import com.intellij.psi.search.GlobalSearchScope | ||
import com.intellij.psi.util.PsiTreeUtil | ||
import com.intellij.testIntegration.TestFinder | ||
import com.intellij.util.indexing.FileBasedIndex | ||
import com.jetbrains.php.PhpIndex | ||
import com.jetbrains.php.lang.psi.elements.PhpClass | ||
import com.pestphp.pest.indexers.PestTestIndex | ||
import com.pestphp.pest.isPestTestFile | ||
import java.util.ArrayList | ||
|
||
class PestTestFinder : TestFinder { | ||
override fun findClassesForTest(element: PsiElement): MutableCollection<PhpClass> { | ||
return PhpIndex.getInstance(element.project) | ||
.getClassesByNameInScope( | ||
element.containingFile.name.removeSuffix("Test.php"), | ||
GlobalSearchScope.projectScope(element.project) | ||
) | ||
} | ||
|
||
override fun findSourceElement(from: PsiElement): PsiElement? { | ||
return from.containingFile | ||
} | ||
|
||
override fun isTest(element: PsiElement): Boolean { | ||
return element.containingFile.isPestTestFile() | ||
} | ||
|
||
override fun findTestsForClass(element: PsiElement): MutableCollection<PsiElement> { | ||
val phpClass = PsiTreeUtil.getNonStrictParentOfType(element, PhpClass::class.java) ?: return arrayListOf() | ||
|
||
return FileBasedIndex.getInstance().getAllKeys( | ||
PestTestIndex.key, | ||
element.project | ||
).filter { it.contains(phpClass.name) } | ||
.flatMap { | ||
FileBasedIndex.getInstance().getContainingFiles( | ||
PestTestIndex.key, | ||
it, | ||
GlobalSearchScope.projectScope(element.project) | ||
) | ||
} | ||
.map { PsiManager.getInstance(element.project).findFile(it)!! } | ||
.toCollection(ArrayList()) | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/kotlin/com/pestphp/pest/indexers/PestTestIndex.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.pestphp.pest.indexers | ||
|
||
import com.intellij.openapi.project.ProjectManager | ||
import com.intellij.openapi.roots.TestSourcesFilter | ||
import com.intellij.openapi.vfs.VirtualFile | ||
import com.intellij.util.indexing.DataIndexer | ||
import com.intellij.util.indexing.DefaultFileTypeSpecificInputFilter | ||
import com.intellij.util.indexing.FileBasedIndex | ||
import com.intellij.util.indexing.FileContent | ||
import com.intellij.util.indexing.ID | ||
import com.intellij.util.indexing.ScalarIndexExtension | ||
import com.intellij.util.io.EnumeratorStringDescriptor | ||
import com.intellij.util.io.KeyDescriptor | ||
import com.jetbrains.php.lang.PhpFileType | ||
import com.pestphp.pest.isPestTestFile | ||
import gnu.trove.THashMap | ||
|
||
class PestTestIndex : ScalarIndexExtension<String>() { | ||
override fun getName(): ID<String, Void> { | ||
return key | ||
} | ||
|
||
override fun getVersion(): Int { | ||
return 0 | ||
} | ||
|
||
override fun dependsOnFileContent(): Boolean { | ||
return true | ||
} | ||
|
||
override fun getIndexer(): DataIndexer<String, Void, FileContent> { | ||
return DataIndexer { inputData -> | ||
val file = inputData.psiFile | ||
|
||
if (!file.isPestTestFile()) { | ||
return@DataIndexer mapOf() | ||
} | ||
|
||
val map = THashMap<String, Void>() | ||
map[file.name] = null | ||
return@DataIndexer map | ||
} | ||
} | ||
|
||
override fun getInputFilter(): FileBasedIndex.InputFilter { | ||
return object : DefaultFileTypeSpecificInputFilter(PhpFileType.INSTANCE) { | ||
override fun acceptInput(file: VirtualFile): Boolean { | ||
if (file.path.contains(""".*?test.*?/.*\..*""".toRegex())) { | ||
return true | ||
} | ||
|
||
return ProjectManager.getInstance().openProjects.any { | ||
TestSourcesFilter.isTestSources(file, it) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun getKeyDescriptor(): KeyDescriptor<String> { | ||
return EnumeratorStringDescriptor.INSTANCE | ||
} | ||
|
||
companion object { | ||
val key = ID.create<String, Void>("php.pest") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/test/kotlin/com/pestphp/pest/PestUtil/IsPestTestFileTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.pestphp.pest.PestUtil | ||
|
||
import com.pestphp.pest.isPestTestFile | ||
import com.pestphp.pest.isPestTestReference | ||
import com.pestphp.pest.tests.PestLightCodeFixture | ||
|
||
class IsPestTestFileTest : PestLightCodeFixture() { | ||
override fun getTestDataPath(): String? { | ||
return "src/test/resources/com/pestphp/pest/PestUtil" | ||
} | ||
|
||
fun testMethodCallNamedTestIsNotPestTest() { | ||
val file = myFixture.configureByFile("MethodCallNamedTest.php") | ||
|
||
assertFalse(file.isPestTestFile()) | ||
} | ||
|
||
fun testMethodCallNamedItIsNotPestTest() { | ||
val file = myFixture.configureByFile("MethodCallNamedIt.php") | ||
|
||
assertFalse(file.isPestTestFile()) | ||
} | ||
|
||
fun testFunctionCallNamedItWithDescriptionAndClosure() { | ||
val file = myFixture.configureByFile("PestItFunctionCallWithDescriptionAndClosure.php") | ||
|
||
assertTrue(file.isPestTestFile()) | ||
} | ||
|
||
fun testFunctionCallNamedItWithDescriptionAndHigherOrder() { | ||
val file = myFixture.configureByFile("PestItFunctionCallWithDescriptionAndHigherOrder.php") | ||
|
||
assertTrue(file.isPestTestFile()) | ||
} | ||
|
||
fun testFunctionCallNamedTestWithDescriptionAndHigherOrder() { | ||
val file = myFixture.configureByFile("PestTestFunctionCallWithDescriptionAndHigherOrder.php") | ||
|
||
assertTrue(file.isPestTestFile()) | ||
} | ||
|
||
fun testMethodCallNamedItAndVariableTestIsNotPestTest() { | ||
val file = myFixture.configureByFile("MethodCallNamedItAndVariableTest.php") | ||
|
||
assertFalse(file.isPestTestFile()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/test/kotlin/com/pestphp/pest/goto/PestTestFinderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.pestphp.pest.goto | ||
|
||
import com.jetbrains.php.lang.psi.PhpFile | ||
import com.jetbrains.php.lang.psi.PhpPsiUtil | ||
import com.pestphp.pest.tests.PestLightCodeFixture | ||
import junit.framework.TestCase | ||
|
||
class PestTestFinderTest : PestLightCodeFixture() { | ||
override fun getTestDataPath(): String? { | ||
return "src/test/resources/com/pestphp/pest/goto/PestTestFinder" | ||
} | ||
|
||
fun testPestTestIsTest() { | ||
val file = myFixture.configureByFile("test/App/UserTest.php") | ||
|
||
val testElement = file.firstChild.lastChild.firstChild | ||
|
||
assertTrue(PestTestFinder().isTest(testElement)) | ||
} | ||
|
||
fun testFileIsTest() { | ||
val file = myFixture.configureByFile("test/App/UserTest.php") | ||
|
||
assertTrue(PestTestFinder().isTest(file)) | ||
} | ||
|
||
fun testRandomElementIsTest() { | ||
val file = myFixture.configureByFile("test/App/UserTest.php") | ||
|
||
assertTrue(PestTestFinder().isTest(file.firstChild.children.random())) | ||
} | ||
|
||
fun testCanFindSourceElement() { | ||
val file = myFixture.configureByFile("App/User.php") | ||
|
||
TestCase.assertSame( | ||
file, | ||
PestTestFinder().findSourceElement( | ||
PhpPsiUtil.findAllClasses(file as PhpFile).first().methods.first() | ||
) | ||
) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/kotlin/com/pestphp/pest/indexers/PestTestIndexTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.pestphp.pest.indexers | ||
|
||
import com.intellij.psi.search.GlobalSearchScope | ||
import com.intellij.util.indexing.FileBasedIndex | ||
import com.pestphp.pest.tests.PestLightCodeFixture | ||
|
||
class PestTestIndexTest : PestLightCodeFixture() { | ||
override fun getTestDataPath(): String? { | ||
return "src/test/resources/com/pestphp/pest/indexers/PestTestIndexTest" | ||
} | ||
|
||
fun testPestTestFileIsIndexed() { | ||
myFixture.copyFileToProject("FileWithPestTest.php", "tests/FileWithPestTest.php") | ||
|
||
val fileBasedIndex = FileBasedIndex.getInstance() | ||
|
||
val indexKeys = fileBasedIndex.getAllKeys(PestTestIndex.key, project).filter { | ||
fileBasedIndex.getContainingFiles(PestTestIndex.key, it, GlobalSearchScope.allScope(project)).isNotEmpty() | ||
} | ||
|
||
assertContainsElements(indexKeys, "FileWithPestTest.php") | ||
} | ||
|
||
fun testPhpFileIsNotIndexed() { | ||
myFixture.copyFileToProject("FileWithoutPestTest.php", "tests/FileWithoutPestTest.php") | ||
|
||
val fileBasedIndex = FileBasedIndex.getInstance() | ||
|
||
val indexKeys = fileBasedIndex.getAllKeys(PestTestIndex.key, project).filter { | ||
fileBasedIndex.getContainingFiles(PestTestIndex.key, it, GlobalSearchScope.allScope(project)).isNotEmpty() | ||
} | ||
|
||
assertDoesntContain(indexKeys, "FileWithoutPestTest.php") | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions
10
src/test/resources/com/pestphp/pest/goto/PestTestFinder/App/User.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
class User { | ||
public function getName(): String | ||
{ | ||
return "Oliver Nybroe"; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/test/resources/com/pestphp/pest/goto/PestTestFinder/test/App/UserTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
test("Can get user's name", function () { | ||
$user = new \App\User(); | ||
|
||
$this->asserEquals("Oliver Nybroe", $user->getName()); | ||
}); |
5 changes: 5 additions & 0 deletions
5
src/test/resources/com/pestphp/pest/indexers/PestTestIndexTest/FileWithPestTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
test('basic', function () { | ||
$this->assertTrue(true); | ||
}); |
3 changes: 3 additions & 0 deletions
3
src/test/resources/com/pestphp/pest/indexers/PestTestIndexTest/FileWithoutPestTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
echo "works"; |