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.SWT;
 
  17 import org.eclipse.swt.events.MouseEvent;
 
  18 import org.eclipse.swt.events.MouseListener;
 
  19 import org.eclipse.swt.layout.GridData;
 
  20 import org.eclipse.swt.layout.GridLayout;
 
  21 import org.eclipse.swt.widgets.Button;
 
  22 import org.eclipse.swt.widgets.Composite;
 
  23 import org.eclipse.ui.IActionBars;
 
  24 import org.eclipse.ui.ISharedImages;
 
  25 import org.eclipse.ui.PlatformUI;
 
  26 import org.eclipse.ui.part.ViewPart;
 
  29  * @author Ali Echihabi
 
  31  * To change the template for this generated type comment go to
 
  32  * Window>Preferences>Java>Code Generation>Code and Comments
 
  35  * Created on May 22, 2004
 
  37  * To change the template for this generated file go to
 
  38  * Window>Preferences>Java>Code Generation>Code and Comments
 
  42  * @author Ali Echihabi (ali_echihabi@ieee.org)
 
  44  * Plugin for PHP unit Testing.
 
  47  * This the main view showing the progress and reports.
 
  51 public class PHPUnitView extends ViewPart {
 
  59          * The first level nodes are the test suites.
 
  60          * children are nested test suites.
 
  61          * leafs: test functions.
 
  62          * hierarchy: package->testsuite1->testcase->test_function
 
  65         private static PHPUnitView view = null;
 
  67         private XMLReportHandler handler;
 
  69         private TestPool testPool;
 
  71         private Button startButton;
 
  73         private ProgressInfoComposite progressInfoComposite;
 
  74         private ResultsInfoComposite resultsInfoComposite;
 
  75         private SettingsInfoComposite settingsInfoComposite; //TODO: move somewhere else, launcher, wizard or preferences.
 
  79         public PHPUnitView() {
 
  86         public static PHPUnitView getDefault() {
 
  92         public void createPartControl(Composite parent) {
 
  94                 //parent.setLayout(new FillLayout(SWT.VERTICAL));
 
  96                 GridLayout gridLayout = new GridLayout();
 
  97                 gridLayout.numColumns = 1;
 
  99                 // set title and layout
 
 100                 parent.setLayout(gridLayout);           
 
 106                 //Build the progress info Composites            
 
 107                 progressInfoComposite = new ProgressInfoComposite(parent);
 
 108                 progressInfoComposite.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
 
 112                 //Build the result info composite
 
 113                 resultsInfoComposite = new ResultsInfoComposite(parent);
 
 114                 resultsInfoComposite.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
 
 116                 //build the settings composite
 
 117                 //buildSettingsComposite(parent);
 
 119                 settingsInfoComposite = new SettingsInfoComposite(parent, SWT.NONE);
 
 122                 startButton = new Button(parent, SWT.CENTER);
 
 123                 startButton.setText("Start Tests");
 
 124                 startButton.addMouseListener(new MouseListener() {
 
 126                         public void mouseDoubleClick(MouseEvent arg0) {
 
 130                         public void mouseDown(MouseEvent arg0) {
 
 136                         public void mouseUp(MouseEvent arg0) {
 
 139                                         String testFile = settingsInfoComposite.getTestSuite();
 
 140                                         startTests(testFile);
 
 141                                 } catch (IOException e) {
 
 142                                         // TODO Auto-generated catch block
 
 148                 }); // end add action listener.
 
 155         private void buildSettingsComposite(Composite parent) {
 
 158                 //settingsInfoComposite = new Group(parent, SWT.NONE);
 
 159                 //settingsInfoComposite.setText("Settings");
 
 160 //              settingsInfoComposite.setLayout(new GridLayout(2,false));
 
 163 //              //the test suite to launch
 
 164 //              Label testSuiteLabel = new Label(settingsInfoComposite, SWT.NONE);
 
 165 //              testSuiteLabel.setText("Test suite to run:");
 
 166 //              //testSuiteLabel.setLayoutData(new GridData())
 
 167 //              Text testSuiteText = new Text(settingsInfoComposite, SWT.NONE);
 
 170 //              Label phpPathLabel = new Label(settingsInfoComposite, SWT.NONE);
 
 171 //              phpPathLabel.setText("php Path:");
 
 172 //              //testSuiteLabel.setLayoutData(new GridData())
 
 173 //              Text phpPathText = new Text(settingsInfoComposite, SWT.NONE);
 
 177         private void setActions() {
 
 178                 final IActionBars actionBars = getViewSite().getActionBars();
 
 179                 IToolBarManager toolBarManager = actionBars.getToolBarManager();
 
 180                 Action action1 = new Action() {};
 
 181                 action1.setText("Action 1");
 
 182                 action1.setToolTipText("Start the testing");
 
 183                 //final URL installUrl = PaintPlugin.getDefault().getDescriptor().getInstallURL();
 
 184                 //final URL imageUrl = new URL(installUrl, PaintPlugin.getResourceString(id + ".image"));
 
 185 //              URL imageUrl = null;
 
 188 //                              new URL("C:\\sample.gif");
 
 189 //              } catch (MalformedURLException e) {
 
 190 //                      // TODO Auto-generated catch block
 
 191 //                      e.printStackTrace();
 
 193 //              action1.setImageDescriptor(ImageDescriptor.createFromURL(imageUrl));
 
 195                 action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
 
 196                         getImageDescriptor(ISharedImages.IMG_OBJS_TASK_TSK));
 
 197                 toolBarManager.add(action1);
 
 203          * @see org.eclipse.ui.IWorkbenchPart#setFocus()
 
 205         public void setFocus() {
 
 210          * mark the given test as passed in the GUI.
 
 214         private void markTestPassed(String testID) {
 
 216                 // testid, use it in hashmap to retrieve tree item of test and
 
 217                 // change icon color, increment pass counter, etc...
 
 219                 testPool.getTest(testID).setVerdict(TestCase.PASS);
 
 224         private void markTestStarted(String testID) {
 
 230         private void createNewTest(String testName, String testID) {
 
 232                 testPool.addTest(new TestCase(testName, testID));
 
 235         private void markTestFail(String testID) {
 
 237                 testPool.getTest(testID).setVerdict(TestCase.FAIL);
 
 241         private void markTestingStarted(int numTestsToBeRun) {
 
 244                 //reportArea.append("Tests started expecting: " + numTests + " \n");
 
 248         private void markTestingFinished() {
 
 251                 //reportArea.append("end all tests \n");
 
 255         // action to start tests:
 
 256         public void startTests() throws IOException {
 
 259 //              // take the full test suite (could containt other test suites).
 
 260 //              // create temp php file that starts that suite and uses socketTestReport 
 
 261 //              // as a test result reporter.
 
 262 //              // add listener: localhost , port 13579
 
 263 //              // start listening at port.
 
 265 //              testPool = new TestPool("RUN MONDAY 11:15 PM");
 
 266 //              listenForReports();
 
 269 //                      Runtime.getRuntime().exec("php.exe \"C:/Program Files/Apache Group/Apache2/htdocs/phpUnit/suite.php\"");
 
 270 //              } catch (Exception e) {
 
 272 //                      e.printStackTrace();
 
 275         startTests("C:/Program Files/Apache Group/Apache2/htdocs/phpUnit/suite.php");
 
 279         public void startTests(String testSuite) throws IOException {
 
 281                 //testSuite: the name of the file containing the suite we want to run.
 
 282                 // we will put that test suite inside a contained that uses our SocketResult.
 
 285                 //reset from previous run
 
 288                 //where the plugin's temp files should go
 
 289                 String tempFolder = "C:\\tmp"; 
 
 290                 String tempFileName = "temTest.php";
 
 293                 File testFile = new File(tempFolder + "/" + tempFileName);
 
 294                 BufferedWriter out = new BufferedWriter(new FileWriter(testFile));
 
 296                 out.write("<?php" + "\n");
 
 297                 out.write("$path = \"C:/Documents and Settings/Ali Echihabi/My Documents/workspace.eclipse2.1/PHPUnit/phpunit\";" + "\n");
 
 298                 out.write("include_once($path . \"/phpunit_test.php\");" + "\n");
 
 299                 out.write("include_once $path . \"/socketTestResult.php\";" + "\n");
 
 301                 //include the test suite that we want to run.
 
 302                 String testSuiteName = "ManyFailingTests2";
 
 303                 String testSuitePath = "C:/eclipse/eclipse/runtime-workspace/PHPProject/testSuite.php";
 
 305                 out.write("include_once(\"" + testSuitePath + "\");" + "\n");
 
 308                 out.write("" + "\n");
 
 309                 out.write("" + "\n");           
 
 311                 out.write("$suite = new TestSuite();" + "\n");
 
 312                 out.write("$suite->addTest(new TestSuite(\"" + testSuiteName + "\"));" + "\n");
 
 316                 //out.write("$suite->addTest(new TestSuite(\"MoreTesterTests\"));" + "\n");
 
 318                 //out.write("$suite->addTest(new TestSuite(\"ManyFailingTests\"));" + "\n");
 
 319                 //out.write("$suite->addTest(new TestSuite(\"AssertEqualsTests\"));" + "\n");           
 
 321                 out.write("$result = new SocketTestResult();" + "\n");
 
 322                 out.write("$suite->run($result);" + "\n");
 
 323                 out.write("$result->report();   " + "\n");
 
 325                 out.write("" + "\n");
 
 326                 out.write("" + "\n");
 
 328                 out.write("?>" + "\n");
 
 336                         Runtime.getRuntime().exec("php.exe " + tempFolder + "/" + tempFileName);
 
 337                 } catch (Exception e) {
 
 349         private void reset() {
 
 351                 handler = new XMLReportHandler();
 
 352                 testPool = new TestPool("Ali Baba");
 
 354                 progressInfoComposite.resetInfo();
 
 355                 resultsInfoComposite.resetInfo();
 
 362         private void listenForReports() {
 
 364                 ConnectionListener conListener = new ConnectionListener();
 
 365                 conListener.start(this);
 
 370          * handle this report: test passed, faile, end of all.
 
 373         public void handleReport(String report) {
 
 375                 //delegate to the XML report handler.           
 
 376                 handler.handle(report, this);
 
 386         public void handleCommand(
 
 392                 if (command.equals("startAll")) {
 
 394                          markTestingStarted(new Integer(testCount).intValue());
 
 397                 } else if (command.equals("testSuiteStarted")) {
 
 399                         createNewTestSuite("TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
 
 400                         markTestSuiteStarted(testID);
 
 402                 } else if (command.equals("testStarted")) {
 
 404                         createNewTest("TestName: " + testID, testID);
 
 405                         markTestStarted(testID);
 
 407                 } else if (command.equals("testFINISHED")) {
 
 411                 } else if (command.equals("endAll")) {
 
 413                         markTestingFinished();
 
 426         private void update() {
 
 428                 //progressInfoComposite.updateInfo(numTests, testPool.getNumTestsRun(), numFailures, numErrors);
 
 429                 progressInfoComposite.updateInfo(testPool);
 
 430                 resultsInfoComposite.updateInfo(testPool);
 
 437         private void markTestSuiteStarted(String testID) {
 
 438                 // TODO Auto-generated method stub
 
 447         private void createNewTestSuite(String name, String testID, int testCount) {
 
 449                 TestSuite suite = new TestSuite(name, testID, testCount);
 
 450                 testPool.addTestSuite(suite);
 
 457         private void markTestFinished() {
 
 465         private void updateResultsInfo() {
 
 466                 // TODO Auto-generated method stub
 
 471          * @param currentTestID
 
 474         public void setTestVerdict(String currentTestID, String verdict) {
 
 476                 if (verdict.equals("passed"))
 
 477                         markTestPassed(currentTestID);
 
 479                         markTestFail(currentTestID);
 
 484          * @param currentTestID
 
 487         public void addTestException(String currentTestID, String exception) {
 
 489                 //TODO: decide how to show exceptions. don't show them for now.
 
 490                 //reportArea.append("   test " + currentTestID + " exception: " + exception + "\n");