Skip to content

Commit c2dcac8

Browse files
committed
Merge pull request #39 from agallou/results_treeview
Display test results as a treeView
2 parents 852e4db + 7ce34a4 commit c2dcac8

18 files changed

+770
-244
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [#30](https://github.com/agallou/phpstorm-plugin/pull/30) Easily identify test files via a custom icon ([@agallou])
66
* [#37](https://github.com/agallou/phpstorm-plugin/pull/37) Save test and tested file before run ([@agallou])
77
* [#38](https://github.com/agallou/phpstorm-plugin/pull/38) Add default keyboard shortcuts ([@agallou])
8+
* [#39](https://github.com/agallou/phpstorm-plugin/pull/39) Display test results as a treeView ([@agallou])
89

910
## Documentation
1011

META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<keyboard-shortcut first-keystroke="alt shift K" keymap="$default"/>
4747
</action>
4848

49-
<action id="AtoumRunTests" class="pl.projectspace.idea.plugins.php.atoum.actions.AtoumRunTests"
49+
<action id="AtoumRunTests" class="org.atoum.intellij.plugin.atoum.actions.Run"
5050
icon="/pl/projectspace/idea/plugins/php/atoum/icons/atoum_16_16.png"
5151
text="run tests">
5252
<add-to-group group-id="RunMenu" anchor="last"/>

README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ Features :
1313
## Detailed Features
1414

1515

16+
### Execute tests inside PhpStorm
17+
18+
By an entry on the run menu and on the right click menu.
19+
20+
![Demo](doc/run.png)
21+
22+
You can run the tests from both the test file or the tested classe's file.
23+
24+
Your test file and tested file will automatically be save before running the test.
25+
26+
The default keyboard shortcut to execute the test is alt+shift+M.
27+
28+
1629
### Go to the test class from the tested class
1730

1831
From the tested class you can go to the test class by clicking on the icon on the left of the class,
@@ -47,21 +60,6 @@ or by an entry bin the right click menu.
4760
The default keyboard shortcut to switch file is alt+shift+K.
4861

4962

50-
### Execute tests inside PhpStorm
51-
52-
By an entry on the run menu and on the right click menu.
53-
54-
![Demo](doc/run.png)
55-
56-
You can run the tests from both the test file or the tested classe's file :
57-
58-
![Demo](doc/run_from_tested_class.png)
59-
60-
Your test file and tested file will automatically be save before running the test.
61-
62-
The default keyboard shortcut to execute the test is alt+shift+M.
63-
64-
6563
### Easily identify test files by a custom icon
6664

6765
Atoum's test files are displayed with a different icon, like that you will easily differentiate them from other PHP files.

doc/run.png

16.4 KB
Loading

doc/run_from_tested_class.png

-166 KB
Binary file not shown.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.atoum.intellij.plugin.atoum;
2+
3+
import com.intellij.openapi.project.Project;
4+
import com.intellij.openapi.vfs.VirtualFile;
5+
import com.intellij.psi.PsiDirectory;
6+
import com.jetbrains.php.lang.psi.PhpFile;
7+
import org.codehaus.jettison.json.JSONException;
8+
import org.codehaus.jettison.json.JSONObject;
9+
import org.jetbrains.annotations.Nullable;
10+
11+
import java.io.File;
12+
import java.io.IOException;
13+
import java.nio.file.Files;
14+
import java.nio.file.Paths;
15+
16+
public class AtoumUtils {
17+
18+
public static VirtualFile findTestBaseDir(PhpFile phpFile, Project project)
19+
{
20+
Boolean continueSearch = true;
21+
Integer maxDirs = 35;
22+
Integer dirCount = 0;
23+
PsiDirectory currentDir = phpFile.getContainingDirectory();
24+
while (continueSearch) {
25+
dirCount++;
26+
if (currentDir.getVirtualFile().equals(project.getBaseDir())) {
27+
continueSearch = false;
28+
} else if (dirCount >= maxDirs) {
29+
continueSearch = false;
30+
} else {
31+
if (new File(currentDir.getVirtualFile().getPath() + "/composer.json").exists()) {
32+
return currentDir.getVirtualFile();
33+
}
34+
}
35+
currentDir = currentDir.getParentDirectory();
36+
if (null == currentDir) {
37+
return project.getBaseDir();
38+
}
39+
}
40+
41+
return project.getBaseDir();
42+
}
43+
44+
public static String findAtoumBinPath(VirtualFile dir)
45+
{
46+
String defaultBinPath = dir.getPath() + "/vendor/bin/atoum";
47+
48+
String binDir = getComposerBinDir(dir.getPath() + "/composer.json");
49+
String binPath = dir.getPath() + "/" + binDir + "/atoum";
50+
if (null != binDir && new File(binPath).exists()) {
51+
return binPath;
52+
}
53+
54+
return defaultBinPath;
55+
}
56+
57+
@Nullable
58+
protected static String getComposerBinDir(String composerPath) {
59+
try {
60+
String composerJsonContent = new String(Files.readAllBytes(Paths.get(composerPath)));
61+
JSONObject obj = new JSONObject(composerJsonContent);
62+
return obj.getJSONObject("config").get("bin-dir").toString();
63+
} catch (JSONException e) {
64+
return null;
65+
} catch (IOException e) {
66+
return null;
67+
}
68+
}
69+
70+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.atoum.intellij.plugin.atoum.actions;
2+
3+
import com.intellij.openapi.actionSystem.AnAction;
4+
import com.intellij.openapi.actionSystem.AnActionEvent;
5+
import com.intellij.openapi.actionSystem.PlatformDataKeys;
6+
import com.intellij.openapi.project.Project;
7+
import com.intellij.openapi.editor.Document;
8+
import com.intellij.openapi.fileEditor.FileDocumentManager;
9+
import com.jetbrains.php.lang.psi.PhpFile;
10+
import com.jetbrains.php.lang.psi.elements.PhpClass;
11+
import org.atoum.intellij.plugin.atoum.model.RunnerConfiguration;
12+
import org.atoum.intellij.plugin.atoum.run.Runner;
13+
import org.jetbrains.annotations.Nullable;
14+
import pl.projectspace.idea.plugins.php.atoum.actions.Utils;
15+
16+
public class Run extends AnAction {
17+
18+
@Override
19+
public void update(AnActionEvent event) {
20+
event.getPresentation().setEnabled(false);
21+
event.getPresentation().setVisible(true);
22+
event.getPresentation().setText("atoum - run test");
23+
24+
PhpClass currentTestClass = getCurrentTestClass(event);
25+
if (currentTestClass == null) {
26+
return;
27+
}
28+
29+
event.getPresentation().setText("atoum - run test : " + currentTestClass.getName());
30+
event.getPresentation().setEnabled(true);
31+
}
32+
33+
protected void saveFiles(PhpClass currentTestClass, Project project) {
34+
Document documentTestClass = FileDocumentManager.getInstance().getDocument(currentTestClass.getContainingFile().getVirtualFile());
35+
Document documentTestedClass = FileDocumentManager.getInstance().getDocument(Utils.locateTestedClass(project, currentTestClass).getContainingFile().getVirtualFile());
36+
FileDocumentManager.getInstance().saveDocument(documentTestClass);
37+
FileDocumentManager.getInstance().saveDocument(documentTestedClass);
38+
39+
}
40+
41+
public void actionPerformed(final AnActionEvent e) {
42+
PhpClass currentTestClass = getCurrentTestClass(e);
43+
if (currentTestClass == null) {
44+
return;
45+
}
46+
47+
Project project = e.getProject();
48+
49+
saveFiles(currentTestClass, project);
50+
51+
RunnerConfiguration runConfiguration = new RunnerConfiguration();
52+
runConfiguration.setFile((PhpFile)currentTestClass.getContainingFile());
53+
54+
Runner runner = new Runner(project);
55+
runner.run(runConfiguration);
56+
}
57+
58+
@Nullable
59+
protected PhpClass getCurrentTestClass(AnActionEvent e) {
60+
Object psiFile = e.getData(PlatformDataKeys.PSI_FILE);
61+
62+
if (null == psiFile) {
63+
return null;
64+
}
65+
66+
if (!(psiFile instanceof PhpFile)) {
67+
return null;
68+
}
69+
PhpFile phpFile = ((PhpFile) psiFile);
70+
71+
PhpClass currentClass = Utils.getFirstClassFromFile(phpFile);
72+
if (null == currentClass) {
73+
return null;
74+
}
75+
76+
if (!Utils.isClassAtoumTest(currentClass)) {
77+
return Utils.locateTestClass(e.getProject(), currentClass);
78+
}
79+
80+
return currentClass;
81+
}
82+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.atoum.intellij.plugin.atoum.model;
2+
3+
import java.util.ArrayList;
4+
5+
public class ClassResult
6+
{
7+
8+
public static String STATE_PASSED = "passed";
9+
public static String STATE_FAILED = "failed";
10+
public static String STATE_SKIPPED = "skipped";
11+
12+
protected ArrayList<MethodResult> methodsResults;
13+
14+
protected String name;
15+
16+
public ClassResult() {
17+
this.methodsResults = new ArrayList<MethodResult>();
18+
}
19+
20+
public void addMethodResult(MethodResult methodResult)
21+
{
22+
this.methodsResults.add(methodResult);
23+
}
24+
25+
public String getState()
26+
{
27+
if (hasMethodOfState(MethodResult.STATE_FAILED)) {
28+
return STATE_FAILED;
29+
}
30+
31+
if (hasMethodOfState(MethodResult.STATE_SKIPPED)) {
32+
return STATE_SKIPPED;
33+
}
34+
35+
if (hasMethodOfState(MethodResult.STATE_PASSED)) {
36+
return STATE_PASSED;
37+
}
38+
39+
return STATE_FAILED;
40+
}
41+
42+
private boolean hasMethodOfState(String state) {
43+
for (MethodResult methodsResult : this.methodsResults) {
44+
if (methodsResult.getState().equals(state)) {
45+
return true;
46+
}
47+
}
48+
return false;
49+
}
50+
51+
public Iterable<MethodResult> getMethods() {
52+
return this.methodsResults;
53+
}
54+
55+
public String getName() {
56+
return name;
57+
}
58+
59+
public void setName(String name) {
60+
this.name = name;
61+
}
62+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.atoum.intellij.plugin.atoum.model;
2+
3+
public class MethodResult {
4+
5+
public static String STATE_PASSED = "passed";
6+
public static String STATE_FAILED = "failed";
7+
public static String STATE_SKIPPED = "skipped";
8+
9+
protected String state = STATE_FAILED;
10+
11+
protected String name;
12+
protected String content;
13+
14+
public MethodResult(String name, String content) {
15+
this.name = name;
16+
this.content = content;
17+
}
18+
19+
public void definedStatePassed() {
20+
this.state = STATE_PASSED;
21+
}
22+
23+
public void definedStateFailed() {
24+
this.state = STATE_FAILED;
25+
}
26+
27+
public void definedStateSkipped() {
28+
this.state = STATE_SKIPPED;
29+
}
30+
31+
public String getName()
32+
{
33+
return this.name;
34+
}
35+
36+
public String getContent()
37+
{
38+
return this.content;
39+
}
40+
41+
public String getState()
42+
{
43+
return this.state;
44+
}
45+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.atoum.intellij.plugin.atoum.model;
2+
3+
import com.jetbrains.php.lang.psi.PhpFile;
4+
5+
public class RunnerConfiguration {
6+
7+
protected PhpFile file;
8+
9+
public PhpFile getFile () {
10+
return file;
11+
}
12+
13+
public void setFile(PhpFile file) {
14+
this.file = file;
15+
}
16+
}

0 commit comments

Comments
 (0)