Skip to content

Commit

Permalink
ScopeCompletionContributor: code-coverage;
Browse files Browse the repository at this point in the history
  • Loading branch information
rentalhost committed Jun 9, 2017
1 parent ab28e17 commit 06e962e
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Database\Eloquent\Model;

class Example extends Model
{
public function scopeTest1() { }
public function scopeTest2($builder) { }
public function scopeTest3($builder, $var, $var2 = null) { }
}

$reference1 = (new Example)->reference;
$reference2 = (new Example)->reference();

class IsNotAModel
{
public function scopeShouldNotBeApplicable() { }
}

$reference3 = (new IsNotAModel)->reference;

$reference4 = (new UnresolvedClass)->reference;

$reference5 = ("not an instance")->reference;
10 changes: 8 additions & 2 deletions src/laravelInsight/scope/ScopeCompletionContributor.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.jetbrains.annotations.NotNull;

import net.rentalhost.idea.laravelInsight.resources.LaravelClasses;
import net.rentalhost.idea.utils.PhpClassUtil;

public class ScopeCompletionContributor extends CompletionContributor {
public ScopeCompletionContributor() {
Expand Down Expand Up @@ -70,9 +71,14 @@ public void addCompletions(

final Set<String> elementClassReferenceAbsolute = elementClassReferenceRelative.getType().globalLocationAware(elementClassReferenceRelative).getTypes();
final Collection<PhpClass> elementClasses = PhpIndex.getInstance(element.getProject()).getAnyByFQN(elementClassReferenceAbsolute.iterator().next());
final PhpClass elementClass = elementClasses.iterator().next();

if (elementClass == null) {
if (elementClasses.isEmpty()) {
return;
}

final PhpClass elementClass = elementClasses.iterator().next();

if (PhpClassUtil.findSuperOfType(elementClass, LaravelClasses.ELOQUENT_MODEL.toString()) == null) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package net.rentalhost.idea.laravelInsight.scope;

import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.psi.PsiFile;
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
import org.junit.Assert;

import net.rentalhost.suite.FixtureSuite;

public class ScopeCompletionContributorTest extends FixtureSuite {
public void testCodeCompletion() {
final PsiFile fileSample = getResourceFile("laravelInsight/scope/ScopeCompletionContributor.samples.php");

// Reference 1 (acceptable: field reference to scoped method).
final PhpNamedElement reference1 = getElementByName(fileSample, "reference1");
final LookupElement[] completionElements1 = getCompletionElements(reference1, 29);

Assert.assertEquals("test1", completionElements1[3].getLookupString());
Assert.assertEquals("test2", completionElements1[4].getLookupString());
Assert.assertEquals("test3", completionElements1[5].getLookupString());

coverageHandleInsert(fileSample, completionElements1[3]);

moveCaret(reference1.getTextOffset() + 29);
acceptLookupElement(completionElements1[3]);

Assert.assertEquals("$reference1 = (new Example)->test1()", reference1.getParent().getText());

// Reference 2 (acceptable: method reference to scoped method).
final PhpNamedElement reference2 = getElementByName(fileSample, "reference2");
final LookupElement[] completionElements2 = getCompletionElements(reference2, 29);

Assert.assertEquals("test1", completionElements2[3].getLookupString());
Assert.assertEquals("test2", completionElements2[4].getLookupString());
Assert.assertEquals("test3", completionElements2[5].getLookupString());

coverageHandleInsert(fileSample, completionElements2[4]);

moveCaret(reference2.getTextOffset() + 29);
acceptLookupElement(completionElements2[4]);

Assert.assertEquals("$reference2 = (new Example)->test2()", reference2.getParent().getText());

// Reference 3 (not acceptable: reference is not instance of Eloquent\Model).
final PhpNamedElement reference3 = getElementByName(fileSample, "reference3");
final LookupElement[] completionElements3 = getCompletionElements(reference3, 33);

Assert.assertEquals(1, completionElements3.length);

// Reference 4 (not acceptable: can't resolve class reference).
final PhpNamedElement reference4 = getElementByName(fileSample, "reference4");
final LookupElement[] completionElements4 = getCompletionElements(reference4, 37);

Assert.assertEquals(0, completionElements4.length);

// Reference 5 (not acceptable: not is an instance of some class - invalid reference).
final PhpNamedElement reference5 = getElementByName(fileSample, "reference5");
final LookupElement[] completionElements5 = getCompletionElements(reference5, 35);

Assert.assertEquals(0, completionElements5.length);
}
}
55 changes: 55 additions & 0 deletions tests/suite/FixtureSuite.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package net.rentalhost.suite;

import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.completion.OffsetMap;
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.codeInsight.lookup.LookupArranger;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
Expand Down Expand Up @@ -90,4 +98,51 @@ protected void run() throws Throwable {
}
}.execute();
}

@NotNull
protected LookupElement[] getCompletionElements(
final PsiElement reference,
final int referenceDistance
) {
myFixture.getEditor().getCaretModel().getPrimaryCaret().moveToOffset(reference.getTextOffset() + referenceDistance);
myFixture.completeBasic();

return valueOf(myFixture.getLookupElements());
}

protected void coverageHandleInsert(
final PsiFile fileSample,
final LookupElement completionElement
) {
runWriteAction(() -> {
final InsertionContext insertionContext = getInsertionContext(fileSample, completionElement);
completionElement.handleInsert(insertionContext);
});
}

protected void moveCaret(final int offset) {
getEditor().getCaretModel().moveToOffset(offset);
}

protected void acceptLookupElement(final LookupElement completionElement) {
final LookupImpl lookup = (LookupImpl) LookupManager
.getInstance(getProject())
.createLookup(getEditor(), new LookupElement[] { completionElement }, "", new LookupArranger.DefaultArranger());
lookup.finishLookup('\n');
}

@NotNull
private InsertionContext getInsertionContext(
final PsiFile fileSample,
final LookupElement completionElement
) {
final Editor fixtureEditor = myFixture.getEditor();

return new InsertionContext(new OffsetMap(fixtureEditor.getDocument()),
Lookup.NORMAL_SELECT_CHAR,
new LookupElement[] { completionElement },
fileSample,
fixtureEditor,
false);
}
}

0 comments on commit 06e962e

Please sign in to comment.