Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to use a phpunit.phar archive to launch phpunit tests #90

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public String getTestFilePattern() {
public String getPHPUnitConfigFile() {
return ""; //$NON-NLS-1$
}

public String getPHPUnitPharFile() {
return ""; //$NON-NLS-1"
}

/**
* @since 2.5.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class MakeGoodProperties {
private static String TESTING_FRAMEWORK_KEY = "testing_framework"; //$NON-NLS-1$
private static String TEST_FOLDERS = "test_folders"; //$NON-NLS-1$
private static String PHPUNIT_CONFIG_FILE = "phpunit_config_file"; //$NON-NLS-1$
private static String PHPUNIT_PHAR_FILE = "phpunit_phar_file"; //$NON-NLS-1$

/**
* @since 2.0.0
Expand Down Expand Up @@ -130,6 +131,14 @@ public String getPHPUnitConfigFile() {
public void setPHPUnitConfigFile(String phpunitConfigFile) {
preferences.put(PHPUNIT_CONFIG_FILE, phpunitConfigFile);
}

public String getPHPUnitPharFile() {
return preferences.get(PHPUNIT_PHAR_FILE, defaultConfiguration.getPHPUnitConfigFile());
}

public void setPHPUnitPharFile(String phpunitPharFile) {
preferences.put(PHPUNIT_PHAR_FILE, phpunitPharFile);
}

/**
* @since 2.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,51 @@ public class CommandLineBuilder {
public static boolean stopOnFailure = false;

private String junitXMLFile;
// Flag indicating whether these command arguments should be compatible with a phpunit phar launch
private boolean isPharLaunch;

public CommandLineBuilder(String junitXMLFile) {
public CommandLineBuilder(String junitXMLFile, boolean isPharLaunch) {
this.junitXMLFile = junitXMLFile;
this.isPharLaunch = isPharLaunch;
}

public String build() throws CoreException, MethodNotFoundException, ResourceNotFoundException {
MakeGoodProperties property = new MakeGoodProperties(TestLifecycle.getInstance().getTestTargets().getFirstResource());
StringBuilder buffer = new StringBuilder();

buffer.append(" --no-ansi"); //$NON-NLS-1$
buffer.append(" " + property.getTestingFramework().name().toLowerCase()); //$NON-NLS-1$

String preloadScript = property.getPreloadScript();
if (!preloadScript.equals("")) { //$NON-NLS-1$
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource preloadResource = root.findMember(preloadScript);
if (preloadResource == null) {
throw new ResourceNotFoundException("The resource [ " + preloadScript + " ] is not found."); //$NON-NLS-1$ //$NON-NLS-2$
}

buffer.append(" -p \"" + preloadResource.getLocation().toString() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}

buffer.append(" --log-junit=\"" + junitXMLFile + "\""); //$NON-NLS-1$ //$NON-NLS-2$
buffer.append(" --log-junit-realtime"); //$NON-NLS-1$

if (stopOnFailure) {
buffer.append(" -s"); //$NON-NLS-1$
if(!isPharLaunch) {
buffer.append(" --no-ansi"); //$NON-NLS-1$
buffer.append(" " + property.getTestingFramework().name().toLowerCase()); //$NON-NLS-1$


String preloadScript = property.getPreloadScript();
if (!preloadScript.equals("")) { //$NON-NLS-1$
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource preloadResource = root.findMember(preloadScript);
if (preloadResource == null) {
throw new ResourceNotFoundException("The resource [ " + preloadScript + " ] is not found."); //$NON-NLS-1$ //$NON-NLS-2$
}
if(isPharLaunch) {
buffer.append(" --bootstrap \"" + preloadResource.getLocation().toString() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
} else {
buffer.append(" -p \"" + preloadResource.getLocation().toString() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
}
}

buffer.append(" --log-junit=\"" + junitXMLFile + "\""); //$NON-NLS-1$ //$NON-NLS-2$
if(isPharLaunch) {
if (stopOnFailure) {
buffer.append(" --stop-on-failure"); //$NON-NLS-1$
}
} else {
buffer.append(" --log-junit-realtime"); //$NON-NLS-1$

if (stopOnFailure) {
buffer.append(" -s"); //$NON-NLS-1$
}
}

if (property.getTestingFramework() == TestingFramework.PHPUnit) {
String phpunitConfigFile = property.getPHPUnitConfigFile();
Expand All @@ -76,7 +92,11 @@ public String build() throws CoreException, MethodNotFoundException, ResourceNot
throw new ResourceNotFoundException("The resource [ " + phpunitConfigFile + " ] is not found."); //$NON-NLS-1$ //$NON-NLS-2$
}

buffer.append(" --phpunit-config=\"" + resource.getLocation().toString() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
if(isPharLaunch) {
buffer.append(" -c \""+ resource.getLocation().toString() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
} else {
buffer.append(" --phpunit-config=\"" + resource.getLocation().toString() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
}
}

Expand Down Expand Up @@ -116,26 +136,28 @@ public String build() throws CoreException, MethodNotFoundException, ResourceNot
}
}

if (testClasses.size() > 0) {
for (String testClass: testClasses) {
buffer.append(" --test-class=\"" + testClass.toString() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
}

if (testMethods.size() > 0) {
for (String testMethod: testMethods) {
buffer.append(" --test-method=\"" + testMethod.toString() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
}

buffer.append(" -R"); //$NON-NLS-1$
buffer.append(
" --test-file-pattern=\"" + //$NON-NLS-1$
(property.getTestFilePattern().equals("") ? property.getTestingFramework().getTestFilePattern() : property.getTestFilePattern()) + //$NON-NLS-1$
"\"" //$NON-NLS-1$
);
for (String testFile: testFiles) {
buffer.append(" \"" + testFile + "\""); //$NON-NLS-1$ //$NON-NLS-2$
if(!isPharLaunch) {
if (testClasses.size() > 0) {
for (String testClass: testClasses) {
buffer.append(" --test-class=\"" + testClass.toString() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
}

if (testMethods.size() > 0) {
for (String testMethod: testMethods) {
buffer.append(" --test-method=\"" + testMethod.toString() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
}

buffer.append(" -R"); //$NON-NLS-1$
buffer.append(
" --test-file-pattern=\"" + //$NON-NLS-1$
(property.getTestFilePattern().equals("") ? property.getTestingFramework().getTestFilePattern() : property.getTestFilePattern()) + //$NON-NLS-1$
"\"" //$NON-NLS-1$
);
for (String testFile: testFiles) {
buffer.append(" \"" + testFile + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
}

return buffer.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.HashSet;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
Expand All @@ -41,6 +43,7 @@
import org.eclipse.php.internal.debug.core.preferences.PHPProjectPreferences;
import org.eclipse.php.internal.debug.core.preferences.PHPexeItem;

import com.piece_framework.makegood.core.preference.MakeGoodProperties;
import com.piece_framework.makegood.stagehandtestrunner.StagehandTestRunner;

@SuppressWarnings("restriction")
Expand Down Expand Up @@ -259,7 +262,7 @@ IPHPDebugConstants.ATTR_FILE_FULL_PATH, getCommandPath()
workingCopy.setAttribute(MAKEGOOD_JUNIT_XML_FILE, junitXMLFile);
workingCopy.setAttribute(
IDebugParametersKeys.EXE_CONFIG_PROGRAM_ARGUMENTS,
new CommandLineBuilder(junitXMLFile).build()
getCommandArguments(junitXMLFile)
);

IProject project = TestLifecycle.getInstance().getTestTargets().getProject();
Expand All @@ -270,9 +273,32 @@ IPHPDebugConstants.ATTR_FILE_FULL_PATH, getCommandPath()

return workingCopy;
}

public static String getCommandArguments(String junitXMLFile) throws CoreException, MethodNotFoundException, ResourceNotFoundException {
MakeGoodProperties property = new MakeGoodProperties(TestLifecycle.getInstance().getTestTargets().getFirstResource());
String pharFile = property.getPHPUnitPharFile().trim();
if("".equals(pharFile)) {
String args = new CommandLineBuilder(junitXMLFile, false).build();
return args;
} else {
String args = new CommandLineBuilder(junitXMLFile, true).build();
return args;
}
}

public static String getCommandPath() throws CoreException, ResourceNotFoundException {
MakeGoodProperties property = new MakeGoodProperties(TestLifecycle.getInstance().getTestTargets().getFirstResource());
String pharFile = property.getPHPUnitPharFile().trim();
if("".equals(pharFile))
return StagehandTestRunner.getCommandPath();
else {
IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(pharFile);
if (resource == null) {
throw new ResourceNotFoundException("The resource [ " + pharFile + " ] is not found."); //$NON-NLS-1$ //$NON-NLS-2$
}

public static String getCommandPath() throws CoreException {
return StagehandTestRunner.getCommandPath();
return resource.getLocation().toString(); //$NON-NLS-1$ //$NON-NLS-2$
}
}

public static String getJUnitXMLFile(ILaunch launch) throws CoreException {
Expand Down
4 changes: 4 additions & 0 deletions com.piece_framework.makegood.ui/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ MakeGoodPropertyPage_preloadScriptLabel=Preload Script:
MakeGoodPropertyPage_preloadScriptBrowseLabel=&Browse...
MakeGoodPropertyPage_phpunitConfigFileDialogTitle=PHPUnit XML Configuration File
MakeGoodPropertyPage_phpunitConfigFileDialogMessage=Select a PHPUnit XML configuration file:
MakeGoodPropertyPage_phpunitPharFileDialogTitle=PHPUnit Phar File
MakeGoodPropertyPage_phpunitPharFileDialogMessage=Select a PHPUnit Phar file:
MakeGoodPropertyPage_phpunitConfigFileLabel=XML Configuration File:
MakeGoodPropertyPage_phpunitConfigFileBrowseLabel=Browse...
MakeGoodPropertyPage_phpunitPharFileLabel=PHPUnit Phar File:
MakeGoodPropertyPage_phpunitPharFileBrowseLabel=Browse...
MakeGoodPropertyPage_testFolderAddLabel=&Add...
MakeGoodPropertyPage_testFolderRemoveLabel=&Remove
MakeGoodPropertyPage_testFolderDialogMessage=Select the test folder:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ public class Messages extends NLS {
public static String MakeGoodPropertyPage_preloadScriptLabel;
public static String MakeGoodPropertyPage_phpunitConfigFileDialogTitle;
public static String MakeGoodPropertyPage_phpunitConfigFileDialogMessage;
public static String MakeGoodPropertyPage_phpunitPharFileDialogTitle;
public static String MakeGoodPropertyPage_phpunitPharFileDialogMessage;
public static String MakeGoodPropertyPage_phpunitConfigFileLabel;
public static String MakeGoodPropertyPage_phpunitConfigFileBrowseLabel;
public static String MakeGoodPropertyPage_phpunitPharFileLabel;
public static String MakeGoodPropertyPage_phpunitPharFileBrowseLabel;
public static String MakeGoodPropertyPage_testFolderRemoveLabel;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class MakeGoodPropertyPage extends PropertyPage {
private static final int SELECTION_ALLOW_FOLDER = 2;
private Text preloadScriptText;
private Text phpunitConfigFileText;
private Text phpunitPharFileText;
private Button phpunitButton;

private TreeViewer testFolderTreeViewer;
Expand Down Expand Up @@ -185,6 +186,23 @@ public void selectionChanged(SelectionChangedEvent event) {
new FileViewerFilter()
)
);

Label phpunitPharFileLabel = new Label(phpunitConfigFile, SWT.NONE);
phpunitPharFileLabel.setText(Messages.MakeGoodPropertyPage_phpunitPharFileLabel);
phpunitPharFileText = new Text(phpunitConfigFile, SWT.SINGLE | SWT.BORDER);
phpunitPharFileText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Button phpunitPharFileBrowseButton = new Button(phpunitConfigFile, SWT.NONE);
phpunitPharFileBrowseButton.setText(Messages.MakeGoodPropertyPage_phpunitPharFileBrowseLabel);
phpunitPharFileBrowseButton.addSelectionListener(
new FileSelectionListener(
phpunitPharFileText,
Messages.MakeGoodPropertyPage_phpunitPharFileDialogTitle,
Messages.MakeGoodPropertyPage_phpunitPharFileDialogMessage,
SELECTION_ALLOW_FILE,
new FileViewerFilter()
)
);

frameworkTabItems.add(phpunitTabItem);

loadProperties(createMakeGoodProperty());
Expand All @@ -211,6 +229,7 @@ public boolean performOk() {
property.setPreloadScript(preloadScriptText.getText());
property.setTestFilePattern(testFilePatternText.getText());
property.setPHPUnitConfigFile(phpunitConfigFileText.getText());
property.setPHPUnitPharFile(phpunitPharFileText.getText());
property.flush();

return true;
Expand Down Expand Up @@ -274,6 +293,7 @@ private void loadProperties(MakeGoodProperties property) {
preloadScriptText.setText(property.getPreloadScript());
testFilePatternText.setText(property.getTestFilePattern());
phpunitConfigFileText.setText(property.getPHPUnitConfigFile());
phpunitPharFileText.setText(property.getPHPUnitPharFile());
}

/**
Expand All @@ -285,6 +305,7 @@ private void loadProperties(DefaultConfiguration defaultConfiguration) {
preloadScriptText.setText(defaultConfiguration.getPreloadScript());
testFilePatternText.setText(defaultConfiguration.getTestFilePattern());
phpunitConfigFileText.setText(defaultConfiguration.getPHPUnitConfigFile());
phpunitPharFileText.setText(defaultConfiguration.getPHPUnitPharFile());
}

/**
Expand Down