forked from maxfilatov/phpuaca
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Prophecy reveal type provider #9 and optimize prophesize class fi…
…lter #8
- Loading branch information
Showing
10 changed files
with
263 additions
and
17 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
64 changes: 64 additions & 0 deletions
64
src/de/espend/idea/php/phpunit/type/RevealProphecyTypeProvider.java
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,64 @@ | ||
package de.espend.idea.php.phpunit.type; | ||
|
||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiElement; | ||
import com.jetbrains.php.PhpIndex; | ||
import com.jetbrains.php.lang.psi.PhpPsiUtil; | ||
import com.jetbrains.php.lang.psi.elements.*; | ||
import com.jetbrains.php.lang.psi.resolve.types.PhpType; | ||
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider3; | ||
import de.espend.idea.php.phpunit.type.utils.PhpTypeProviderUtil; | ||
import de.espend.idea.php.phpunit.type.utils.ProphecyTypeUtil; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Collection; | ||
import java.util.Set; | ||
|
||
/** | ||
* $foobar = $this->prophesize(Foobar::class); | ||
* $foobar->reveal(); | ||
* | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
*/ | ||
public class RevealProphecyTypeProvider implements PhpTypeProvider3 { | ||
@Override | ||
public char getKey() { | ||
return '\u1537'; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public PhpType getType(PsiElement element) { | ||
if(element instanceof MethodReference && "reveal".equals(((MethodReference) element).getName())) { | ||
PhpExpression classReference = ((MethodReference) element).getClassReference(); | ||
if(classReference instanceof Variable || classReference instanceof MethodReference) { | ||
Method method = PhpPsiUtil.getParentByCondition(element, Method.INSTANCEOF); | ||
if(method != null) { | ||
PhpClass containingClass = method.getContainingClass(); | ||
|
||
// filter phpunit test methods | ||
if(containingClass != null && containingClass.getName().endsWith("Test")) { | ||
String prophesize = ProphecyTypeUtil.getProphesizeSignatureFromTypes(classReference.getType().getTypes()); | ||
if(prophesize != null) { | ||
return new PhpType().add("#" + this.getKey() + prophesize); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) { | ||
PhpIndex phpIndex = PhpIndex.getInstance(project); | ||
|
||
String resolvedParameter = PhpTypeProviderUtil.getResolvedParameter(phpIndex, expression); | ||
if(resolvedParameter == null) { | ||
return null; | ||
} | ||
|
||
return phpIndex.getAnyByFQN(resolvedParameter); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/de/espend/idea/php/phpunit/type/utils/ProphecyTypeUtil.java
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,29 @@ | ||
package de.espend.idea.php.phpunit.type.utils; | ||
|
||
import de.espend.idea.php.phpunit.type.MockProphecyTypeProvider; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Collection; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
*/ | ||
public class ProphecyTypeUtil { | ||
/** | ||
* #元#M#C\FooTest.prophesizeƒ#K#C\Foo.class => #K#C\Foo.class | ||
*/ | ||
public static String getProphesizeSignatureFromTypes(@NotNull Collection<String> types) { | ||
return types.stream().filter(s -> { | ||
boolean b = s.startsWith("#" + MockProphecyTypeProvider.CHAR); | ||
if (!b) { | ||
return false; | ||
} | ||
|
||
int i = s.indexOf(MockProphecyTypeProvider.TRIM_KEY); | ||
return i >= 0 && s.substring(2, i).endsWith("prophesize"); | ||
}).findFirst().map(s -> | ||
s.substring(s.indexOf(MockProphecyTypeProvider.TRIM_KEY) + 1) | ||
).orElse(null); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
tests/de/espend/idea/php/phpunit/tests/type/RevealProphecyTypeProviderTest.java
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,51 @@ | ||
package de.espend.idea.php.phpunit.tests.type; | ||
|
||
import com.intellij.patterns.PlatformPatterns; | ||
import com.jetbrains.php.lang.PhpFileType; | ||
import com.jetbrains.php.lang.psi.elements.Method; | ||
import de.espend.idea.php.phpunit.tests.PhpUnitLightCodeInsightFixtureTestCase; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
* @see de.espend.idea.php.phpunit.type.RevealProphecyTypeProvider | ||
*/ | ||
public class RevealProphecyTypeProviderTest extends PhpUnitLightCodeInsightFixtureTestCase { | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
myFixture.copyFileToProject("RevealProphecyTypeProvider.php"); | ||
} | ||
|
||
public String getTestDataPath() { | ||
return new File(this.getClass().getResource("fixtures").getFile()).getAbsolutePath(); | ||
} | ||
|
||
public void testThatRevealIsResolved() { | ||
assertPhpReferenceResolveTo(PhpFileType.INSTANCE, "<?php\n" + | ||
"class FooTest extends \\PHPUnit\\Framework\\TestCase\n" + | ||
" {\n" + | ||
" public function testFoobar()\n" + | ||
" {\n" + | ||
" $foo = $this->prophesize(Foo::class);\n" + | ||
" $foo->reveal()->getB<caret>ar();\n" + | ||
" }\n" + | ||
" }", | ||
PlatformPatterns.psiElement(Method.class).withName("getBar") | ||
); | ||
} | ||
|
||
public void testThatRevealIsResolvedForStringClass() { | ||
assertPhpReferenceResolveTo(PhpFileType.INSTANCE, "<?php\n" + | ||
"class FooTest extends \\PHPUnit\\Framework\\TestCase\n" + | ||
" {\n" + | ||
" public function testFoobar()\n" + | ||
" {\n" + | ||
" $foo = $this->prophesize('Foo');\n" + | ||
" $foo->reveal()->getB<caret>ar();\n" + | ||
" }\n" + | ||
" }", | ||
PlatformPatterns.psiElement(Method.class).withName("getBar") | ||
); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
tests/de/espend/idea/php/phpunit/tests/type/fixtures/RevealProphecyTypeProvider.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,39 @@ | ||
<?php | ||
|
||
namespace PHPUnit\Framework | ||
{ | ||
|
||
use Prophecy\Prophecy\ObjectProphecy; | ||
|
||
class TestCase extends \PHPUnit_Framework_MockObject_InvocationMocker | ||
{ | ||
/** | ||
* @param null $classOrInterface | ||
* @return \Prophecy\Prophecy\ObjectProphecy | ||
*/ | ||
protected function prophesize($classOrInterface = null) | ||
{ | ||
return new ObjectProphecy(); | ||
} | ||
}; | ||
} | ||
|
||
namespace Prophecy\Prophecy | ||
{ | ||
class ObjectProphecy | ||
{ | ||
public function reveal() | ||
{ | ||
} | ||
} | ||
} | ||
|
||
namespace | ||
{ | ||
class Foo | ||
{ | ||
public function getBar() | ||
{ | ||
} | ||
} | ||
} |