Created
March 21, 2012 22:23
-
-
Save elblinkin/2153769 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PHPUnit_Extensions_PHPUI_Command { | |
private $arguments; | |
private $loader; | |
private $coverage_filter; | |
private $test_suite; | |
public function __construct( | |
PHPUnit_Runner_TestSuiteLoader $loader = null, | |
PHP_CodeCoverage_Filter $coverage_filter = null, | |
PHPUnit_Framework_TestSuite $test_suite = null, | |
array $arguments = array() | |
) { | |
$this->loader = $loader; | |
$this->coverage_filter = $coverage_filter; | |
$this->test_suite = $test_suite; | |
$this->arguments = $arguments; | |
} | |
public function log($format, $file_name) { | |
switch (strtolower($format)) { | |
case 'junit': | |
$this->arguments['junitLogfile'] = $file_name; | |
break; | |
case 'tap': | |
$this->arguments['tapLogfile'] = $file_name; | |
break; | |
case 'json': | |
$this->arguments['jsonLogfile'] = $file_name; | |
break; | |
case 'plain': | |
$this->addTestListener( | |
new PHPUnit_TextUI_ResultPrinter( | |
$file_name, | |
true | |
) | |
); | |
break; | |
default: | |
throw new InvalidArgumentException( | |
sprintf( | |
'Invalid format: %s. Must be "junit", "tap", "json", or "plain".', | |
$format | |
) | |
); | |
} | |
return $this; | |
} | |
public function coverage($format, $file_or_directory_name) { | |
switch (strtolower($format)) { | |
case 'clover': | |
$this->arguments['coverageClover'] = $file_or_directory_name; | |
break; | |
case 'html': | |
$this->arguments['reportDirectory'] = $file_or_directory_name; | |
break; | |
case 'php': | |
$this->arguments['coveragePHP'] = $file_or_directory_name; | |
break; | |
case 'text': | |
$this->arguments['coverageText'] = $file_or_directory_name; | |
break; | |
default: | |
throw new InvalidArgumentException( | |
sprintf( | |
'Invalid format: %s. Must be "clover", "html", "php", or "text".', | |
$format | |
) | |
); | |
} | |
return $this; | |
} | |
public function testdox($format, $file_name) { | |
switch (strtolower($format)) { | |
case 'html': | |
$this->arguments['testdoxHTMLFile'] = $file_name; | |
break; | |
case 'text': | |
$this->arguments['testdoxTextFile'] = $file_name; | |
break; | |
default: | |
throw new InvalidArgumentException( | |
sprintf( | |
'Invalid format: %s. Must be "html" or "text".', | |
$format | |
) | |
); | |
} | |
return $this; | |
} | |
public function filter($pattern) { | |
$this->arguments['filter'] = $pattern; | |
return $this; | |
} | |
public function includeGroup($group) { | |
if (!isset($this->arguments['groups'])) { | |
$this->arguments['groups'] = array(); | |
} | |
$this->arguments['groups'][] = $group; | |
return $this; | |
} | |
public function excludeGroup($group) { | |
if (!isset($this->arguments['excludeGroups'])) { | |
$this->arguments['excludeGroups'] = array(); | |
} | |
$this->arguments['excludeGroups'][] = $group; | |
return $this; | |
} | |
public function repeat($times) { | |
$this->arguments['repeat'] = $times; | |
return $this; | |
} | |
public function colors($enable = true) { | |
$this->arguments['colors'] = $enable; | |
return $this; | |
} | |
public function convertToException($error_level) { | |
switch (strtolower($error_level)) { | |
case 'notices': | |
$this->arguments['convertNoticesToExceptions'] = true; | |
break; | |
case 'warnings': | |
$this->arguments['convertWarningsToExceptions'] = true; | |
break; | |
case 'errors': | |
$this->arguments['convertErrorsToExceptions'] = true; | |
break; | |
default: | |
throw new InvalidArgumentException( | |
sprintf( | |
'Invalid error level: %s. Must be "notices", "warnings", or "errors".', | |
$error_level | |
) | |
); | |
} | |
return $this; | |
} | |
public function stopOn($test_status) { | |
switch (strtolower($test_status)) { | |
case 'failure': | |
$this->arguments['stopOnFailure'] = true; | |
break; | |
case 'error': | |
$this->arguments['stopOnError'] = true; | |
break; | |
case 'incomplete': | |
$this->arguments['stopOnIncomplete'] = true; | |
break; | |
case 'skipped': | |
$this->arguments['stopOnSkipped'] = true; | |
break; | |
default: | |
throw new InvalidArgumentException( | |
sprintf( | |
'Invalid test status: %s. Must be "failure", "error", "incomplete", or "skipped".', | |
$test_status | |
) | |
); | |
} | |
return $this; | |
} | |
public function timeoutForSize($test_size, $timeout) { | |
switch (strtolower($test_size)) { | |
case 'small': | |
$this->arguments['timeoutForSmallTests'] = $timeout; | |
break; | |
case 'medium': | |
$this->arguments['timeoutForMediumTests'] = $timeout; | |
break; | |
case 'large': | |
$this->arguments['timeoutForLargeTests'] = $timeout; | |
break; | |
default: | |
throw new InvalidArgumentException( | |
sprintf( | |
'Invalid test size: %s. Must be "small", "medium", or "large".', | |
$test_size | |
) | |
); | |
} | |
return $this; | |
} | |
public function strict($enable = true) { | |
$this->arguments['strict'] = $enable; | |
return $this; | |
} | |
public function verbose($enable = true) { | |
$this->arguments['verbose'] = $enable; | |
return $this; | |
} | |
public function debug($enable = true) { | |
$this->arguments['debug'] = $enable; | |
return $this; | |
} | |
public function processIsolation($enable = true) { | |
$this->arguments['processIsolation'] = $enable; | |
return $this; | |
} | |
public function backupGlobals($enable = true) { | |
$this->arguments['backupGlobals'] = $enable; | |
return $this; | |
} | |
public function backupStaticAttributes($enable = true) { | |
$this->arguments['backupStaticAttributes'] = $enable; | |
return $this; | |
} | |
public function cacheTokens($enable = true) { | |
$this->arguments['cacheTokens'] = $enable; | |
return $this; | |
} | |
public function forceCoversAnnotation($enable = true) { | |
$this->arguments['forceCoversAnnotation'] = $enable; | |
return $this; | |
} | |
public function mapTestClassNameToCoveredClassName($enable = true) { | |
$this->arguments['mapTestClassNameToCoveredClassName'] = $enable; | |
return $this; | |
} | |
public function logIncompleteAndSkippedTests($enable = true) { | |
$this->arguments['logIncompleteSkipped'] = $enable; | |
return $this; | |
} | |
public function addTestSuite(PHPUnit_Framework_TestSuite $test_suite) { | |
if ($this->test_suite === null) { | |
$this->test_suite = $test_suite; | |
} else { | |
$this->test_suite->addTestSuite($test_suite); | |
} | |
return $this; | |
} | |
public function setCoverageFilter(PHP_CodeCoverage_Filter $coverage_filter) { | |
$this->coverage_filter = $coverage_filter; | |
return $this; | |
} | |
public function addTestListener(PHPUnit_Framework_TestListener $listener) { | |
if (!isset($this->arguments['listeners'])) { | |
$this->arguments['listeners'] = array(); | |
} | |
$this->arguments['listeners'][] = $listener; | |
return $this; | |
} | |
public function setTestSuiteLoader($loader) { | |
$this->loader = $loader; | |
return $this; | |
} | |
public function setPrinter($printer) { | |
$this->arguments['printer'] = $printer; | |
return $this; | |
} | |
public function run($exit = true) { | |
if (!isset($this->loader)) { | |
$this->loader = new PHPUnit_Runner_StandardTestSuiteLoader(); | |
} | |
if (!isset($this->coverage_filter)) { | |
$this->coverage_filter = new PHP_CodeCoverage_Filter(); | |
} | |
if (!isset($this->test_suite)) { | |
$this->test_suite = new PHPUnit_Framework_TestSuite(); | |
} | |
$runner = new PHPUnit_TextUI_TestRunner($this->loader, $this->coverage_filter); | |
try { | |
$result = $runner->doRun($this->test_suite, $this->arguments); | |
} catch (PHPUnit_Framework_Exception $e) { | |
print $e->getMessage() . "\n"; | |
} | |
$ret = PHPUnit_TextUI_TestRunner::FAILURE_EXIT; | |
if (isset($result) && $result->wasSuccessful()) { | |
$ret = PHPUnit_TextUI_TestRunner::SUCCESS_EXIT; | |
} else if (!isset($result) || $result->errorCount() > 0) { | |
$ret = PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT; | |
} | |
if ($exit) { | |
exit($ret); | |
} else { | |
return $ret; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment