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.preferences.PHPUnitPreferencePage;
11 import net.sourceforge.phpeclipse.phpunit.testpool.TestCase;
12 import net.sourceforge.phpeclipse.phpunit.testpool.TestPool;
13 import net.sourceforge.phpeclipse.phpunit.testpool.TestSuite;
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.action.IToolBarManager;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.FileDialog;
21 import org.eclipse.ui.IActionBars;
22 import org.eclipse.ui.part.ViewPart;
25 * @author Ali Echihabi
27 * To change the template for this generated type comment go to
28 * Window>Preferences>Java>Code Generation>Code and Comments
31 * Created on May 22, 2004
33 * To change the template for this generated file go to
34 * Window>Preferences>Java>Code Generation>Code and Comments
38 * @author Ali Echihabi (ali_echihabi@ieee.org)
40 * Plugin for PHP unit Testing.
43 * This the main view showing the progress and reports.
47 public class PHPUnitView extends ViewPart {
55 * The first level nodes are the test suites.
56 * children are nested test suites.
57 * leafs: test functions.
58 * hierarchy: package->testsuite1->testcase->test_function
61 private static PHPUnitView view = null;
63 private XMLReportHandler handler;
65 private TestPool testPool;
67 //private Button startButton;
69 private ProgressInfoComposite progressInfoComposite;
70 private ResultsInfoComposite resultsInfoComposite;
71 //private SettingsInfoComposite settingsInfoComposite; //TODO: move somewhere else, launcher, wizard or preferences.
72 private FileDialog dialog;
74 private String testSuiteToRun = "";
77 public PHPUnitView() {
86 public static PHPUnitView getDefault() {
92 public void createPartControl(Composite parent) {
94 //parent.setLayout(new FillLayout(SWT.VERTICAL));
96 dialog = new FileDialog(parent.getShell());
98 GridLayout gridLayout = new GridLayout();
99 gridLayout.numColumns = 1;
101 // set title and layout
102 parent.setLayout(gridLayout);
108 //Build the progress info Composites
109 progressInfoComposite = new ProgressInfoComposite(parent);
110 progressInfoComposite.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
114 //Build the result info composite
115 resultsInfoComposite = new ResultsInfoComposite(parent);
116 resultsInfoComposite.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
118 //build the settings composite
119 //buildSettingsComposite(parent);
121 //settingsInfoComposite = new SettingsInfoComposite(parent, SWT.NONE);
124 // startButton = new Button(parent, SWT.CENTER);
125 // startButton.setText("Start Tests");
126 // startButton.addMouseListener(new MouseListener() {
128 // public void mouseDoubleClick(MouseEvent arg0) {
132 // public void mouseDown(MouseEvent arg0) {
135 // String testFile = settingsInfoComposite.getTestSuite();
136 // startTests(testFile);
137 // } catch (IOException e) {
138 // // TODO Auto-generated catch block
139 // e.printStackTrace();
145 // public void mouseUp(MouseEvent arg0) {
150 // }); // end add action listener.
157 private void buildSettingsComposite(Composite parent) {
160 //settingsInfoComposite = new Group(parent, SWT.NONE);
161 //settingsInfoComposite.setText("Settings");
162 // settingsInfoComposite.setLayout(new GridLayout(2,false));
165 // //the test suite to launch
166 // Label testSuiteLabel = new Label(settingsInfoComposite, SWT.NONE);
167 // testSuiteLabel.setText("Test suite to run:");
168 // //testSuiteLabel.setLayoutData(new GridData())
169 // Text testSuiteText = new Text(settingsInfoComposite, SWT.NONE);
172 // Label phpPathLabel = new Label(settingsInfoComposite, SWT.NONE);
173 // phpPathLabel.setText("php Path:");
174 // //testSuiteLabel.setLayoutData(new GridData())
175 // Text phpPathText = new Text(settingsInfoComposite, SWT.NONE);
179 private void setActions() {
180 final IActionBars actionBars = getViewSite().getActionBars();
181 IToolBarManager toolBarManager = actionBars.getToolBarManager();
183 Action selectTestAction = new Action() {
187 testSuiteToRun = dialog.open();
191 selectTestAction.setText("Select Test Suite");
192 selectTestAction.setToolTipText("Select Test Suite");
193 selectTestAction.setImageDescriptor(PHPUnitImages.DESC_SELECT_TEST_SUITE);
196 toolBarManager.add(selectTestAction);
199 Action startTestAction = new Action() {
205 if(testSuiteToRun == null || testSuiteToRun == "")
208 startTests(testSuiteToRun);
209 } catch (IOException e) {
210 // TODO Auto-generated catch block
218 startTestAction.setText("Start Test");
219 startTestAction.setToolTipText("Start Test Suite");
220 startTestAction.setImageDescriptor(PHPUnitImages.DESC_RUN_TEST_SUITE);
223 toolBarManager.add(startTestAction);
229 * @see org.eclipse.ui.IWorkbenchPart#setFocus()
231 public void setFocus() {
236 * mark the given test as passed in the GUI.
240 private void markTestPassed(String testID) {
242 // testid, use it in hashmap to retrieve tree item of test and
243 // change icon color, increment pass counter, etc...
245 testPool.getTest(testID).setVerdict(TestCase.PASS);
251 private void markTestFail(String testID) {
253 testPool.getTest(testID).setVerdict(TestCase.FAIL);
259 public void startTests(String testSuite) throws IOException {
261 //testSuite: the name of the file containing the suite we want to run.
262 // we will put that test suite inside a contained that uses our SocketResult.
264 //reset from previous run
268 testSuite = testSuite.replaceAll("\\\\", "/");
270 System.out.println("new: " + testSuite);
272 //where the plugin's temp files should go
273 String tempFolder = "C:\\tmp";
274 String tempFileName = "temTest.php";
277 File testFile = new File(tempFolder + "/" + tempFileName);
278 BufferedWriter out = new BufferedWriter(new FileWriter(testFile));
280 out.write("<?php" + "\n");
281 out.write("ob_start();" + "\n");
283 String path = PHPUnitPlugin.getDefault().getPreferenceStore().getString(PHPUnitPreferencePage.PHPUNIT_PATH);
286 out.write("$path = \"" + path + "\";" + "\n");
288 out.write("include_once($path . \"/phpunit_test.php\");" + "\n");
289 out.write("include_once $path . \"/socketTestResult.php\";" + "\n");
291 //include the test suite that we want to run.
292 String testSuiteName = "";
293 testSuiteName = testSuite.substring(testSuite.lastIndexOf('/') + 1, testSuite.lastIndexOf('.'));
295 out.write("include_once(\"" + testSuite + "\");" + "\n");
298 out.write("" + "\n");
299 out.write("" + "\n");
301 out.write("$suite = new TestSuite();" + "\n");
302 out.write("$suite->addTest(new TestSuite(\"" + testSuiteName + "\"));" + "\n");
305 out.write("$result = new SocketTestResult();" + "\n");
306 out.write("$suite->run($result);" + "\n");
307 out.write("$result->report(); " + "\n");
309 out.write("" + "\n");
310 out.write("" + "\n");
312 out.write("$output = ob_get_contents();" + "\n");
313 out.write("$fileHandle = fopen('c:/tmp/phpOut.txt');" + "\n");
314 out.write("fclose($fileHandle);" + "\n");
317 out.write("ob_end();" + "\n");
318 out.write("?>" + "\n");
326 Runtime.getRuntime().exec("php.exe " + tempFolder + "/" + tempFileName);
327 } catch (Exception e) {
339 private void reset() {
341 handler = new XMLReportHandler();
342 testPool = new TestPool("Ali Baba");
344 progressInfoComposite.resetInfo();
345 resultsInfoComposite.resetInfo();
352 private void listenForReports() {
354 ConnectionListener conListener = new ConnectionListener();
355 conListener.start(this);
360 * handle this report: test passed, faile, end of all.
363 public void handleReport(String report) {
365 //delegate to the XML report handler.
366 handler.handle(report, this);
376 public void handleCommand(
377 String command, String[] args) {
380 if (command.equals("startAll")) {
382 //markTestingStarted(new Integer(testCount).intValue());
385 } else if (command.equals("testSuiteStarted")) {
387 String testID = args[0];
388 String testCount = args[1];
390 //createNewTestSuite("TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
391 TestSuite suite = new TestSuite(null, "TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
392 testPool.addTestSuite(suite);
394 } else if (command.equals("testStarted")) {
396 String testID = args[0];
397 String testCount = args[1];
398 String testName = args[2];
399 String parentTestSuiteName = args[3];
401 testPool.addTest(new TestCase(testID, testName, parentTestSuiteName));
403 } else if (command.equals("testFINISHED")) {
407 } else if (command.equals("endAll")) {
422 private void update() {
424 //progressInfoComposite.updateInfo(numTests, testPool.getNumTestsRun(), numFailures, numErrors);
425 progressInfoComposite.updateInfo(testPool);
426 resultsInfoComposite.updateInfo(testPool);
432 * @param currentTestID
435 public void setTestVerdict(String currentTestID, String verdict) {
437 if (verdict.equals("passed"))
438 markTestPassed(currentTestID);
440 markTestFail(currentTestID);
445 * @param currentTestID
448 public void addTestException(String currentTestID, String exception) {
450 //TODO: decide how to show exceptions. don't show them for now.
451 //reportArea.append(" test " + currentTestID + " exception: " + exception + "\n");