1414import com .intellij .openapi .actionSystem .PlatformDataKeys ;
1515import com .intellij .openapi .project .Project ;
1616import com .intellij .openapi .util .Key ;
17+ import com .intellij .openapi .vfs .VirtualFile ;
1718import com .intellij .openapi .wm .ToolWindow ;
1819import com .intellij .openapi .wm .ToolWindowAnchor ;
1920import com .intellij .openapi .wm .ToolWindowManager ;
21+ import com .intellij .psi .PsiDirectory ;
2022import com .intellij .ui .content .Content ;
2123import com .intellij .ui .content .ContentManager ;
2224import com .jetbrains .php .lang .psi .PhpFile ;
2527import org .codehaus .jettison .json .JSONObject ;
2628import org .jetbrains .annotations .Nullable ;
2729
28- import java .io .BufferedReader ;
2930import java .io .File ;
3031import java .io .IOException ;
31- import java .io .InputStreamReader ;
3232import java .nio .file .Files ;
3333import java .nio .file .Paths ;
3434import java .util .Arrays ;
@@ -75,9 +75,10 @@ protected String runTest(PhpClass currentTestClass, Project project) {
7575 commandLineArgs [0 ] = currentTestClass .getContainingFile ().getVirtualFile ().getPath ();
7676
7777 try {
78+ VirtualFile testBaseDir = findTestBaseDir (currentTestClass , project );
7879 OSProcessHandler processHandler = ScriptRunnerUtil .execute (
79- findAtoumBinPath (project ),
80- project . getBasePath (),
80+ findAtoumBinPath (testBaseDir ),
81+ testBaseDir . getPath (),
8182 null ,
8283 commandLineArgs
8384 );
@@ -102,12 +103,38 @@ public void onTextAvailable(ProcessEvent event, Key outputType) {
102103 }
103104 }
104105
105- protected String findAtoumBinPath ( Project project )
106+ protected VirtualFile findTestBaseDir ( PhpClass currentTestClass , Project project )
106107 {
107- String defaultBinPath = project .getBasePath () + "/vendor/bin/atoum" ;
108+ Boolean continueSearch = true ;
109+ Integer maxDirs = 35 ;
110+ Integer dirCount = 0 ;
111+ PsiDirectory currentDir = currentTestClass .getContainingFile ().getContainingDirectory ();
112+ while (continueSearch ) {
113+ dirCount ++;
114+ if (currentDir .getVirtualFile ().equals (project .getBaseDir ())) {
115+ continueSearch = false ;
116+ } else if (dirCount >= maxDirs ) {
117+ continueSearch = false ;
118+ } else {
119+ if (new File (currentDir .getVirtualFile ().getPath () + "/composer.json" ).exists ()) {
120+ return currentDir .getVirtualFile ();
121+ }
122+ }
123+ currentDir = currentDir .getParentDirectory ();
124+ if (null == currentDir ) {
125+ return project .getBaseDir ();
126+ }
127+ }
128+
129+ return project .getBaseDir ();
130+ }
131+
132+ protected String findAtoumBinPath (VirtualFile dir )
133+ {
134+ String defaultBinPath = dir .getPath () + "/vendor/bin/atoum" ;
108135
109- String binDir = getComposerBinDir (project . getBasePath () + "/composer.json" );
110- String binPath = project . getBasePath () + "/" + binDir + "/atoum" ;
136+ String binDir = getComposerBinDir (dir . getPath () + "/composer.json" );
137+ String binPath = dir . getPath () + "/" + binDir + "/atoum" ;
111138 if (null != binDir && new File (binPath ).exists ()) {
112139 return binPath ;
113140 }
0 commit comments