Skip to content

Commit

Permalink
Merge pull request #56 from pestphp/duplicate-test-name-inspection
Browse files Browse the repository at this point in the history
feat(inspection): Add duplicate test name inspection
  • Loading branch information
olivernybroe authored Aug 20, 2020
2 parents 47d60e3 + d13256a commit 36a6585
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Type provider for Pest test functions ([#48](https://github.com/pestphp/pest-intellij/pull/48))
- Added support for navigation between tests and test subject ([#53](https://github.com/pestphp/pest-intellij/pull/53))
- Added error reporting to GitHub issues ([#55](https://github.com/pestphp/pest-intellij/pull/55))
- Added inspection for duplicate test names in same file. ([#56](https://github.com/pestphp/pest-intellij/pull/56))

### Changed

Expand Down
12 changes: 10 additions & 2 deletions src/main/kotlin/com/pestphp/pest/PestFunctionsUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package com.pestphp.pest

import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
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.MethodReference
import com.jetbrains.php.lang.psi.elements.Statement
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression
import com.jetbrains.php.lang.psi.elements.impl.FunctionReferenceImpl

Expand Down Expand Up @@ -56,5 +57,12 @@ fun PsiElement?.getPestTestName(): String? {
}

fun PsiFile.getPestTests(): Set<FunctionReference> {
return PsiTreeUtil.findChildrenOfType(this, FunctionReference::class.java).toSet()
if (this !is PhpFile) return setOf()

return this.firstChild.children
.filterIsInstance<Statement>()
.mapNotNull { it.firstChild }
.filter { it.isPestTestReference() }
.filterIsInstance<FunctionReference>()
.toSet()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.pestphp.pest.inspections

import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.PsiElementVisitor
import com.jetbrains.php.lang.inspections.PhpInspection
import com.jetbrains.php.lang.psi.PhpFile
import com.jetbrains.php.lang.psi.elements.FunctionReference
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor
import com.pestphp.pest.getPestTestName
import com.pestphp.pest.getPestTests

class DuplicateTestNameInspection : PhpInspection() {
companion object {
private const val DESCRIPTION = "Pest test names must be unique in the same file."
}

override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : PhpElementVisitor() {
override fun visitPhpFile(file: PhpFile) {
file.getPestTests()
.groupBy { it.getPestTestName()!! }
.filter { it.value.count() > 1 }
.forEach {
declareProblemType(holder, it.value)
}
}
}
}

@Suppress("SpreadOperator")
private fun declareProblemType(holder: ProblemsHolder, tests: List<FunctionReference>) {
tests.forEach {
holder.registerProblem(
it,
DESCRIPTION,
ProblemHighlightType.GENERIC_ERROR,
*LocalQuickFix.EMPTY_ARRAY
)
}
}
}
9 changes: 9 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
<testFinder implementation="com.pestphp.pest.goto.PestTestFinder"/>
<fileBasedIndex implementation="com.pestphp.pest.indexers.PestTestIndex"/>
<errorHandler implementation="com.pestphp.pest.GithubErrorReporter"/>
<localInspection
language="PHP"
groupPath="PHP"
groupName="Pest"
shortName="DuplicateTestNameInspection"
enabledByDefault="true"
displayName="Duplicate test name"
implementationClass="com.pestphp.pest.inspections.DuplicateTestNameInspection"
/>
</extensions>

<extensions defaultExtensionNs="com.jetbrains.php">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html lang="en">
<body>
Reports duplicate pest test names in the same file.
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.pestphp.pest.inspections

import com.pestphp.pest.tests.PestLightCodeFixture


class DuplicateTestNameInspectionTest : PestLightCodeFixture() {
override fun getTestDataPath(): String? {
return "src/test/resources/com/pestphp/pest/inspections"
}

override fun setUp() {
super.setUp()

myFixture.enableInspections(DuplicateTestNameInspection::class.java)
}

fun testHasDuplicateTest() {
val file = myFixture.configureByFile("DuplicateTestName.php")

myFixture.checkHighlighting()
}

fun testNoDuplicateTest() {
val file = myFixture.configureByFile("NoDuplicateTestName.php")

myFixture.checkHighlighting()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

<error descr="Pest test names must be unique in the same file.">test('basic', function () {
$this->assertTrue(true);
})</error>;

<error descr="Pest test names must be unique in the same file.">test('basic', function () {
$this->assertTrue(true);
})</error>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

test('One test', function () {
$this->assertTrue(true);
});

test('Another test', function () {
$this->assertTrue(true);
});

0 comments on commit 36a6585

Please sign in to comment.