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;
11 import java.io.BufferedWriter;
13 import java.io.FileWriter;
14 import java.io.IOException;
16 import net.sourceforge.phpeclipse.phpunit.preferences.PHPUnitPreferencePage;
17 import net.sourceforge.phpeclipse.phpunit.reporthandling.ConnectionListener;
18 import net.sourceforge.phpeclipse.phpunit.reporthandling.XMLReportHandler;
19 import net.sourceforge.phpeclipse.phpunit.testpool.TestCase;
20 import net.sourceforge.phpeclipse.phpunit.testpool.TestPool;
21 import net.sourceforge.phpeclipse.phpunit.testpool.TestSuite;
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.action.IToolBarManager;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.FileDialog;
29 import org.eclipse.ui.IActionBars;
30 import org.eclipse.ui.part.ViewPart;
32 public class PHPUnitView extends ViewPart {
35 * like J Unit a tree. The first level nodes are the test suites. children
36 * are nested test suites. leafs: test functions. hierarchy:
37 * package->testsuite1->testcase->test_function
40 private static PHPUnitView view = null;
42 private XMLReportHandler handler;
44 private TestPool testPool;
46 // private Button startButton;
48 private ProgressInfoComposite progressInfoComposite;
50 private ResultsInfoComposite resultsInfoComposite;
52 // private SettingsInfoComposite settingsInfoComposite; //TODO: move
53 // somewhere else, launcher, wizard or preferences.
54 private FileDialog dialog;
56 private String testSuiteToRun;
58 private Action selectTestAction;
60 private Action startTestAction;
62 public PHPUnitView() {
69 public static PHPUnitView getDefault() {
74 public void createPartControl(Composite parent) {
76 // parent.setLayout(new FillLayout(SWT.VERTICAL));
78 dialog = new FileDialog(parent.getShell());
80 GridLayout gridLayout = new GridLayout();
81 gridLayout.numColumns = 1;
83 // set title and layout
84 parent.setLayout(gridLayout);
89 // Build the progress info Composites
90 progressInfoComposite = new ProgressInfoComposite(parent);
91 progressInfoComposite.setLayoutData(new GridData(
92 GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL
93 | GridData.VERTICAL_ALIGN_BEGINNING));
95 // Build the result info composite
96 resultsInfoComposite = new ResultsInfoComposite(parent);
97 resultsInfoComposite.setLayoutData(new GridData(GridData.GRAB_VERTICAL
98 | GridData.FILL_BOTH));
100 // build the settings composite
101 // buildSettingsComposite(parent);
103 // settingsInfoComposite = new SettingsInfoComposite(parent, SWT.NONE);
105 // startButton = new Button(parent, SWT.CENTER);
106 // startButton.setText("Start Tests");
107 // startButton.addMouseListener(new MouseListener() {
109 // public void mouseDoubleClick(MouseEvent arg0) {
113 // public void mouseDown(MouseEvent arg0) {
116 // String testFile = settingsInfoComposite.getTestSuite();
117 // startTests(testFile);
118 // } catch (IOException e) {
119 // // TODO Auto-generated catch block
120 // e.printStackTrace();
126 // public void mouseUp(MouseEvent arg0) {
131 // }); // end add action listener.
138 private void buildSettingsComposite(Composite parent) {
140 // settingsInfoComposite = new Group(parent, SWT.NONE);
141 // settingsInfoComposite.setText("Settings");
142 // settingsInfoComposite.setLayout(new GridLayout(2,false));
145 // //the test suite to launch
146 // Label testSuiteLabel = new Label(settingsInfoComposite, SWT.NONE);
147 // testSuiteLabel.setText("Test suite to run:");
148 // //testSuiteLabel.setLayoutData(new GridData())
149 // Text testSuiteText = new Text(settingsInfoComposite, SWT.NONE);
152 // Label phpPathLabel = new Label(settingsInfoComposite, SWT.NONE);
153 // phpPathLabel.setText("php Path:");
154 // //testSuiteLabel.setLayoutData(new GridData())
155 // Text phpPathText = new Text(settingsInfoComposite, SWT.NONE);
159 private void setActions() {
161 final IActionBars actionBars = getViewSite().getActionBars();
162 IToolBarManager toolBarManager = actionBars.getToolBarManager();
164 selectTestAction = new Action() {
168 testSuiteToRun = dialog.open();
169 startTestAction.setEnabled(true);
173 selectTestAction.setText("Select Test Suite");
174 selectTestAction.setToolTipText("Select Test Suite");
176 .setImageDescriptor(PHPUnitImages.DESC_SELECT_TEST_SUITE);
178 toolBarManager.add(selectTestAction);
180 startTestAction = new Action() {
185 if (testSuiteToRun == null || testSuiteToRun == "")
188 startTests(testSuiteToRun);
189 // setEnabled(false);
191 } catch (IOException e) {
200 startTestAction.setText("Start Test");
202 .setToolTipText("Start Test Suite. Select a Test Suite first.");
203 startTestAction.setImageDescriptor(PHPUnitImages.DESC_RUN_TEST_SUITE);
204 startTestAction.setEnabled(false);
206 toolBarManager.add(startTestAction);
212 * @see org.eclipse.ui.IWorkbenchPart#setFocus()
214 public void setFocus() {
219 * mark the given test as passed in the GUI.
223 private void markTestPassed(String testID) {
225 // testid, use it in hashmap to retrieve tree item of test and
226 // change icon color, increment pass counter, etc...
228 testPool.getTest(testID).setVerdict(TestCase.PASS);
232 private void markTestFail(String testID) {
234 testPool.getTest(testID).setVerdict(TestCase.FAIL);
238 public void startTests(String testSuite) throws IOException {
240 // testSuite: the name of the file containing the suite we want to run.
241 // we will put that test suite inside a contained that uses our
244 // reset from previous run
247 testSuite = testSuite.replaceAll("\\\\", "/");
249 System.out.println("new: " + testSuite);
251 // where the plugin's temp files should go
252 String tempFolder = "C:\\tmp";
253 String tempFileName = "temTest.php";
256 File testFile = new File(tempFolder + "/" + tempFileName);
257 BufferedWriter out = new BufferedWriter(new FileWriter(testFile));
259 out.write("<?php" + "\n");
260 out.write("ob_start();" + "\n");
262 String path = PHPUnitPlugin.getDefault().getPreferenceStore()
263 .getString(PHPUnitPreferencePage.PHPUNIT_PATH);
265 out.write("$path = \"" + path + "\";" + "\n");
267 out.write("include_once($path . \"/phpunit_test.php\");" + "\n");
268 out.write("include_once $path . \"/socketTestResult.php\";" + "\n");
270 // include the test suite that we want to run.
271 String testSuiteName = "";
272 testSuiteName = testSuite.substring(testSuite.lastIndexOf('/') + 1,
273 testSuite.lastIndexOf('.'));
275 out.write("include_once(\"" + testSuite + "\");" + "\n");
277 out.write("" + "\n");
278 out.write("" + "\n");
280 out.write("$suite = new TestSuite();" + "\n");
281 out.write("$suite->addTest(new TestSuite(\"" + testSuiteName + "\"));"
284 out.write("$result = new SocketTestResult();" + "\n");
285 out.write("$suite->run($result);" + "\n");
286 out.write("$result->report(); " + "\n");
288 out.write("" + "\n");
289 out.write("" + "\n");
291 out.write("$output = ob_get_contents();" + "\n");
292 out.write("$fileHandle = fopen('c:/tmp/phpOut.txt');" + "\n");
293 out.write("fclose($fileHandle);" + "\n");
295 out.write("ob_end();" + "\n");
296 out.write("?>" + "\n");
304 Runtime.getRuntime().exec(
305 "php.exe " + tempFolder + "/" + tempFileName);
306 } catch (Exception e) {
311 // testFile.delete();
318 private void reset() {
320 handler = new XMLReportHandler();
321 testPool = new TestPool("Ali Baba");
323 progressInfoComposite.resetInfo();
324 resultsInfoComposite.resetInfo();
331 private void listenForReports() {
333 ConnectionListener conListener = new ConnectionListener();
334 conListener.start(this);
339 * handle this report: test passed, faile, end of all.
343 public void handleReport(String report) {
345 // delegate to the XML report handler.
346 handler.handle(report, this);
355 public void handleCommand(String command, String[] args) {
357 if (command.equals("startAll")) {
359 // markTestingStarted(new Integer(testCount).intValue());
361 } else if (command.equals("testSuiteStarted")) {
363 String testID = args[0];
364 String testCount = args[1];
366 // createNewTestSuite("TestSuiteName: " + testID, testID, new
367 // Integer(testCount).intValue());
368 TestSuite suite = new TestSuite(null, "TestSuiteName: " + testID,
369 testID, new Integer(testCount).intValue());
370 testPool.addTestSuite(suite);
372 } else if (command.equals("testStarted")) {
374 String testID = args[0];
375 String testCount = args[1];
376 String testName = args[2];
377 String parentTestSuiteName = args[3];
380 .addTest(new TestCase(testID, testName, parentTestSuiteName));
382 } else if (command.equals("testFINISHED")) {
384 } else if (command.equals("endAll")) {
395 private void update() {
397 // progressInfoComposite.updateInfo(numTests, testPool.getNumTestsRun(),
398 // numFailures, numErrors);
399 progressInfoComposite.updateInfo(testPool);
400 resultsInfoComposite.updateInfo(testPool);
405 * @param currentTestID
408 public void setTestVerdict(String currentTestID, String verdict) {
410 if (verdict.equals("passed"))
411 markTestPassed(currentTestID);
413 markTestFail(currentTestID);
418 * @param currentTestID
421 public void addTestException(String currentTestID, String exception) {
423 // TODO: decide how to show exceptions. don't show them for now.
424 // reportArea.append(" test " + currentTestID + " exception: " +
425 // exception + "\n");