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.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;
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;
36 public class PHPUnitView extends ViewPart {
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
50 private static PHPUnitView view = null;
52 private XMLReportHandler handler;
54 private TestPool testPool;
56 //private Button startButton;
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;
65 private Action selectTestAction;
66 private Action startTestAction;
69 public PHPUnitView() {
78 public static PHPUnitView getDefault() {
84 public void createPartControl(Composite parent) {
86 //parent.setLayout(new FillLayout(SWT.VERTICAL));
88 dialog = new FileDialog(parent.getShell());
90 GridLayout gridLayout = new GridLayout();
91 gridLayout.numColumns = 1;
93 // set title and layout
94 parent.setLayout(gridLayout);
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));
106 //Build the result info composite
107 resultsInfoComposite = new ResultsInfoComposite(parent);
108 resultsInfoComposite.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
110 //build the settings composite
111 //buildSettingsComposite(parent);
113 //settingsInfoComposite = new SettingsInfoComposite(parent, SWT.NONE);
116 // startButton = new Button(parent, SWT.CENTER);
117 // startButton.setText("Start Tests");
118 // startButton.addMouseListener(new MouseListener() {
120 // public void mouseDoubleClick(MouseEvent arg0) {
124 // public void mouseDown(MouseEvent arg0) {
127 // String testFile = settingsInfoComposite.getTestSuite();
128 // startTests(testFile);
129 // } catch (IOException e) {
130 // // TODO Auto-generated catch block
131 // e.printStackTrace();
137 // public void mouseUp(MouseEvent arg0) {
142 // }); // end add action listener.
149 private void buildSettingsComposite(Composite parent) {
152 //settingsInfoComposite = new Group(parent, SWT.NONE);
153 //settingsInfoComposite.setText("Settings");
154 // settingsInfoComposite.setLayout(new GridLayout(2,false));
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);
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);
171 private void setActions() {
173 final IActionBars actionBars = getViewSite().getActionBars();
174 IToolBarManager toolBarManager = actionBars.getToolBarManager();
176 selectTestAction = new Action() {
180 testSuiteToRun = dialog.open();
181 startTestAction.setEnabled(true);
185 selectTestAction.setText("Select Test Suite");
186 selectTestAction.setToolTipText("Select Test Suite");
187 selectTestAction.setImageDescriptor(PHPUnitImages.DESC_SELECT_TEST_SUITE);
190 toolBarManager.add(selectTestAction);
193 startTestAction = new Action() {
199 if(testSuiteToRun == null || testSuiteToRun == "")
202 startTests(testSuiteToRun);
205 } catch (IOException e) {
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);
219 toolBarManager.add(startTestAction);
225 * @see org.eclipse.ui.IWorkbenchPart#setFocus()
227 public void setFocus() {
232 * mark the given test as passed in the GUI.
236 private void markTestPassed(String testID) {
238 // testid, use it in hashmap to retrieve tree item of test and
239 // change icon color, increment pass counter, etc...
241 testPool.getTest(testID).setVerdict(TestCase.PASS);
247 private void markTestFail(String testID) {
249 testPool.getTest(testID).setVerdict(TestCase.FAIL);
255 public void startTests(String testSuite) throws IOException {
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.
260 //reset from previous run
264 testSuite = testSuite.replaceAll("\\\\", "/");
266 System.out.println("new: " + testSuite);
268 //where the plugin's temp files should go
269 String tempFolder = "C:\\tmp";
270 String tempFileName = "temTest.php";
273 File testFile = new File(tempFolder + "/" + tempFileName);
274 BufferedWriter out = new BufferedWriter(new FileWriter(testFile));
276 out.write("<?php" + "\n");
277 out.write("ob_start();" + "\n");
279 String path = PHPUnitPlugin.getDefault().getPreferenceStore().getString(PHPUnitPreferencePage.PHPUNIT_PATH);
282 out.write("$path = \"" + path + "\";" + "\n");
284 out.write("include_once($path . \"/phpunit_test.php\");" + "\n");
285 out.write("include_once $path . \"/socketTestResult.php\";" + "\n");
287 //include the test suite that we want to run.
288 String testSuiteName = "";
289 testSuiteName = testSuite.substring(testSuite.lastIndexOf('/') + 1, testSuite.lastIndexOf('.'));
291 out.write("include_once(\"" + testSuite + "\");" + "\n");
294 out.write("" + "\n");
295 out.write("" + "\n");
297 out.write("$suite = new TestSuite();" + "\n");
298 out.write("$suite->addTest(new TestSuite(\"" + testSuiteName + "\"));" + "\n");
301 out.write("$result = new SocketTestResult();" + "\n");
302 out.write("$suite->run($result);" + "\n");
303 out.write("$result->report(); " + "\n");
305 out.write("" + "\n");
306 out.write("" + "\n");
308 out.write("$output = ob_get_contents();" + "\n");
309 out.write("$fileHandle = fopen('c:/tmp/phpOut.txt');" + "\n");
310 out.write("fclose($fileHandle);" + "\n");
313 out.write("ob_end();" + "\n");
314 out.write("?>" + "\n");
322 Runtime.getRuntime().exec("php.exe " + tempFolder + "/" + tempFileName);
323 } catch (Exception e) {
335 private void reset() {
337 handler = new XMLReportHandler();
338 testPool = new TestPool("Ali Baba");
340 progressInfoComposite.resetInfo();
341 resultsInfoComposite.resetInfo();
348 private void listenForReports() {
350 ConnectionListener conListener = new ConnectionListener();
351 conListener.start(this);
356 * handle this report: test passed, faile, end of all.
359 public void handleReport(String report) {
361 //delegate to the XML report handler.
362 handler.handle(report, this);
372 public void handleCommand(
373 String command, String[] args) {
376 if (command.equals("startAll")) {
378 //markTestingStarted(new Integer(testCount).intValue());
381 } else if (command.equals("testSuiteStarted")) {
383 String testID = args[0];
384 String testCount = args[1];
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);
390 } else if (command.equals("testStarted")) {
392 String testID = args[0];
393 String testCount = args[1];
394 String testName = args[2];
395 String parentTestSuiteName = args[3];
397 testPool.addTest(new TestCase(testID, testName, parentTestSuiteName));
399 } else if (command.equals("testFINISHED")) {
403 } else if (command.equals("endAll")) {
418 private void update() {
420 //progressInfoComposite.updateInfo(numTests, testPool.getNumTestsRun(), numFailures, numErrors);
421 progressInfoComposite.updateInfo(testPool);
422 resultsInfoComposite.updateInfo(testPool);
428 * @param currentTestID
431 public void setTestVerdict(String currentTestID, String verdict) {
433 if (verdict.equals("passed"))
434 markTestPassed(currentTestID);
436 markTestFail(currentTestID);
441 * @param currentTestID
444 public void addTestException(String currentTestID, String exception) {
446 //TODO: decide how to show exceptions. don't show them for now.
447 //reportArea.append(" test " + currentTestID + " exception: " + exception + "\n");