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.
Reimplement magic Prophecy method #8
- Loading branch information
Showing
5 changed files
with
170 additions
and
2 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
62 changes: 62 additions & 0 deletions
62
src/de/espend/idea/php/phpunit/type/ProphecyTypeProvider.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,62 @@ | ||
package de.espend.idea.php.phpunit.type; | ||
|
||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiElement; | ||
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 org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Collection; | ||
import java.util.Set; | ||
|
||
/** | ||
* $this->prophesize(Foobar::class)->find()->will<caret>Return; | ||
* | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
*/ | ||
public class ProphecyTypeProvider implements PhpTypeProvider3 { | ||
@Override | ||
public char getKey() { | ||
return '\u1530'; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public PhpType getType(PsiElement element) { | ||
if(element instanceof MethodReference) { | ||
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")) { | ||
boolean isProphesize = classReference.getType().getTypes().stream().anyMatch(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"); | ||
}); | ||
|
||
if(isProphesize) { | ||
return (new PhpType()).add("\\Prophecy\\Prophecy\\MethodProphecy"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) { | ||
return null; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
tests/de/espend/idea/php/phpunit/tests/type/ProphecyTypeProviderTest.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,50 @@ | ||
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.ProphecyTypeProvider | ||
*/ | ||
public class ProphecyTypeProviderTest extends PhpUnitLightCodeInsightFixtureTestCase { | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
myFixture.copyFileToProject("ProphecyTypeProvider.php"); | ||
} | ||
|
||
public String getTestDataPath() { | ||
return new File(this.getClass().getResource("fixtures").getFile()).getAbsolutePath(); | ||
} | ||
|
||
public void testThatProphesizeForVariableIsResolved() { | ||
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->find()->will<caret>Return();\n" + | ||
" }\n" + | ||
" }", | ||
PlatformPatterns.psiElement(Method.class).withName("willReturn") | ||
); | ||
} | ||
|
||
public void testThatProphesizeForMethodReferenceIsResolved() { | ||
assertPhpReferenceResolveTo(PhpFileType.INSTANCE, "<?php\n" + | ||
"class FooTest extends \\PHPUnit\\Framework\\TestCase\n" + | ||
" {\n" + | ||
" public function testFoobar()\n" + | ||
" {\n" + | ||
" $foo = $this->prophesize(Foo::class)->find()->will<caret>Return();\n" + | ||
" }\n" + | ||
" }", | ||
PlatformPatterns.psiElement(Method.class).withName("willReturn") | ||
); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
tests/de/espend/idea/php/phpunit/tests/type/fixtures/ProphecyTypeProvider.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,54 @@ | ||
<?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 willExtend($class) | ||
{ | ||
} | ||
} | ||
|
||
class MethodProphecy | ||
{ | ||
public function willReturn() | ||
{ | ||
} | ||
} | ||
} | ||
|
||
namespace | ||
{ | ||
class PHPUnit_Framework_MockObject_InvocationMocker | ||
{ | ||
public function willReturn($value, ...$nextValues) | ||
{ | ||
} | ||
} | ||
|
||
class Foo | ||
{ | ||
public function getBar() | ||
{ | ||
} | ||
} | ||
} | ||
|