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

Refactoring of TestRunner, TestSuite #781

Merged
merged 2 commits into from
Jan 26, 2013
Merged
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
5 changes: 5 additions & 0 deletions PHPUnit/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ function ($class)
'phpunit_util_fileloader' => '/Util/Fileloader.php',
'phpunit_util_filesystem' => '/Util/Filesystem.php',
'phpunit_util_filter' => '/Util/Filter.php',
'phpunit_util_filters_excludegroupfilteriterator' => '/Util/Filters/ExcludeGroupFilterIterator.php',
'phpunit_util_filters_filteriteratorfactory' => '/Util/Filters/FilterIteratorFactory.php',
'phpunit_util_filters_groupfilteriterator' => '/Util/Filters/GroupFilterIterator.php',
'phpunit_util_filters_includegroupfilteriterator' => '/Util/Filters/IncludeGroupFilterIterator.php',
'phpunit_util_filters_testfilteriterator' => '/Util/Filters/TestFilterIterator.php',
'phpunit_util_getopt' => '/Util/Getopt.php',
'phpunit_util_globalstate' => '/Util/GlobalState.php',
'phpunit_util_invalidargumenthelper' => '/Util/InvalidArgumentHelper.php',
Expand Down
16 changes: 3 additions & 13 deletions PHPUnit/Extensions/RepeatedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class PHPUnit_Extensions_RepeatedTest extends PHPUnit_Extensions_TestDecorator
* @param boolean $processIsolation
* @throws PHPUnit_Framework_Exception
*/
public function __construct(PHPUnit_Framework_Test $test, $timesRepeat = 1, $filter = FALSE, array $groups = array(), array $excludeGroups = array(), $processIsolation = FALSE)
public function __construct(PHPUnit_Framework_Test $test, $timesRepeat = 1, $processIsolation = FALSE)
{
parent::__construct($test);

Expand All @@ -103,9 +103,6 @@ public function __construct(PHPUnit_Framework_Test $test, $timesRepeat = 1, $fil
);
}

$this->filter = $filter;
$this->groups = $groups;
$this->excludeGroups = $excludeGroups;
$this->processIsolation = $processIsolation;
}

Expand Down Expand Up @@ -138,16 +135,9 @@ public function run(PHPUnit_Framework_TestResult $result = NULL)
for ($i = 0; $i < $this->timesRepeat && !$result->shouldStop(); $i++) {
//@codingStandardsIgnoreEnd
if ($this->test instanceof PHPUnit_Framework_TestSuite) {
$this->test->run(
$result,
$this->filter,
$this->groups,
$this->excludeGroups,
$this->processIsolation
);
} else {
$this->test->run($result);
$this->test->setRunTestInSeparateProcess($this->processIsolation);
}
$this->test->run($result);
}

return $result;
Expand Down
198 changes: 84 additions & 114 deletions PHPUnit/Framework/TestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Fra
*/
protected $backupStaticAttributes = NULL;

protected $runTestInSeparateProcess = FALSE;

/**
* The name of the test suite.
*
Expand Down Expand Up @@ -128,6 +130,11 @@ class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Fra
*/
protected $testCase = FALSE;

/**
* @var PHPUnit_Util_Filters_FilterIteratorFactory
*/
private $iteratorFilter = NULL;

/**
* Constructs a new TestSuite:
*
Expand Down Expand Up @@ -424,17 +431,13 @@ public function addTestFiles($filenames)
*/
public function count()
{
if ($this->numTests > -1) {
return $this->numTests;
}
$numTests = 0;

$this->numTests = 0;

foreach ($this->tests as $test) {
$this->numTests += count($test);
foreach ($this as $test) {
$numTests += count($test);
}

return $this->numTests;
return $numTests;
}

/**
Expand Down Expand Up @@ -613,6 +616,10 @@ public function getGroups()
return array_keys($this->groups);
}

public function getGroupDetails() {
return $this->groups;
}

/**
* Runs the tests and collects their result in a TestResult.
*
Expand All @@ -624,149 +631,99 @@ public function getGroups()
* @return PHPUnit_Framework_TestResult
* @throws PHPUnit_Framework_Exception
*/
public function run(PHPUnit_Framework_TestResult $result = NULL, $filter = FALSE, array $groups = array(), array $excludeGroups = array(), $processIsolation = FALSE)
public function run(PHPUnit_Framework_TestResult $result = NULL)
{
if ($result === NULL) {
$result = $this->createResult();
}

if (count($this) == 0) {
return $result;
}

$result->startTestSuite($this);

$doSetup = TRUE;
try {
$this->setUp();

if (!empty($excludeGroups)) {
foreach ($this->groups as $_group => $_tests) {
if (in_array($_group, $excludeGroups) &&
count($_tests) == count($this->tests)) {
$doSetup = FALSE;
}
if ($this->testCase &&
// Some extensions use test names that are not classes;
// The method_exists() triggers an autoload call that causes issues with die()ing autoloaders.
class_exists($this->name, false) &&
method_exists($this->name, 'setUpBeforeClass')) {
call_user_func(array($this->name, 'setUpBeforeClass'));
}
}

if ($doSetup) {
try {
$this->setUp();
catch (PHPUnit_Framework_SkippedTestSuiteError $e) {
$numTests = count($this);

if ($this->testCase &&
// Some extensions use test names that are not classes;
// The method_exists() triggers an autoload call that causes issues with die()ing autoloaders.
class_exists($this->name, false) &&
method_exists($this->name, 'setUpBeforeClass')) {
call_user_func(array($this->name, 'setUpBeforeClass'));
}
for ($i = 0; $i < $numTests; $i++) {
$result->addFailure($this, $e, 0);
}

catch (PHPUnit_Framework_SkippedTestSuiteError $e) {
$numTests = count($this);

for ($i = 0; $i < $numTests; $i++) {
$result->addFailure($this, $e, 0);
}

return $result;
}

catch (Exception $e) {
$numTests = count($this);

for ($i = 0; $i < $numTests; $i++) {
$result->addError($this, $e, 0);
}

return $result;
}
return $result;
}

if (empty($groups)) {
$tests = $this->tests;
} else {
$tests = new SplObjectStorage;
catch (Exception $e) {
$numTests = count($this);

foreach ($groups as $group) {
if (isset($this->groups[$group])) {
foreach ($this->groups[$group] as $test) {
$tests->attach($test);
}
}
for ($i = 0; $i < $numTests; $i++) {
$result->addError($this, $e, 0);
}

return $result;
}

foreach ($tests as $test) {
foreach ($this as $test) {
if ($result->shouldStop()) {
break;
}

if ($test instanceof PHPUnit_Framework_TestSuite) {
if ($test instanceof PHPUnit_Framework_TestCase ||
$test instanceof PHPUnit_Framework_TestSuite) {
$test->setBackupGlobals($this->backupGlobals);
$test->setBackupStaticAttributes($this->backupStaticAttributes);
$test->setRunTestInSeparateProcess($this->runTestInSeparateProcess);
}

$test->run(
$result, $filter, $groups, $excludeGroups, $processIsolation
);
} else {
$runTest = TRUE;

if ($filter !== FALSE ) {
$tmp = PHPUnit_Util_Test::describe($test, FALSE);

if ($tmp[0] != '') {
$name = join('::', $tmp);
} else {
$name = $tmp[1];
}

if (preg_match($filter, $name) == 0) {
$runTest = FALSE;
}
}

if ($runTest && !empty($excludeGroups)) {
foreach ($this->groups as $_group => $_tests) {
if (in_array($_group, $excludeGroups)) {
foreach ($_tests as $_test) {
if ($test === $_test) {
$runTest = FALSE;
break 2;
}
}
}
}
}

if ($runTest) {
if ($test instanceof PHPUnit_Framework_TestCase) {
$test->setBackupGlobals($this->backupGlobals);
$test->setBackupStaticAttributes(
$this->backupStaticAttributes
);
$test->setRunTestInSeparateProcess($processIsolation);
}

$this->runTest($test, $result);
}
}
$test->run($result);
}

if ($doSetup) {
if ($this->testCase &&
// Some extensions use test names that are not classes;
// The method_exists() triggers an autoload call that causes issues with die()ing autoloaders.
class_exists($this->name, false) &&
method_exists($this->name, 'tearDownAfterClass')) {
call_user_func(array($this->name, 'tearDownAfterClass'));
}

$this->tearDown();
if ($this->testCase &&
// Some extensions use test names that are not classes;
// The method_exists() triggers an autoload call that causes issues with die()ing autoloaders.
class_exists($this->name, false) &&
method_exists($this->name, 'tearDownAfterClass')) {
call_user_func(array($this->name, 'tearDownAfterClass'));
}

$this->tearDown();

$result->endTestSuite($this);

return $result;
}

/**
* @param boolean $runTestInSeparateProcess
* @throws InvalidArgumentException
* @since Method available since Release 3.7.0
*/
public function setRunTestInSeparateProcess($runTestInSeparateProcess)
{
if (is_bool($runTestInSeparateProcess)) {
$this->runTestInSeparateProcess = $runTestInSeparateProcess;
} else {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
}
}

/**
* Runs a test.
*
* @deprecated
* @param PHPUnit_Framework_Test $test
* @param PHPUnit_Framework_TestResult $result
*/
Expand Down Expand Up @@ -923,9 +880,22 @@ public function setBackupStaticAttributes($backupStaticAttributes)
*/
public function getIterator()
{
return new RecursiveIteratorIterator(
new PHPUnit_Util_TestSuiteIterator($this)
);
$iterator = new PHPUnit_Util_TestSuiteIterator($this);

if ($this->iteratorFilter !== NULL) {
$iterator = $this->iteratorFilter->factory($iterator, $this);
}

return $iterator;
}

public function injectFilter(PHPUnit_Util_Filters_FilterIteratorFactory $filter) {
$this->iteratorFilter = $filter;
foreach ($this as $test) {
if ($test instanceof PHPUnit_Framework_TestSuite) {
$test->injectFilter($filter);
}
}
}

/**
Expand Down
Loading