Code folding for PHPdoc at start of PHP file
[phpeclipse.git] / net.sourceforge.phpeclipse.phpunit / src / net / sourceforge / phpeclipse / phpunit / PHPUnitView.java
1 /*************************************************************************
2  * @author Ali Echihabi (ali_echihabi@ieee.org, ali.echihabi@souss.ca)
3  *
4  * Plugin for PHP unit Testing.
5  * www.phpeclipse.de
6  * 
7  *************************************************************************/
8
9 package net.sourceforge.phpeclipse.phpunit;
10
11
12
13 import java.io.BufferedWriter;
14 import java.io.File;
15 import java.io.FileWriter;
16 import java.io.IOException;
17
18 import net.sourceforge.phpeclipse.phpunit.preferences.PHPUnitPreferencePage;
19 import net.sourceforge.phpeclipse.phpunit.reporthandling.ConnectionListener;
20 import net.sourceforge.phpeclipse.phpunit.reporthandling.XMLReportHandler;
21 import net.sourceforge.phpeclipse.phpunit.testpool.TestCase;
22 import net.sourceforge.phpeclipse.phpunit.testpool.TestPool;
23 import net.sourceforge.phpeclipse.phpunit.testpool.TestSuite;
24
25 import org.eclipse.jface.action.Action;
26 import org.eclipse.jface.action.IToolBarManager;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.FileDialog;
31 import org.eclipse.ui.IActionBars;
32 import org.eclipse.ui.part.ViewPart;
33
34
35
36 public class PHPUnitView extends ViewPart {
37
38
39
40
41         /*
42          * like J Unit
43          * a tree.
44          * The first level nodes are the test suites.
45          * children are nested test suites.
46          * leafs: test functions.
47          * hierarchy: package->testsuite1->testcase->test_function
48          */
49
50         private static PHPUnitView view = null;
51
52         private XMLReportHandler handler;
53         
54         private TestPool testPool;
55
56         //private Button startButton;
57
58         private ProgressInfoComposite progressInfoComposite;
59         private ResultsInfoComposite resultsInfoComposite;
60         //private SettingsInfoComposite settingsInfoComposite; //TODO: move somewhere else, launcher, wizard or preferences.
61         private FileDialog dialog;
62         private String testSuiteToRun;
63
64
65         private Action selectTestAction;
66         private Action startTestAction;
67
68
69         public PHPUnitView() {
70                 
71                 if(view == null)
72                         view = this;
73                         
74                         
75
76         }
77         
78         public static PHPUnitView getDefault() {
79                 
80                 
81                 return view; 
82         }
83
84         public void createPartControl(Composite parent) {
85
86                 //parent.setLayout(new FillLayout(SWT.VERTICAL));
87
88                 dialog = new FileDialog(parent.getShell());
89
90                 GridLayout gridLayout = new GridLayout();
91                 gridLayout.numColumns = 1;
92                 
93                 // set title and layout
94                 parent.setLayout(gridLayout);           
95                 
96
97                 //Launch ToolBar:
98                 setActions();
99
100                 //Build the progress info Composites            
101                 progressInfoComposite = new ProgressInfoComposite(parent);
102                 progressInfoComposite.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
103                 
104                 
105         
106                 //Build the result info composite
107                 resultsInfoComposite = new ResultsInfoComposite(parent);
108                 resultsInfoComposite.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
109                 
110                 //build the settings composite
111                 //buildSettingsComposite(parent);
112                 
113                 //settingsInfoComposite = new SettingsInfoComposite(parent, SWT.NONE);
114
115
116 //              startButton = new Button(parent, SWT.CENTER);
117 //              startButton.setText("Start Tests");
118 //              startButton.addMouseListener(new MouseListener() {
119 //
120 //                      public void mouseDoubleClick(MouseEvent arg0) {
121 //
122 //                      }
123 //
124 //                      public void mouseDown(MouseEvent arg0) {
125 //
126 //                              try {
127 //                                      String testFile = settingsInfoComposite.getTestSuite();
128 //                                      startTests(testFile);
129 //                              } catch (IOException e) {
130 //                                      // TODO Auto-generated catch block
131 //                                      e.printStackTrace();
132 //                              }
133 //
134 //
135 //                      }
136 //
137 //                      public void mouseUp(MouseEvent arg0) {
138 //
139 //
140 //                      }
141 //
142 //              }); // end add action listener.
143
144         }
145
146         /**
147          * @param parent
148          */
149         private void buildSettingsComposite(Composite parent) {
150                 
151                 
152                 //settingsInfoComposite = new Group(parent, SWT.NONE);
153                 //settingsInfoComposite.setText("Settings");
154 //              settingsInfoComposite.setLayout(new GridLayout(2,false));
155 //              
156 //              
157 //              //the test suite to launch
158 //              Label testSuiteLabel = new Label(settingsInfoComposite, SWT.NONE);
159 //              testSuiteLabel.setText("Test suite to run:");
160 //              //testSuiteLabel.setLayoutData(new GridData())
161 //              Text testSuiteText = new Text(settingsInfoComposite, SWT.NONE);
162 //              
163 //              //the path to php
164 //              Label phpPathLabel = new Label(settingsInfoComposite, SWT.NONE);
165 //              phpPathLabel.setText("php Path:");
166 //              //testSuiteLabel.setLayoutData(new GridData())
167 //              Text phpPathText = new Text(settingsInfoComposite, SWT.NONE);
168
169         }
170
171         private void setActions() {
172                 
173                 final IActionBars actionBars = getViewSite().getActionBars();
174                 IToolBarManager toolBarManager = actionBars.getToolBarManager();
175
176                 selectTestAction = new Action() {
177                         
178                         public void run() {
179                                                 
180                                 testSuiteToRun = dialog.open();
181                                 startTestAction.setEnabled(true);       
182                         }
183                 };
184                 
185                 selectTestAction.setText("Select Test Suite");
186                 selectTestAction.setToolTipText("Select Test Suite");           
187                 selectTestAction.setImageDescriptor(PHPUnitImages.DESC_SELECT_TEST_SUITE);
188                 
189                                 
190                 toolBarManager.add(selectTestAction);
191                 
192                 
193                 startTestAction = new Action() {
194                 
195                         public void run() {
196                                 
197                                 
198                                 try {
199                                         if(testSuiteToRun == null || testSuiteToRun == "")
200                                                 return;
201                                                 
202                                         startTests(testSuiteToRun);
203                                         //setEnabled(false);
204                                         
205                                 } catch (IOException e) {
206                                         
207                                         e.printStackTrace();
208                                 }
209                                         
210                         }
211                         
212                 };
213                 
214                 startTestAction.setText("Start Test");
215                 startTestAction.setToolTipText("Start Test Suite. Select a Test Suite first.");
216                 startTestAction.setImageDescriptor(PHPUnitImages.DESC_RUN_TEST_SUITE);
217                 startTestAction.setEnabled(false);
218         
219                 toolBarManager.add(startTestAction);
220         }
221
222
223
224         /* (non-Javadoc)
225          * @see org.eclipse.ui.IWorkbenchPart#setFocus()
226          */
227         public void setFocus() {
228                 
229         }
230
231         /**
232          * mark the given test as passed in the GUI.
233          * 
234          * @param testID
235          */
236         private void markTestPassed(String testID) {
237
238                 // testid, use it in hashmap to retrieve tree item of test and
239                 // change icon color, increment pass counter, etc...
240
241                 testPool.getTest(testID).setVerdict(TestCase.PASS);
242                 
243
244         }
245
246
247         private void markTestFail(String testID) {
248                 
249                 testPool.getTest(testID).setVerdict(TestCase.FAIL);
250                 
251         }
252
253
254
255         public void startTests(String testSuite) throws IOException {
256                 
257                 //testSuite: the name of the file containing the suite we want to run.
258                 // we will put that test suite inside a contained that uses our SocketResult.
259                 
260                 //reset from previous run
261                 reset();
262                 
263                 
264                 testSuite = testSuite.replaceAll("\\\\", "/");
265                 
266                 System.out.println("new: " +  testSuite);
267                 
268                 //where the plugin's temp files should go
269                 String tempFolder = "C:\\tmp"; 
270                 String tempFileName = "temTest.php";
271                 
272                 //create the file.
273                 File testFile = new File(tempFolder + "/" + tempFileName);
274                 BufferedWriter out = new BufferedWriter(new FileWriter(testFile));
275
276                 out.write("<?php" + "\n");
277                 out.write("ob_start();" + "\n");
278                                 
279                 String path = PHPUnitPlugin.getDefault().getPreferenceStore().getString(PHPUnitPreferencePage.PHPUNIT_PATH);
280
281                 
282                 out.write("$path = \"" + path + "\";" + "\n");
283                 
284                 out.write("include_once($path . \"/phpunit_test.php\");" + "\n");
285                 out.write("include_once $path . \"/socketTestResult.php\";" + "\n");
286
287                 //include the test suite that we want to run.
288                 String testSuiteName = "";
289                 testSuiteName = testSuite.substring(testSuite.lastIndexOf('/') + 1, testSuite.lastIndexOf('.'));
290
291                 out.write("include_once(\"" + testSuite + "\");" + "\n");
292                 
293
294                 out.write("" + "\n");
295                 out.write("" + "\n");           
296                 
297                 out.write("$suite = new TestSuite();" + "\n");
298                 out.write("$suite->addTest(new TestSuite(\"" + testSuiteName + "\"));" + "\n");
299                 
300         
301                 out.write("$result = new SocketTestResult();" + "\n");
302                 out.write("$suite->run($result);" + "\n");
303                 out.write("$result->report();   " + "\n");
304
305                 out.write("" + "\n");
306                 out.write("" + "\n");
307                 
308                 out.write("$output = ob_get_contents();" + "\n");
309                 out.write("$fileHandle = fopen('c:/tmp/phpOut.txt');" + "\n");
310                 out.write("fclose($fileHandle);" + "\n");
311                 
312                 
313                 out.write("ob_end();" + "\n");
314                 out.write("?>" + "\n");
315                 
316                 out.flush();
317                 out.close();
318                 
319                 listenForReports();
320                 
321                 try {
322                         Runtime.getRuntime().exec("php.exe " + tempFolder + "/" + tempFileName);
323                 } catch (Exception e) {
324                         
325                         e.printStackTrace();
326                 }
327                 
328                 //testFile.delete();
329                 
330         }
331
332         /**
333          * 
334          */
335         private void reset() {
336                 
337                 handler = new XMLReportHandler();
338                 testPool = new TestPool("Ali Baba");
339                  
340                 progressInfoComposite.resetInfo();
341                 resultsInfoComposite.resetInfo();
342                 
343         }
344
345         /**
346          * 
347          */
348         private void listenForReports() {
349
350                 ConnectionListener conListener = new ConnectionListener();
351                 conListener.start(this);
352
353         } //end of method
354
355         /**
356          * handle this report: test passed, faile, end of all.
357          * @param report
358          */
359         public void handleReport(String report) {
360
361                 //delegate to the XML report handler.           
362                 handler.handle(report, this);
363
364         }
365
366
367         /**
368          * @param command
369          * @param testCount
370          * @param testID
371          */
372         public void handleCommand(
373                 String command, String[] args) {
374         
375
376                 if (command.equals("startAll")) {
377                 
378                          //markTestingStarted(new Integer(testCount).intValue());
379                          
380                         
381                 } else if (command.equals("testSuiteStarted")) {
382
383                         String testID = args[0];
384                         String testCount = args[1];
385                         
386                         //createNewTestSuite("TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
387                         TestSuite suite = new TestSuite(null, "TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
388                         testPool.addTestSuite(suite);
389
390                 } else if (command.equals("testStarted")) {
391
392                         String testID = args[0];
393                         String testCount = args[1];
394                         String testName = args[2];
395                         String parentTestSuiteName = args[3];
396                         
397                         testPool.addTest(new TestCase(testID, testName, parentTestSuiteName));                  
398
399                 } else if (command.equals("testFINISHED")) {
400
401                         
402                         
403                 } else if (command.equals("endAll")) {
404
405                         
406                 }
407
408                 
409                 update();
410
411
412
413         }
414
415         /**
416          * 
417          */
418         private void update() {
419                 
420                 //progressInfoComposite.updateInfo(numTests, testPool.getNumTestsRun(), numFailures, numErrors);
421                 progressInfoComposite.updateInfo(testPool);
422                 resultsInfoComposite.updateInfo(testPool);
423                 
424         }
425
426
427         /**
428          * @param currentTestID
429          * @param verdict
430          */
431         public void setTestVerdict(String currentTestID, String verdict) {
432
433                 if (verdict.equals("passed"))
434                         markTestPassed(currentTestID);
435                 else
436                         markTestFail(currentTestID);
437
438         }
439
440         /**
441          * @param currentTestID
442          * @param exception
443          */
444         public void addTestException(String currentTestID, String exception) {
445
446                 //TODO: decide how to show exceptions. don't show them for now.
447                 //reportArea.append("   test " + currentTestID + " exception: " + exception + "\n");
448
449         }
450
451
452
453
454 } //end of class