Skip to content

Commit

Permalink
Merge pull request #2324 from Haehnchen/feature/test-cases
Browse files Browse the repository at this point in the history
migrate new Symfony test cases action
  • Loading branch information
Haehnchen authored Mar 31, 2024
2 parents c7e37fb + a193af1 commit 37a14fe
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 195 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fr.adrienbrault.idea.symfony2plugin.action;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.refactoring.PhpNameUtil;
import com.jetbrains.php.roots.PhpNamespaceCompositeProvider;
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.util.psi.PhpBundleFileFactory;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;

/**
* @author Daniel Espendiller <[email protected]>
*/
public class NewKernelTestCaseAction extends AbstractProjectDumbAwareAction {
public NewKernelTestCaseAction() {
super("KernelTestCase", "Create KernelTestCase Class", Symfony2Icons.SYMFONY);
}

public void update(AnActionEvent event) {
this.setStatus(event, false);
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
return;
}

if (NewFileActionUtil.getSelectedDirectoryFromAction(event) != null) {
this.setStatus(event, true);
}
}

@Override
public void actionPerformed(@NotNull AnActionEvent event) {
PsiDirectory directory = NewFileActionUtil.getSelectedDirectoryFromAction(event);
if (directory == null) {
return;
}

Project project = getEventProject(event);
String className = Messages.showInputDialog(project, "New class name:", "New File", Symfony2Icons.SYMFONY);
if (StringUtils.isBlank(className)) {
return;
}

if (!className.toLowerCase().endsWith("test")) {
className += "Test";
}

if (!PhpNameUtil.isValidClassName(className)) {
JOptionPane.showMessageDialog(null, "Invalid class name");
return;
}

List<String> strings = PhpNamespaceCompositeProvider.INSTANCE.suggestNamespaces(directory);
if (strings.isEmpty()) {
JOptionPane.showMessageDialog(null, "No namespace found");
return;
}

String finalClassName = className;
ApplicationManager.getApplication().runWriteAction(() -> {
HashMap<String, String> hashMap = new HashMap<>() {{
put("class", finalClassName);
put("namespace", strings.get(0));
}};

PsiElement commandAttributes = PhpBundleFileFactory.createFile(
project,
directory.getVirtualFile(),
"kernel_test_case",
finalClassName,
hashMap
);

new OpenFileDescriptor(project, commandAttributes.getContainingFile().getVirtualFile(), 0).navigate(true);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package fr.adrienbrault.idea.symfony2plugin.action;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.refactoring.PhpNameUtil;
import com.jetbrains.php.roots.PhpNamespaceCompositeProvider;
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.util.psi.PhpBundleFileFactory;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;

/**
* @author Daniel Espendiller <[email protected]>
*/
public class NewWebTestCaseAction extends AbstractProjectDumbAwareAction {
public NewWebTestCaseAction() {
super("WebTestCase", "Create WebTestCase Class", Symfony2Icons.SYMFONY);
}

public void update(AnActionEvent event) {
this.setStatus(event, false);
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
return;
}

if (NewFileActionUtil.getSelectedDirectoryFromAction(event) != null) {
this.setStatus(event, true);
}
}

@Override
public void actionPerformed(@NotNull AnActionEvent event) {

PsiDirectory directory = NewFileActionUtil.getSelectedDirectoryFromAction(event);
if (directory == null) {
return;
}

Project project = getEventProject(event);
String className = Messages.showInputDialog(project, "New class name:", "New File", Symfony2Icons.SYMFONY);
if (StringUtils.isBlank(className)) {
return;
}

if (!className.toLowerCase().endsWith("test")) {
className += "Test";
}

if (!PhpNameUtil.isValidClassName(className)) {
JOptionPane.showMessageDialog(null, "Invalid class name");
return;
}

List<String> strings = PhpNamespaceCompositeProvider.INSTANCE.suggestNamespaces(directory);
if (strings.isEmpty()) {
JOptionPane.showMessageDialog(null, "No namespace found");
return;
}

String finalClassName = className;
ApplicationManager.getApplication().runWriteAction(() -> {
HashMap<String, String> hashMap = new HashMap<>() {{
put("class", finalClassName);
put("namespace", strings.get(0));
}};

PsiElement commandAttributes = PhpBundleFileFactory.createFile(
project,
directory.getVirtualFile(),
"web_test_case",
finalClassName,
hashMap
);

new OpenFileDescriptor(project, commandAttributes.getContainingFile().getVirtualFile(), 0).navigate(true);
});
}
}

This file was deleted.

This file was deleted.

28 changes: 13 additions & 15 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -778,12 +778,15 @@

<group id="SymfonyGroup" text="Symfony" popup="false">
<group id="SymfonyNewFile" class="com.intellij.ide.actions.NonTrivialActionGroup" text="Symfony" popup="true" icon="SymfonyIcons.Symfony">
<action id="SymfonyNewCommandAction" class="fr.adrienbrault.idea.symfony2plugin.action.NewCommandAction"/>
<action id="SymfonyNewControllerAction" class="fr.adrienbrault.idea.symfony2plugin.action.NewControllerAction"/>
<action id="SymfonyNewCommand" class="fr.adrienbrault.idea.symfony2plugin.action.NewCommandAction"/>
<action id="SymfonyNewController" class="fr.adrienbrault.idea.symfony2plugin.action.NewControllerAction"/>
<separator/>
<action id="SymfonyNewFormTypeAction" class="fr.adrienbrault.idea.symfony2plugin.action.NewFormTypeAction"/>
<action id="SymfonyNewTwigExtensionAction" class="fr.adrienbrault.idea.symfony2plugin.action.NewTwigExtensionAction"/>
<action id="SymfonyNewCompilerPassAction" class="fr.adrienbrault.idea.symfony2plugin.action.NewCompilerPassAction"/>
<action id="SymfonyNewFormType" class="fr.adrienbrault.idea.symfony2plugin.action.NewFormTypeAction"/>
<action id="SymfonyNewTwigExtension" class="fr.adrienbrault.idea.symfony2plugin.action.NewTwigExtensionAction"/>
<action id="SymfonyNewCompilerPass" class="fr.adrienbrault.idea.symfony2plugin.action.NewCompilerPassAction"/>
<separator/>
<action id="SymfonyNewKernelTestCase" class="fr.adrienbrault.idea.symfony2plugin.action.NewKernelTestCaseAction"/>
<action id="SymfonyNewTestCase" class="fr.adrienbrault.idea.symfony2plugin.action.NewWebTestCaseAction"/>
<separator/>
<action id="Symfony2NewXmlService" class="fr.adrienbrault.idea.symfony2plugin.action.NewXmlServiceAction"/>
<action id="Symfony2NewYamlService" class="fr.adrienbrault.idea.symfony2plugin.action.NewYamlServiceAction"/>
Expand All @@ -792,19 +795,14 @@
</group>

<group id="SymfonyGroupShortcut" text="Symfony" popup="false">
<action id="SymfonyNewCommandActionShortcut" class="fr.adrienbrault.idea.symfony2plugin.action.NewCommandAction$Shortcut"/>
<action id="SymfonyNewControllerActionShortcut" class="fr.adrienbrault.idea.symfony2plugin.action.NewControllerAction$Shortcut"/>
<action id="SymfonyNewFormTypeActionShortcut" class="fr.adrienbrault.idea.symfony2plugin.action.NewFormTypeAction$Shortcut"/>
<action id="SymfonyNewTwigExtensionActionShortcut" class="fr.adrienbrault.idea.symfony2plugin.action.NewTwigExtensionAction$Shortcut"/>
<action id="SymfonyNewCompilerPassActionShortcut" class="fr.adrienbrault.idea.symfony2plugin.action.NewCompilerPassAction$Shortcut"/>
<action id="SymfonyNewCommandShortcut" class="fr.adrienbrault.idea.symfony2plugin.action.NewCommandAction$Shortcut"/>
<action id="SymfonyNewControllerShortcut" class="fr.adrienbrault.idea.symfony2plugin.action.NewControllerAction$Shortcut"/>
<action id="SymfonyNewFormTypeShortcut" class="fr.adrienbrault.idea.symfony2plugin.action.NewFormTypeAction$Shortcut"/>
<action id="SymfonyNewTwigExtensionShortcut" class="fr.adrienbrault.idea.symfony2plugin.action.NewTwigExtensionAction$Shortcut"/>
<action id="SymfonyNewCompilerPassShortcut" class="fr.adrienbrault.idea.symfony2plugin.action.NewCompilerPassAction$Shortcut"/>
<add-to-group group-id="NewGroup" anchor="before" relative-to-action="SymfonyGroup" />
</group>

<action id="SymfonyWebTestCaseGenerator" class="fr.adrienbrault.idea.symfony2plugin.action.bundle.WebTestCaseGeneratorAction">
<!-- <add-to-group group-id="PhpUnitNewGroup" anchor="last"/> DROP in Phpstorm 2017.3 -->
<add-to-group group-id="NewGroup" anchor="last"/>
</action>

<action icon="SymfonyIcons.TwigBlockOverwrite"
id="SymfonyTwigBlockOverwrite"
class="fr.adrienbrault.idea.symfony2plugin.templating.action.TwigBlockOverwriteGenerator"
Expand Down
Loading

0 comments on commit 37a14fe

Please sign in to comment.