diff --git a/CHANGELOG.md b/CHANGELOG.md index d8e12159..062f0b80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Line markers now works, for the whole file and the single test. ([#17](https://github.com/pestphp/pest-intellij/pull/17), [#24](https://github.com/pestphp/pest-intellij/pull/24)) - Add running support with debugger ([#19](https://github.com/pestphp/pest-intellij/pull/19)) - `$this->field` type support for fields declared in `beforeEach` and `beforeAll` functions ([#22](https://github.com/pestphp/pest-intellij/pull/22)) +- Run tests scoped based on the cursor ([#24](https://github.com/pestphp/pest-intellij/pull/24), [#26](https://github.com/pestphp/pest-intellij/pull/26)) ## [v0.1.1] ### Added diff --git a/gradle.properties b/gradle.properties index 1572ed56..eb2a2425 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ pluginGroup = com.pestphp pluginName = Pest -pluginVersion = 0.2.0 +pluginVersion = 0.2.1-alpha.1 pluginSinceBuild = 201 pluginUntilBuild = null diff --git a/src/main/java/com/pestphp/pest/PestTestRunLineMarkerProvider.java b/src/main/java/com/pestphp/pest/PestTestRunLineMarkerProvider.java deleted file mode 100644 index 9f4faf03..00000000 --- a/src/main/java/com/pestphp/pest/PestTestRunLineMarkerProvider.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.pestphp.pest; - -import com.intellij.execution.lineMarker.RunLineMarkerContributor; -import com.intellij.psi.PsiElement; -import com.intellij.util.ObjectUtils; -import com.jetbrains.php.lang.lexer.PhpTokenTypes; -import com.jetbrains.php.lang.psi.PhpPsiUtil; -import com.jetbrains.php.lang.psi.elements.FunctionReference; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -public class PestTestRunLineMarkerProvider extends RunLineMarkerContributor { - @Override - public @Nullable Info getInfo(@NotNull PsiElement leaf) { - if (!PhpPsiUtil.isOfType(leaf, PhpTokenTypes.IDENTIFIER)) { - return null; - } - - FunctionReference element = ObjectUtils.tryCast(leaf.getParent(), FunctionReference.class); - - if (element == null) { - return null; - } - - if (!PestUtil.isPestTestFunction(element)) { - return null; - } - - // return RunLineMarkerContributor.withExecutorActions(getTestStateIcon(getLocationHint(testName), leaf.getProject(), false)); - return RunLineMarkerContributor.withExecutorActions(PestIcons.RUN_SINGLE_TEST); - } -} diff --git a/src/main/java/com/pestphp/pest/PestUtil.java b/src/main/java/com/pestphp/pest/PestUtil.java deleted file mode 100644 index 507652c9..00000000 --- a/src/main/java/com/pestphp/pest/PestUtil.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.pestphp.pest; - -import com.intellij.notification.Notification; -import com.intellij.notification.NotificationType; -import com.intellij.notification.Notifications; -import com.intellij.openapi.application.Application; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.text.StringUtil; -import com.intellij.psi.PsiElement; -import com.intellij.psi.util.PsiTreeUtil; -import com.jetbrains.php.lang.psi.PhpFile; -import com.jetbrains.php.lang.psi.elements.FunctionReference; -import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; -import com.jetbrains.php.testFramework.PhpTestFrameworkSettingsManager; -import org.jetbrains.annotations.Nls; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.Collection; - -public class PestUtil { - private static final String NOTIFICATION_GROUP = "Pest"; - - public static boolean isPestTestFile(@Nullable PsiElement element) { - if (element == null) { - return false; - } - - if (!(element instanceof PhpFile)) { - return false; - } - - Collection functions = PsiTreeUtil.findChildrenOfType(element, FunctionReference.class); - - return functions.stream() - .anyMatch(PestUtil::isPestTestFunction); - } - - public static boolean isPestTestFunction(@NotNull FunctionReference reference) { - String s = reference.getCanonicalText(); - return s.equals("it") || s.equals("test"); - } - - public static @Nullable String getTestName(FunctionReference element) { - PsiElement parameter = element.getParameter(0); - - if (!(parameter instanceof StringLiteralExpression)) { - return null; - } - - return ((StringLiteralExpression) parameter).getContents(); - } - - public static boolean isEnabled(@NotNull Project project) { - return PhpTestFrameworkSettingsManager - .getInstance(project) - .getConfigurations(PestFrameworkType.getInstance()) - .stream() - .anyMatch(config -> StringUtil.isNotEmpty(config.getExecutablePath())); - } - - public static void doNotify( - @NotNull String title, - @NotNull @Nls(capitalization = Nls.Capitalization.Sentence) String content, - @NotNull NotificationType type, - @Nullable Project project - ) { - Notification notification = new Notification(NOTIFICATION_GROUP, title, content, type); - doNotify(notification, project); - } - - public static void doNotify(Notification notification, @Nullable Project project) { - if (project != null && !project.isDisposed() && !project.isDefault()) { - project.getMessageBus().syncPublisher(Notifications.TOPIC).notify(notification); - } else { - Application app = ApplicationManager.getApplication(); - if (!app.isDisposed()) { - app.getMessageBus().syncPublisher(Notifications.TOPIC).notify(notification); - } - } - } -} diff --git a/src/main/kotlin/com/pestphp/pest/PestTestRunLineMarkerProvider.kt b/src/main/kotlin/com/pestphp/pest/PestTestRunLineMarkerProvider.kt new file mode 100644 index 00000000..0f919bf8 --- /dev/null +++ b/src/main/kotlin/com/pestphp/pest/PestTestRunLineMarkerProvider.kt @@ -0,0 +1,24 @@ +package com.pestphp.pest + +import com.intellij.execution.lineMarker.RunLineMarkerContributor +import com.intellij.psi.PsiElement +import com.jetbrains.php.lang.lexer.PhpTokenTypes +import com.jetbrains.php.lang.psi.PhpPsiUtil +import com.jetbrains.php.lang.psi.elements.FunctionReference +import com.pestphp.pest.PestUtil.isPestTestFunction + +class PestTestRunLineMarkerProvider : RunLineMarkerContributor() { + override fun getInfo(leaf: PsiElement): Info? { + if (!PhpPsiUtil.isOfType(leaf, PhpTokenTypes.IDENTIFIER)) { + return null + } + + // return RunLineMarkerContributor.withExecutorActions(getTestStateIcon(getLocationHint(testName), leaf.getProject(), false)); + return when { + leaf.parent !is FunctionReference -> null + isPestTestFunction(leaf.parent as FunctionReference) -> withExecutorActions(PestIcons.RUN_SINGLE_TEST) + else -> null + } + + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/pestphp/pest/PestUtil.kt b/src/main/kotlin/com/pestphp/pest/PestUtil.kt new file mode 100644 index 00000000..433c52fb --- /dev/null +++ b/src/main/kotlin/com/pestphp/pest/PestUtil.kt @@ -0,0 +1,105 @@ +package com.pestphp.pest + +import com.intellij.notification.Notification +import com.intellij.notification.NotificationType +import com.intellij.notification.Notifications +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.project.Project +import com.intellij.openapi.util.text.StringUtil +import com.intellij.psi.PsiElement +import com.intellij.psi.util.PsiTreeUtil +import com.intellij.util.ObjectUtils +import com.jetbrains.php.lang.psi.PhpFile +import com.jetbrains.php.lang.psi.elements.FunctionReference +import com.jetbrains.php.lang.psi.elements.MethodReference +import com.jetbrains.php.lang.psi.elements.StringLiteralExpression +import com.jetbrains.php.testFramework.PhpTestFrameworkConfiguration +import com.jetbrains.php.testFramework.PhpTestFrameworkSettingsManager +import org.jetbrains.annotations.Nls + +object PestUtil { + private const val NOTIFICATION_GROUP = "Pest" + + @JvmStatic + fun isPestTestFile(element: PsiElement?): Boolean { + if (element == null) { + return false + } + if (element !is PhpFile) { + return false + } + val functions = PsiTreeUtil.findChildrenOfType(element, FunctionReference::class.java) + return functions.stream() + .anyMatch { obj: FunctionReference? -> isPestTestFunction(obj) } + } + + @JvmStatic + fun isPestTestFunction(element: PsiElement?): Boolean { + return when (element) { + null -> false + is MethodReference -> isPestTestFunction(element) + is FunctionReference -> isPestTestFunction(element) + else -> false + } + } + + @JvmStatic + fun isPestTestFunction(reference: FunctionReference): Boolean { + return reference.canonicalText in setOf("it", "test") + } + + @JvmStatic + fun isPestTestFunction(methodReference: MethodReference): Boolean { + val reference = ObjectUtils.tryCast(methodReference.classReference, FunctionReference::class.java) + return reference != null && isPestTestFunction(reference) + } + + @JvmStatic + fun getTestName(element: FunctionReference): String? { + return when (val parameter = element.getParameter(0)) { + is StringLiteralExpression -> parameter.contents + else -> null + } + } + + @JvmStatic + fun getTestName(element: PsiElement?): String? { + return when (element) { + is MethodReference -> (element.classReference as? FunctionReference)?.let(this::getTestName) + is FunctionReference -> getTestName(element) + else -> null + } + } + + @JvmStatic + fun isEnabled(project: Project): Boolean { + return PhpTestFrameworkSettingsManager + .getInstance(project) + .getConfigurations(PestFrameworkType.getInstance()) + .stream() + .anyMatch { config: PhpTestFrameworkConfiguration -> StringUtil.isNotEmpty(config.executablePath) } + } + + @JvmStatic + fun doNotify( + title: String, + content: @Nls(capitalization = Nls.Capitalization.Sentence) String, + type: NotificationType, + project: Project? + ) { + val notification = Notification(NOTIFICATION_GROUP, title, content, type) + doNotify(notification, project) + } + + @JvmStatic + fun doNotify(notification: Notification?, project: Project?) { + if (project != null && !project.isDisposed && !project.isDefault) { + project.messageBus.syncPublisher(Notifications.TOPIC).notify(notification!!) + } else { + val app = ApplicationManager.getApplication() + if (!app.isDisposed) { + app.messageBus.syncPublisher(Notifications.TOPIC).notify(notification!!) + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/pestphp/pest/configuration/PestRunConfigurationProducer.kt b/src/main/kotlin/com/pestphp/pest/configuration/PestRunConfigurationProducer.kt index 916b7c8d..71d848c2 100644 --- a/src/main/kotlin/com/pestphp/pest/configuration/PestRunConfigurationProducer.kt +++ b/src/main/kotlin/com/pestphp/pest/configuration/PestRunConfigurationProducer.kt @@ -8,7 +8,6 @@ import com.intellij.psi.PsiDirectory import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import com.intellij.util.Function -import com.jetbrains.php.lang.psi.elements.FunctionReference import com.jetbrains.php.testFramework.run.PhpTestConfigurationProducer import com.pestphp.pest.PestUtil @@ -32,14 +31,10 @@ class PestRunConfigurationProducer : PhpTestConfigurationProducer { element: PsiElement? -> - (element is FunctionReference - && PestUtil.isPestTestFunction(element)) + return@Condition PestUtil.isPestTestFunction(element) } private val METHOD_NAMER = Function { element: PsiElement? -> - if (element is FunctionReference) { - return@Function PestUtil.getTestName(element) - } - null + return@Function PestUtil.getTestName(element) } private val FILE_TO_SCOPE = Function { file: PsiFile? -> if (PestUtil.isPestTestFile(file)) { diff --git a/src/test/kotlin/com/pestphp/pest/tests/PestUtilTest.kt b/src/test/kotlin/com/pestphp/pest/tests/PestUtilTest.kt index 5a16ce24..e760b9f2 100644 --- a/src/test/kotlin/com/pestphp/pest/tests/PestUtilTest.kt +++ b/src/test/kotlin/com/pestphp/pest/tests/PestUtilTest.kt @@ -20,7 +20,7 @@ class PestUtilTest: PestLightCodeFixture() { val file = myFixture.configureByFile("SimpleTest.php") val functions = PsiTreeUtil.findChildrenOfType(file, FunctionReference::class.java) - .stream().filter(PestUtil::isPestTestFunction).collect(Collectors.toList()) + .stream().filter { element -> PestUtil.isPestTestFunction(element)}.collect(Collectors.toList()) assertEquals(1, functions.count()) diff --git a/src/test/kotlin/com/pestphp/pest/tests/fixtures/SimpleHigherOrderTestWithName.php b/src/test/kotlin/com/pestphp/pest/tests/fixtures/SimpleHigherOrderTestWithName.php new file mode 100644 index 00000000..940d54c7 --- /dev/null +++ b/src/test/kotlin/com/pestphp/pest/tests/fixtures/SimpleHigherOrderTestWithName.php @@ -0,0 +1,3 @@ +assertTrue(true);