1 package net.sourceforge.phpeclipse.phpunit;
5 import java.io.BufferedWriter;
7 import java.io.FileWriter;
8 import java.io.IOException;
10 import net.sourceforge.phpeclipse.phpunit.testpool.TestCase;
11 import net.sourceforge.phpeclipse.phpunit.testpool.TestPool;
12 import net.sourceforge.phpeclipse.phpunit.testpool.TestSuite;
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.action.IToolBarManager;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.FileDialog;
20 import org.eclipse.ui.IActionBars;
21 import org.eclipse.ui.part.ViewPart;
24 * @author Ali Echihabi
26 * To change the template for this generated type comment go to
27 * Window>Preferences>Java>Code Generation>Code and Comments
30 * Created on May 22, 2004
32 * To change the template for this generated file go to
33 * Window>Preferences>Java>Code Generation>Code and Comments
37 * @author Ali Echihabi (ali_echihabi@ieee.org)
39 * Plugin for PHP unit Testing.
42 * This the main view showing the progress and reports.
46 public class PHPUnitView extends ViewPart {
54 * The first level nodes are the test suites.
55 * children are nested test suites.
56 * leafs: test functions.
57 * hierarchy: package->testsuite1->testcase->test_function
60 private static PHPUnitView view = null;
62 private XMLReportHandler handler;
64 private TestPool testPool;
66 //private Button startButton;
68 private ProgressInfoComposite progressInfoComposite;
69 private ResultsInfoComposite resultsInfoComposite;
70 //private SettingsInfoComposite settingsInfoComposite; //TODO: move somewhere else, launcher, wizard or preferences.
71 private FileDialog dialog;
73 private String testSuiteToRun = "";
76 public PHPUnitView() {
85 public static PHPUnitView getDefault() {
91 public void createPartControl(Composite parent) {
93 //parent.setLayout(new FillLayout(SWT.VERTICAL));
95 dialog = new FileDialog(parent.getShell());
97 GridLayout gridLayout = new GridLayout();
98 gridLayout.numColumns = 1;
100 // set title and layout
101 parent.setLayout(gridLayout);
107 //Build the progress info Composites
108 progressInfoComposite = new ProgressInfoComposite(parent);
109 progressInfoComposite.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
113 //Build the result info composite
114 resultsInfoComposite = new ResultsInfoComposite(parent);
115 resultsInfoComposite.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
117 //build the settings composite
118 //buildSettingsComposite(parent);
120 //settingsInfoComposite = new SettingsInfoComposite(parent, SWT.NONE);
123 // startButton = new Button(parent, SWT.CENTER);
124 // startButton.setText("Start Tests");
125 // startButton.addMouseListener(new MouseListener() {
127 // public void mouseDoubleClick(MouseEvent arg0) {
131 // public void mouseDown(MouseEvent arg0) {
134 // String testFile = settingsInfoComposite.getTestSuite();
135 // startTests(testFile);
136 // } catch (IOException e) {
137 // // TODO Auto-generated catch block
138 // e.printStackTrace();
144 // public void mouseUp(MouseEvent arg0) {
149 // }); // end add action listener.
156 private void buildSettingsComposite(Composite parent) {
159 //settingsInfoComposite = new Group(parent, SWT.NONE);
160 //settingsInfoComposite.setText("Settings");
161 // settingsInfoComposite.setLayout(new GridLayout(2,false));
164 // //the test suite to launch
165 // Label testSuiteLabel = new Label(settingsInfoComposite, SWT.NONE);
166 // testSuiteLabel.setText("Test suite to run:");
167 // //testSuiteLabel.setLayoutData(new GridData())
168 // Text testSuiteText = new Text(settingsInfoComposite, SWT.NONE);
171 // Label phpPathLabel = new Label(settingsInfoComposite, SWT.NONE);
172 // phpPathLabel.setText("php Path:");
173 // //testSuiteLabel.setLayoutData(new GridData())
174 // Text phpPathText = new Text(settingsInfoComposite, SWT.NONE);
178 private void setActions() {
179 final IActionBars actionBars = getViewSite().getActionBars();
180 IToolBarManager toolBarManager = actionBars.getToolBarManager();
182 Action selectTestAction = new Action() {
186 testSuiteToRun = dialog.open();
190 selectTestAction.setText("Select Test Suite");
191 selectTestAction.setToolTipText("Select Test Suite");
192 selectTestAction.setImageDescriptor(PHPUnitImages.DESC_SELECT_TEST_SUITE);
195 toolBarManager.add(selectTestAction);
198 Action startTestAction = new Action() {
204 if(testSuiteToRun == null || testSuiteToRun == "")
207 startTests(testSuiteToRun);
208 } catch (IOException e) {
209 // TODO Auto-generated catch block
217 startTestAction.setText("Start Test");
218 startTestAction.setToolTipText("Start Test Suite");
219 startTestAction.setImageDescriptor(PHPUnitImages.DESC_RUN_TEST_SUITE);
222 toolBarManager.add(startTestAction);
228 * @see org.eclipse.ui.IWorkbenchPart#setFocus()
230 public void setFocus() {
235 * mark the given test as passed in the GUI.
239 private void markTestPassed(String testID) {
241 // testid, use it in hashmap to retrieve tree item of test and
242 // change icon color, increment pass counter, etc...
244 testPool.getTest(testID).setVerdict(TestCase.PASS);
250 private void markTestFail(String testID) {
252 testPool.getTest(testID).setVerdict(TestCase.FAIL);
258 public void startTests(String testSuite) throws IOException {
260 //testSuite: the name of the file containing the suite we want to run.
261 // we will put that test suite inside a contained that uses our SocketResult.
263 //reset from previous run
267 testSuite = testSuite.replaceAll("\\\\", "/");
269 System.out.println("new: " + testSuite);
271 //where the plugin's temp files should go
272 String tempFolder = "C:\\tmp";
273 String tempFileName = "temTest.php";
276 File testFile = new File(tempFolder + "/" + tempFileName);
277 BufferedWriter out = new BufferedWriter(new FileWriter(testFile));
279 out.write("<?php" + "\n");
280 out.write("ob_start();" + "\n");
282 //TODO: use install dir for path to phpunit. include it with plugin.
283 out.write("$path = \"C:/Documents and Settings/Ali Echihabi/My Documents/workspace.eclipse2.1/PHPUnit/phpunit\";" + "\n");
285 out.write("include_once($path . \"/phpunit_test.php\");" + "\n");
286 out.write("include_once $path . \"/socketTestResult.php\";" + "\n");
288 //include the test suite that we want to run.
289 String testSuiteName = "";
290 testSuiteName = testSuite.substring(testSuite.lastIndexOf('/') + 1, testSuite.lastIndexOf('.'));
292 out.write("include_once(\"" + testSuite + "\");" + "\n");
295 out.write("" + "\n");
296 out.write("" + "\n");
298 out.write("$suite = new TestSuite();" + "\n");
299 out.write("$suite->addTest(new TestSuite(\"" + testSuiteName + "\"));" + "\n");
302 out.write("$result = new SocketTestResult();" + "\n");
303 out.write("$suite->run($result);" + "\n");
304 out.write("$result->report(); " + "\n");
306 out.write("" + "\n");
307 out.write("" + "\n");
309 out.write("$output = ob_get_contents();" + "\n");
310 out.write("$fileHandle = fopen('c:/tmp/phpOut.txt');" + "\n");
311 out.write("fclose($fileHandle);" + "\n");
314 out.write("ob_end();" + "\n");
315 out.write("?>" + "\n");
323 Runtime.getRuntime().exec("php.exe " + tempFolder + "/" + tempFileName);
324 } catch (Exception e) {
336 private void reset() {
338 handler = new XMLReportHandler();
339 testPool = new TestPool("Ali Baba");
341 progressInfoComposite.resetInfo();
342 resultsInfoComposite.resetInfo();
349 private void listenForReports() {
351 ConnectionListener conListener = new ConnectionListener();
352 conListener.start(this);
357 * handle this report: test passed, faile, end of all.
360 public void handleReport(String report) {
362 //delegate to the XML report handler.
363 handler.handle(report, this);
373 public void handleCommand(
374 String command, String[] args) {
377 if (command.equals("startAll")) {
379 //markTestingStarted(new Integer(testCount).intValue());
382 } else if (command.equals("testSuiteStarted")) {
384 String testID = args[0];
385 String testCount = args[1];
387 //createNewTestSuite("TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
388 TestSuite suite = new TestSuite(null, "TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
389 testPool.addTestSuite(suite);
391 } else if (command.equals("testStarted")) {
393 String testID = args[0];
394 String testCount = args[1];
395 String testName = args[2];
396 String parentTestSuiteName = args[3];
398 testPool.addTest(new TestCase(testID, testName, parentTestSuiteName));
400 } else if (command.equals("testFINISHED")) {
404 } else if (command.equals("endAll")) {
419 private void update() {
421 //progressInfoComposite.updateInfo(numTests, testPool.getNumTestsRun(), numFailures, numErrors);
422 progressInfoComposite.updateInfo(testPool);
423 resultsInfoComposite.updateInfo(testPool);
429 * @param currentTestID
432 public void setTestVerdict(String currentTestID, String verdict) {
434 if (verdict.equals("passed"))
435 markTestPassed(currentTestID);
437 markTestFail(currentTestID);
442 * @param currentTestID
445 public void addTestException(String currentTestID, String exception) {
447 //TODO: decide how to show exceptions. don't show them for now.
448 //reportArea.append(" test " + currentTestID + " exception: " + exception + "\n");