1 /*************************************************************************
2 * @author Ali Echihabi (ali_echihabi@ieee.org, ali.echihabi@souss.ca)
4 * Plugin for PHP unit Testing.
7 *************************************************************************/
9 package net.sourceforge.phpeclipse.phpunit;
13 import java.io.BufferedWriter;
15 import java.io.FileWriter;
16 import java.io.IOException;
18 import net.sourceforge.phpeclipse.phpunit.preferences.PHPUnitPreferencePage;
19 import net.sourceforge.phpeclipse.phpunit.reporthandling.*;
20 import net.sourceforge.phpeclipse.phpunit.testpool.TestCase;
21 import net.sourceforge.phpeclipse.phpunit.testpool.TestPool;
22 import net.sourceforge.phpeclipse.phpunit.testpool.TestSuite;
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.action.IToolBarManager;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.FileDialog;
30 import org.eclipse.ui.IActionBars;
31 import org.eclipse.ui.part.ViewPart;
35 public class PHPUnitView extends ViewPart {
43 * The first level nodes are the test suites.
44 * children are nested test suites.
45 * leafs: test functions.
46 * hierarchy: package->testsuite1->testcase->test_function
49 private static PHPUnitView view = null;
51 private XMLReportHandler handler;
53 private TestPool testPool;
55 //private Button startButton;
57 private ProgressInfoComposite progressInfoComposite;
58 private ResultsInfoComposite resultsInfoComposite;
59 //private SettingsInfoComposite settingsInfoComposite; //TODO: move somewhere else, launcher, wizard or preferences.
60 private FileDialog dialog;
61 private String testSuiteToRun;
64 private Action selectTestAction;
65 private Action startTestAction;
68 public PHPUnitView() {
77 public static PHPUnitView getDefault() {
83 public void createPartControl(Composite parent) {
85 //parent.setLayout(new FillLayout(SWT.VERTICAL));
87 dialog = new FileDialog(parent.getShell());
89 GridLayout gridLayout = new GridLayout();
90 gridLayout.numColumns = 1;
92 // set title and layout
93 parent.setLayout(gridLayout);
99 //Build the progress info Composites
100 progressInfoComposite = new ProgressInfoComposite(parent);
101 progressInfoComposite.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
105 //Build the result info composite
106 resultsInfoComposite = new ResultsInfoComposite(parent);
107 resultsInfoComposite.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
109 //build the settings composite
110 //buildSettingsComposite(parent);
112 //settingsInfoComposite = new SettingsInfoComposite(parent, SWT.NONE);
115 // startButton = new Button(parent, SWT.CENTER);
116 // startButton.setText("Start Tests");
117 // startButton.addMouseListener(new MouseListener() {
119 // public void mouseDoubleClick(MouseEvent arg0) {
123 // public void mouseDown(MouseEvent arg0) {
126 // String testFile = settingsInfoComposite.getTestSuite();
127 // startTests(testFile);
128 // } catch (IOException e) {
129 // // TODO Auto-generated catch block
130 // e.printStackTrace();
136 // public void mouseUp(MouseEvent arg0) {
141 // }); // end add action listener.
148 private void buildSettingsComposite(Composite parent) {
151 //settingsInfoComposite = new Group(parent, SWT.NONE);
152 //settingsInfoComposite.setText("Settings");
153 // settingsInfoComposite.setLayout(new GridLayout(2,false));
156 // //the test suite to launch
157 // Label testSuiteLabel = new Label(settingsInfoComposite, SWT.NONE);
158 // testSuiteLabel.setText("Test suite to run:");
159 // //testSuiteLabel.setLayoutData(new GridData())
160 // Text testSuiteText = new Text(settingsInfoComposite, SWT.NONE);
163 // Label phpPathLabel = new Label(settingsInfoComposite, SWT.NONE);
164 // phpPathLabel.setText("php Path:");
165 // //testSuiteLabel.setLayoutData(new GridData())
166 // Text phpPathText = new Text(settingsInfoComposite, SWT.NONE);
170 private void setActions() {
172 final IActionBars actionBars = getViewSite().getActionBars();
173 IToolBarManager toolBarManager = actionBars.getToolBarManager();
175 selectTestAction = new Action() {
179 testSuiteToRun = dialog.open();
180 startTestAction.setEnabled(true);
184 selectTestAction.setText("Select Test Suite");
185 selectTestAction.setToolTipText("Select Test Suite");
186 selectTestAction.setImageDescriptor(PHPUnitImages.DESC_SELECT_TEST_SUITE);
189 toolBarManager.add(selectTestAction);
192 startTestAction = new Action() {
198 if(testSuiteToRun == null || testSuiteToRun == "")
201 startTests(testSuiteToRun);
204 } catch (IOException e) {
213 startTestAction.setText("Start Test");
214 startTestAction.setToolTipText("Start Test Suite. Select a Test Suite first.");
215 startTestAction.setImageDescriptor(PHPUnitImages.DESC_RUN_TEST_SUITE);
216 startTestAction.setEnabled(false);
218 toolBarManager.add(startTestAction);
224 * @see org.eclipse.ui.IWorkbenchPart#setFocus()
226 public void setFocus() {
231 * mark the given test as passed in the GUI.
235 private void markTestPassed(String testID) {
237 // testid, use it in hashmap to retrieve tree item of test and
238 // change icon color, increment pass counter, etc...
240 testPool.getTest(testID).setVerdict(TestCase.PASS);
246 private void markTestFail(String testID) {
248 testPool.getTest(testID).setVerdict(TestCase.FAIL);
254 public void startTests(String testSuite) throws IOException {
256 //testSuite: the name of the file containing the suite we want to run.
257 // we will put that test suite inside a contained that uses our SocketResult.
259 //reset from previous run
263 testSuite = testSuite.replaceAll("\\\\", "/");
265 System.out.println("new: " + testSuite);
267 //where the plugin's temp files should go
268 String tempFolder = "C:\\tmp";
269 String tempFileName = "temTest.php";
272 File testFile = new File(tempFolder + "/" + tempFileName);
273 BufferedWriter out = new BufferedWriter(new FileWriter(testFile));
275 out.write("<?php" + "\n");
276 out.write("ob_start();" + "\n");
278 String path = PHPUnitPlugin.getDefault().getPreferenceStore().getString(PHPUnitPreferencePage.PHPUNIT_PATH);
281 out.write("$path = \"" + path + "\";" + "\n");
283 out.write("include_once($path . \"/phpunit_test.php\");" + "\n");
284 out.write("include_once $path . \"/socketTestResult.php\";" + "\n");
286 //include the test suite that we want to run.
287 String testSuiteName = "";
288 testSuiteName = testSuite.substring(testSuite.lastIndexOf('/') + 1, testSuite.lastIndexOf('.'));
290 out.write("include_once(\"" + testSuite + "\");" + "\n");
293 out.write("" + "\n");
294 out.write("" + "\n");
296 out.write("$suite = new TestSuite();" + "\n");
297 out.write("$suite->addTest(new TestSuite(\"" + testSuiteName + "\"));" + "\n");
300 out.write("$result = new SocketTestResult();" + "\n");
301 out.write("$suite->run($result);" + "\n");
302 out.write("$result->report(); " + "\n");
304 out.write("" + "\n");
305 out.write("" + "\n");
307 out.write("$output = ob_get_contents();" + "\n");
308 out.write("$fileHandle = fopen('c:/tmp/phpOut.txt');" + "\n");
309 out.write("fclose($fileHandle);" + "\n");
312 out.write("ob_end();" + "\n");
313 out.write("?>" + "\n");
321 Runtime.getRuntime().exec("php.exe " + tempFolder + "/" + tempFileName);
322 } catch (Exception e) {
334 private void reset() {
336 handler = new XMLReportHandler();
337 testPool = new TestPool("Ali Baba");
339 progressInfoComposite.resetInfo();
340 resultsInfoComposite.resetInfo();
347 private void listenForReports() {
349 ConnectionListener conListener = new ConnectionListener();
350 conListener.start(this);
355 * handle this report: test passed, faile, end of all.
358 public void handleReport(String report) {
360 //delegate to the XML report handler.
361 handler.handle(report, this);
371 public void handleCommand(
372 String command, String[] args) {
375 if (command.equals("startAll")) {
377 //markTestingStarted(new Integer(testCount).intValue());
380 } else if (command.equals("testSuiteStarted")) {
382 String testID = args[0];
383 String testCount = args[1];
385 //createNewTestSuite("TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
386 TestSuite suite = new TestSuite(null, "TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
387 testPool.addTestSuite(suite);
389 } else if (command.equals("testStarted")) {
391 String testID = args[0];
392 String testCount = args[1];
393 String testName = args[2];
394 String parentTestSuiteName = args[3];
396 testPool.addTest(new TestCase(testID, testName, parentTestSuiteName));
398 } else if (command.equals("testFINISHED")) {
402 } else if (command.equals("endAll")) {
417 private void update() {
419 //progressInfoComposite.updateInfo(numTests, testPool.getNumTestsRun(), numFailures, numErrors);
420 progressInfoComposite.updateInfo(testPool);
421 resultsInfoComposite.updateInfo(testPool);
427 * @param currentTestID
430 public void setTestVerdict(String currentTestID, String verdict) {
432 if (verdict.equals("passed"))
433 markTestPassed(currentTestID);
435 markTestFail(currentTestID);
440 * @param currentTestID
443 public void addTestException(String currentTestID, String exception) {
445 //TODO: decide how to show exceptions. don't show them for now.
446 //reportArea.append(" test " + currentTestID + " exception: " + exception + "\n");