Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.jetbrains.php.lang.psi.resolve.types.PhpType
* When @kphp-... tags (regardless of elementType) are stored as stubs,
* they hold only name and optional arbitrary string value.
* Similar to [com.jetbrains.php.lang.documentation.phpdoc.psi.stubs.PhpDocTagStubImpl]
*
* TODO: make a research to check are we really need stubs?
*/
class KphpDocTagStubImpl(
parent: StubElement<*>?,
Expand All @@ -22,4 +24,6 @@ class KphpDocTagStubImpl(
override fun getType() = PhpType()
override fun getName() = name
override fun getValue() = value

override fun toString(): String = "$name${if (value != null) ": $value" else ""}"
}
12 changes: 12 additions & 0 deletions src/test/fixtures/kphp_stub/kphp-json.fixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/**
* @kphp-json rename_policy=camelCase
*/
class C {
/**
* @kphp-json float_precision=1
* @kphp-json required
*/
public float $field;
}
9 changes: 9 additions & 0 deletions src/test/fixtures/kphp_stub/kphp-json.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PhpFileStubImpl
DOC_COMMENT:PhpDocCommentStubImpl
@kphp-json {name: @kphp-json}:@kphp-json
CLASS:PhpClassStubImpl{C}
DOC_COMMENT:PhpDocCommentStubImpl
@kphp-json {name: @kphp-json}:@kphp-json
@kphp-json {name: @kphp-json}:@kphp-json
CLASS_FIELDS:PhpClassFieldListStubImpl
CLASS_FIELD:PhpFieldStubImpl
22 changes: 22 additions & 0 deletions src/test/fixtures/kphp_stub/kphp-serialize.fixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* @kphp-serializable
* @kphp-reserved-fields [2]
*/
class C {
/**
* @kphp-serialized-float32 arg
*/
public float $field0;

/**
* @kphp-serialized-field 1
*/
public float $field1;

/**
* @kphp-serialized-field 3
*/
public int $field3;
}
17 changes: 17 additions & 0 deletions src/test/fixtures/kphp_stub/kphp-serialize.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
PhpFileStubImpl
DOC_COMMENT:PhpDocCommentStubImpl
@kphp-... {name: @kphp-serializable}:@kphp-serializable
@kphp-... {name: @kphp-reserved-fields}:@kphp-reserved-fields
CLASS:PhpClassStubImpl{C}
DOC_COMMENT:PhpDocCommentStubImpl
@kphp-... {name: @kphp-serialized-float32}:@kphp-serialized-float32
CLASS_FIELDS:PhpClassFieldListStubImpl
CLASS_FIELD:PhpFieldStubImpl
DOC_COMMENT:PhpDocCommentStubImpl
@kphp-... {name: @kphp-serialized-field}:@kphp-serialized-field
CLASS_FIELDS:PhpClassFieldListStubImpl
CLASS_FIELD:PhpFieldStubImpl
DOC_COMMENT:PhpDocCommentStubImpl
@kphp-... {name: @kphp-serialized-field}:@kphp-serialized-field
CLASS_FIELDS:PhpClassFieldListStubImpl
CLASS_FIELD:PhpFieldStubImpl
9 changes: 9 additions & 0 deletions src/test/fixtures/kphp_stub/kphp-simple-tags.fixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/**
* @kphp-throws \Exception
* @kphp-color red
* @kphp-required
* @kphp-inline
* @kphp-profile
*/
function test($arg1, $arg2) { }
10 changes: 10 additions & 0 deletions src/test/fixtures/kphp_stub/kphp-simple-tags.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PhpFileStubImpl
DOC_COMMENT:PhpDocCommentStubImpl
@kphp-... {name: @kphp-throws}:@kphp-throws
@kphp-... {name: @kphp-color}:@kphp-color
@kphp-... {name: @kphp-required}:@kphp-required
@kphp-... {name: @kphp-inline}:@kphp-inline
@kphp-... {name: @kphp-profile}:@kphp-profile
FUNCTION:PhpFunctionStubImpl
NOT_PROMOTED_PARAMETER:PhpParameterStubImpl
NOT_PROMOTED_PARAMETER:PhpParameterStubImpl
5 changes: 5 additions & 0 deletions src/test/fixtures/kphp_stub/kphp-template.fixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
/**
* @kphp-template-class int,string
*/
class C { }
4 changes: 4 additions & 0 deletions src/test/fixtures/kphp_stub/kphp-template.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PhpFileStubImpl
DOC_COMMENT:PhpDocCommentStubImpl
@kphp-template-class {name: @kphp-template-class, value: int,string}:@kphp-template-class: int,string
CLASS:PhpClassStubImpl{C}
4 changes: 4 additions & 0 deletions src/test/fixtures/kphp_stub/kphp-warn-performance.fixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

/** @kphp-warn-performance all */
function test() { }
4 changes: 4 additions & 0 deletions src/test/fixtures/kphp_stub/kphp-warn-performance.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PhpFileStubImpl
DOC_COMMENT:PhpDocCommentStubImpl
@kphp-warn-performance {name: @kphp-warn-performance}:@kphp-warn-performance
FUNCTION:PhpFunctionStubImpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.vk.kphpstorm.testing.infrastructure

import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.stubs.PsiFileStubImpl
import com.intellij.psi.stubs.Stub
import com.intellij.psi.stubs.StubElement
import com.jetbrains.php.lang.documentation.phpdoc.psi.stubs.PhpDocTagStub
import com.jetbrains.php.lang.psi.stubs.PhpFileStubBuilder
import com.vk.kphpstorm.configuration.KphpStormConfiguration
import java.io.File


abstract class StubTestBase : KphpStormTestBase() {

private val stubBuilder = PhpFileStubBuilder()

/**
* Run stub test on the file.fixture.php
* file.stub.php must exist. It contains dumped stub tree
*/
protected fun doStubTest(fixtureFile: String) {
KphpStormConfiguration.saveThatSetupForProjectDone(project)
myFixture.configureByFile(fixtureFile)

val stubTree = stubBuilder.buildStubTree(myFixture.file)
val stubTreeString = dumpToString(stubTree)

val expectedFileRelPath = fixtureFile.replace(".fixture.php", ".stub.php")
if (fixtureFile == expectedFileRelPath) {
fail("wrong input fixture file path: $fixtureFile")
}

val expectedFile = File(testDataPath).resolve(expectedFileRelPath)
if (!expectedFile.exists() || !expectedFile.isFile) {
expectedFile.createNewFile()
expectedFile.writeText(stubTreeString)
fail("new .stub.php file was created")
}

assertSameLinesWithFile(expectedFile.absolutePath, stubTreeString)
}


private fun dumpToString(node: StubElement<*>): String {
val buffer = StringBuilder()
dumpToString(node, buffer, 0)

return buffer.toString()
}

private fun dumpToString(node: StubElement<*>, buffer: StringBuilder, indent: Int) {
StringUtil.repeatSymbol(buffer, ' ', indent)

val presentable = getPresentable(node)

if (presentable != null) {
buffer.append(presentable.toString()).append(':')
}

buffer.append(node.toString()).append('\n')

for (child in node.childrenStubs) {
dumpToString(child, buffer, indent + 2)
}
}

@Suppress("UnstableApiUsage")
private fun getPresentable(node: Stub): Any? {
return when (node) {
is PsiFileStubImpl<*> -> {
null
}

is PhpDocTagStub -> {
"${node.stubType} {name: ${node.name}${if (node.value != null) ", value: " + node.value else ""}}"
}

else -> node.stubType
}
}
}
25 changes: 25 additions & 0 deletions src/test/kotlin/com/vk/kphpstorm/testing/tests/KphpStubTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.vk.kphpstorm.testing.tests

import com.vk.kphpstorm.testing.infrastructure.StubTestBase

class KphpStubTest : StubTestBase() {
fun testKphpJson() {
doStubTest("kphp_stub/kphp-json.fixture.php")
}

fun testKphpSimpleTags() {
doStubTest("kphp_stub/kphp-simple-tags.fixture.php")
}

fun testKphpTemplate() {
doStubTest("kphp_stub/kphp-template.fixture.php")
}

fun testKphpWarnPerformance() {
doStubTest("kphp_stub/kphp-warn-performance.fixture.php")
}

fun testKphpSerialize() {
doStubTest("kphp_stub/kphp-serialize.fixture.php")
}
}
Loading