Skip to content

Commit

Permalink
Merge pull request #84 from Haehnchen/feature/exception-php-doc
Browse files Browse the repository at this point in the history
migrate expected exception from docblock to inline php check (docbloc…
  • Loading branch information
Haehnchen committed Feb 19, 2023
2 parents 969bdce + dcbd606 commit 5c389a1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement
}

if (IntentionPreviewUtils.isPreviewElement(psiElement) || exceptions.size() == 1) {
PhpUnitPluginUtil.insertExpectedException(editor.getDocument(), method, exceptions.iterator().next());
PhpUnitPluginUtil.insertExpectedException(editor.getDocument(), method, psiElement, exceptions.iterator().next());

return;
}
Expand All @@ -60,11 +60,11 @@ public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement

JBPopupFactory.getInstance().createPopupChooserBuilder(list)
.setTitle("PHPUnit: Select Exception")
.setItemSelectedCallback(s -> WriteCommandAction.runWriteCommandAction(
.setItemChosenCallback(s -> WriteCommandAction.runWriteCommandAction(
psiElement.getProject(),
getText(),
"",
() -> PhpUnitPluginUtil.insertExpectedException(editor.getDocument(), method, s),
() -> PhpUnitPluginUtil.insertExpectedException(editor.getDocument(), method, psiElement, s),
psiElement.getContainingFile()
))
.createPopup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,47 +141,26 @@ public static String findCreateMockParameterOnParameterScope(@NotNull StringLite
/**
* Insert "expectException" for given scope (eg method)
*/
public static void insertExpectedException(@NotNull Document document, @NotNull PhpNamedElement forElement, @NotNull String exceptionClass) {
PhpDocComment docComment = forElement.getDocComment();
public static void insertExpectedException(@NotNull Document document, @NotNull Function function, @NotNull PsiElement psiElement, @NotNull String exceptionClass) {
String fqn = "\\" + StringUtils.stripStart(exceptionClass, "\\");

String tagString = "@expectedException \\" + exceptionClass;

// we need to update
if(docComment != null) {
PsiElement elementToInsert = PhpPsiElementFactory.createFromText(forElement.getProject(), PhpDocTag.class, "/** " + tagString + " */\\n");
if(elementToInsert == null) {
return;
}

PsiElement fromText = PhpPsiElementFactory.createFromText(forElement.getProject(), PhpDocTokenTypes.DOC_LEADING_ASTERISK, "/** \n * @var */");
docComment.addBefore(fromText, docComment.getLastChild());
docComment.addBefore(elementToInsert, docComment.getLastChild());

PsiDocumentManager.getInstance(forElement.getProject()).doPostponedOperationsAndUnblockDocument(document);
PsiDocumentManager.getInstance(forElement.getProject()).commitDocument(document);
// add scope
PsiElement addScope = PsiTreeUtil.getPrevSiblingOfType(psiElement, Statement.class);;
if (addScope == null) {
addScope = PsiTreeUtil.getNextSiblingOfType(psiElement, Statement.class);
}

return;
if (addScope == null) {
addScope = PsiTreeUtil.getParentOfType(psiElement, Statement.class, true, GroupStatement.class);
}

// new PhpDoc see PhpDocCommentGenerator
docComment = PhpPsiElementFactory.createFromText(forElement.getProject(), PhpDocComment.class, "/**\n " + tagString + " \n */");
if(docComment == null) {
if (addScope == null) {
return;
}

PsiElement parent = forElement.getParent();
int atOffset = forElement.getTextRange().getStartOffset() + 1;
parent.addBefore(docComment, forElement);
PsiDocumentManager.getInstance(forElement.getProject()).doPostponedOperationsAndUnblockDocument(document);
PsiElement atElement = forElement.getContainingFile().findElementAt(atOffset);
if (atElement != null) {
PsiElement docParent = PsiTreeUtil.findFirstParent(atElement, true, element -> ((element instanceof PhpDocComment)) || ((element instanceof PhpFile)));
if ((docParent instanceof PhpDocComment)) {
CodeStyleManager.getInstance(forElement.getProject()).reformatNewlyAddedElement(docParent.getParent().getNode(), docParent.getNode());
}
}
String s = PhpElementsUtil.insertUseIfNecessary(function, fqn);
Statement statement = PhpPsiElementFactory.createStatement(function.getProject(), "$this->expectException(" + (s != null ? s : fqn) + "::class);");

PsiDocumentManager.getInstance(forElement.getProject()).doPostponedOperationsAndUnblockDocument(document);
PsiDocumentManager.getInstance(forElement.getProject()).commitDocument(document);
addScope.getParent().addAfter(statement, addScope);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.psi.PhpPsiElementFactory;
Expand Down Expand Up @@ -33,47 +34,25 @@ public void testIsTestClassWithoutIndexAccess() {
));
}

public void testThatInsertExpectedExceptionForNonExistingDocBlock() {
public void testInsertExpectedException() {
PsiFile psiFile = myFixture.configureByText("test.php", "<?php\n" +
" function test()" +
" {" +
" }"
);

Document document = PsiDocumentManager.getInstance(getProject()).getDocument(psiFile);
Function function = PsiTreeUtil.findChildOfType(psiFile, Function.class);

new WriteCommandAction.Simple(getProject(), "PHPUnit: expectedException Insert") {
@Override
protected void run() {
PhpUnitPluginUtil.insertExpectedException(document, function, "Foobar\\Foobar");
}
}.execute();

assertTrue(psiFile.getText().contains("@expectedException \\Foobar\\Foobar"));
}

public void testInsertExpectedExceptionForDocBlockUpdate() {
PsiFile psiFile = myFixture.configureByText("test.php", "<?php\n" +
"/**\n" +
" * @Foo\n" +
" * @return Foo\n" +
" */\n" +
"function test()\n" +
"{\n" +
" (new Foo())->foo();" +
" <caret>\n" +
"}\n"
);

PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

Document document = PsiDocumentManager.getInstance(getProject()).getDocument(psiFile);
Function function = PsiTreeUtil.findChildOfType(psiFile, Function.class);

new WriteCommandAction.Simple(getProject(), "PHPUnit: expectedException Insert") {
@Override
protected void run() {
PhpUnitPluginUtil.insertExpectedException(document, function, "Foobar\\Foobar");
}
}.execute();
WriteCommandAction.runWriteCommandAction(getProject(), () -> PhpUnitPluginUtil.insertExpectedException(document, function, psiElement, "Foobar\\Foobar"));

String text = psiFile.getText();

assertTrue(psiFile.getText().contains("@expectedException \\Foobar\\Foobar"));
assertTrue(text.contains("use Foobar\\Foobar;"));
assertTrue(text.contains("$this->expectException(Foobar::class);"));
}
}

0 comments on commit 5c389a1

Please sign in to comment.