Permalink
Browse files

Stashing WIP for July 30, 2011

  • Loading branch information...
Pádraic Brady
Pádraic Brady committed Aug 3, 2011
1 parent 180371d commit e5d0072be7afbb49a4a738e510d4bcb780ea6ebd
View
@@ -144,7 +144,7 @@ PHPUnit test suites often use a TestHelper.php or Bootstrap.php file.
For example, imagine we usually employ the following to run some PHPUnit tests:
- phpunit AllTests.php --exclude-group=disabled
+ phpunit AllTests.php --exclude-group disabled
In addition, we use the file TestHelper.php to setup autloading for the tests
(this would normally be included from within AllTests.php manually but Mutagenesis
@@ -153,7 +153,7 @@ needs to load it as early as possible).
We can pass this to mutagenesis as:
mutagenesis --src="/path/project/library" --tests="/path/project/tests" \
- --options="--exclude-group=disabled" --constraint="AllTests.php" \
+ --options="--exclude-group disabled" --constraint="AllTests.php" \
--bootstrap="TestHelper.php"
Note: "\\" merely marks a line break for this README. The command should be on
@@ -32,24 +32,23 @@
* @var string
*/
protected $_output = '';
-
- /**
- * Execute the Adapter to run the test suite and parse the results
- *
- * @param array $options Options to be used when called the test suite runner
- * @return bool Boolean indicating whether test suite failed or passed
- */
- abstract public function execute(array $options);
/**
- * Parse the result output text to see if there were any failures.
- * In the context of mutation testing, a test failure is good (i.e. the
- * mutation was detected by the test suite).
+ * Runs the tests suite according to Runner set options and the execution
+ * order of test case (if any). It then returns an array of two elements.
+ * First element is a boolean result value indicating if tests passed or not.
+ * Second element is an array containing the key "stdout" which stores the
+ * output from the last test run.
*
- * @param string $output
- * @return bool
+ * @param \Mutagenesis\Runner\RunnerAbstract $baseRunner
+ * @param bool $useStdout
+ * @param bool $firstRun
+ * @param array $mutation
+ * @param array $testCases
+ * @return array
*/
- abstract public function processOutput($output);
+ abstract public function runTests(\Mutagenesis\Runner\Base $runner, $useStdout = false,
+ $firstRun = false, array $mutation = array(), array $testCases = array());
/**
* Set the test library output so it can be used later
@@ -21,34 +21,144 @@
namespace Mutagenesis\Adapter;
+require_once 'PHPUnit/TextUI/Command.php';
+
+if (class_exists('PHP_CodeCoverage_Filter', true)) {
+ \PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'PHPUNIT');
+}
+
class Phpunit extends AdapterAbstract
{
/**
- * Execute the Adapter to run the test suite and parse the results
+ * Runs the tests suite according to Runner set options and the execution
+ * order of test case (if any). It then returns an array of two elements.
+ * First element is a boolean result value indicating if tests passed or not.
+ * Second element is an array containing the key "stdout" which stores the
+ * output from the last test run.
*
- * @param array $options Options to be used when called the test suite runner
- * @return bool Boolean indicating whether test suite failed or passed
+ * @param \Mutagenesis\Runner\RunnerAbstract $baseRunner
+ * @param bool $useStdout
+ * @param bool $firstRun
+ * @param array $mutation
+ * @param array $testCases
+ * @return array
*/
- public function execute(array $options, $useStdout = false, $firstRun = false, array $testCases = array())
+ public function runTests(\Mutagenesis\Runner\Base $runner, $useStdout = false,
+ $firstRun = false, array $mutation = array(), array $testCases = array())
{
+ $options = $runner->getOptions();
+ $job = new \Mutagenesis\Utility\Job;
+ if(!$useStdout) {
+ array_unshift($options['clioptions'], '--stderr');
+ }
+ if (!in_array('--stop-on-failure', $options['clioptions'])) {
+ array_unshift($options['clioptions'], '--stop-on-failure');
+ }
+ array_unshift($options['clioptions'], 'phpunit');
if ($firstRun) {
$options['clioptions'] = array_merge(
$options['clioptions'],
array('--log-junit', $options['cache'] . '/mutagenesis.xml'),
explode(' ', $options['constraint'])
);
}
- if (count($testCases) > 0) {
+ if (count($testCases) > 0) { // tests cases always 0 on first run
foreach ($testCases as $case) {
$args = $options;
$args['clioptions'][] = $case['class'];
$args['clioptions'][] = $case['file'];
- Phpunit\Runner::main($args, $useStdout);
+ $output = self::execute(
+ $job->generate(
+ $mutation,
+ $args,
+ $runner->getBootstrap()
+ ),
+ $runner->getTimeout()
+ );
+ if (!$this->processOutput($output['stdout'])) {
+ return array(false, $output);
+ }
}
} else {
- Phpunit\Runner::main($options, $useStdout);
+ $output = self::execute(
+ $job->generate(
+ $mutation,
+ $options,
+ $runner->getBootstrap()
+ ),
+ $runner->getTimeout()
+ );
+ if (!$this->processOutput($output['stdout'])) {
+ return array(false, $output);
+ }
+ }
+ return array(true, $output);
+ }
+
+ /**
+ * Execute the generated job which is to call the static main method.
+ *
+ * @param string $jobScript
+ * @return string $output
+ */
+ public static function execute($jobScript, $timeout = 120)
+ {
+ $output = \Mutagenesis\Utility\Process::run($jobScript, $timeout);
+ return $output;
+ }
+
+ /**
+ * Uses an instance of PHPUnit_TextUI_Command to execute the PHPUnit
+ * tests and simulates any Mutagenesis supported command line options suitable
+ * for PHPUnit. At present, we merely dissect a generic 'options' string
+ * equivelant to anything typed into a console after a normal 'phpunit'
+ * command. The adapter captures the TextUI output for further processing.
+ *
+ * To prevent duplication of output from stdout, PHPUnit is hard
+ * configured to write to stderrm(stdin is used in proc_open call)
+ *
+ * @param array $arguments Mutagenesis arguments to pass to PHPUnit
+ * @return void
+ */
+ public static function main($arguments, $mutation = null, $bootstrap = null)
+ {
+ $arguments = unserialize($arguments);
+
+ /**
+ * Grab the Runkit extension utility and apply the mutation if needed
+ */
+ if (!is_null($mutation)) {
+ $mutation = unserialize($mutation);
+ if (!empty($mutation)) {
+ if (!is_null($bootstrap)) {
+ require_once $bootstrap;
+ }
+ if(!in_array('runkit', get_loaded_extensions())) {
+ throw new \Exception(
+ 'Runkit extension is not loaded. Unfortunately, runkit'
+ . ' is essential for Mutagenesis. Please see the manual or'
+ . ' README which explains how to install an updated runkit'
+ . ' extension suitable for Mutagenesis and PHP 5.3.'
+ );
+ }
+ $runkit = new \Mutagenesis\Utility\Runkit;
+ $runkit->applyMutation($mutation);
+ }
+ }
+
+ //var_dump($arguments); exit;
+
+ /**
+ * Switch working directory to tests and execute the test suite
+ */
+ $originalWorkingDirectory = getcwd();
+ if (isset($arguments['tests'])) {
+ chdir($arguments['tests']);
}
+ $command = new \PHPUnit_TextUI_Command;
+ $command->run($arguments['clioptions'], false);
+ chdir($originalWorkingDirectory);
}
/**
@@ -59,7 +169,7 @@ public function execute(array $options, $useStdout = false, $firstRun = false, a
* @param string $output
* @return bool
*/
- public function processOutput($output)
+ public static function processOutput($output)
{
if (substr($output, 0, 21) == 'Your tests timed out.') { //TODO: Multiple instances
return self::TIMED_OUT;
@@ -17,6 +17,7 @@
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mutateme/blob/rewrite/LICENSE New BSD License
+ * @deprecated
*/
namespace Mutagenesis\Adapter\Phpunit;
@@ -27,6 +28,9 @@
\PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'PHPUNIT');
}
+/**
+ * @deprecated
+ */
class Runner
{
@@ -48,7 +52,7 @@ public static function main(array $arguments, $useStdout = false)
if(!$useStdout) {
array_unshift($arguments['clioptions'], '--stderr');
}
- if (!in_array($arguments['clioptions'], '--stop-on-failure')) {
+ if (!in_array('--stop-on-failure', $arguments['clioptions'])) {
array_unshift($arguments['clioptions'], '--stop-on-failure');
}
array_unshift($arguments['clioptions'], 'phpunit');
@@ -33,7 +33,7 @@ public function execute()
{
$renderer = $this->getRenderer();
echo $renderer->renderOpening();
- $job = new \Mutagenesis\Utility\Job($this);
+
/**
* Run the test suite once to verify it is in a passing state before
@@ -45,14 +45,13 @@ public function execute()
* attempt to run the fastest test cases first (and slowest last)
* which in all probability should result in faster mutation test runs.
*/
- $output = \Mutagenesis\Utility\Process::run($job->generate(array(), true));
- $result = $this->getAdapter()->processOutput($output['stdout']);
- echo $renderer->renderPretest($result, $output['stdout']);
+ $result = $this->getAdapter()->runTests($this, false, true);
+ echo $renderer->renderPretest($result[0], $result[1]['stdout']);
/**
* If the underlying test suite is not passing, we can't continue.
*/
- if (!$result) {
+ if ($result[0] === 'timed out' || !$result[0]) {
return;
}
@@ -89,30 +88,41 @@ public function execute()
foreach ($mutables as $i=>$mutable) {
$mutations = $mutable->generate()->getMutations();
foreach ($mutations as $mutation) {
+
+ $result = $this->getAdapter()->runTests(
+ $this,
+ false,
+ false,
+ $mutation,
+ $orderedTestCases
+ );
+
$output = \Mutagenesis\Utility\Process::run(
$job->generate($mutation, false, $orderedTestCases), $this->getTimeout()
);
/* TODO: Store output for per-mutant results */
$result = $this->getAdapter()->processOutput($output['stdout']);
+
+
$countMutants++;
- if ($result === 'timed out' || !$result) {
+ if ($result[0] === 'timed out' || !$result[0]) {
$countMutantsKilled++;
if ($this->getDetailCaptures()) {
$mutation['mutation']->mutate(
$mutation['tokens'],
$mutation['index']
);
- $mutantsCaptured[] = array($mutation, $output['stdout']);
+ $mutantsCaptured[] = array($mutation, $result['stdout']);
}
- } elseif ($result) {
+ } else {
$countMutantsEscaped++;
$mutation['mutation']->mutate(
$mutation['tokens'],
$mutation['index']
);
$mutantsEscaped[] = $mutation;
}
- echo $renderer->renderProgressMark($result);
+ echo $renderer->renderProgressMark($result[0]);
}
$mutable->cleanup();
unset($this->_mutables[$i]);
@@ -17,10 +17,13 @@
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mutateme/blob/rewrite/LICENSE New BSD License
+ * @deprecated
*/
namespace Mutagenesis\Runner;
-
+/**
+ * @deprecated
+ */
class Mutation extends RunnerAbstract
{
@@ -56,7 +59,8 @@ public function execute($firstRun = false)
}
$this->getRunkit()->applyMutation($mutation);
}
- $this->getAdapter()->execute($this->getOptions(), false, $firstRun, $this->_testCasesInExecutionOrder);
+ // TODO
+
}
/**
@@ -85,6 +89,11 @@ public function setTestCasesInExecutionOrder($testCases)
$this->_testCasesInExecutionOrder = unserlialize($testCases);
}
+ /**
+ * Set array of adapter options parsed from string. These are
+ * passed to the testing framework's cli command
+ * @param string $options Serialized array of options to pass
+ */
public function setAdapterOptions($options)
{
parent::setAdapterOptions(unserialize($options));
Oops, something went wrong.

0 comments on commit e5d0072

Please sign in to comment.