5
* Copyright (c) 2002-2010, Sebastian Bergmann <sebastian@phpunit.de>.
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* * Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in
17
* the documentation and/or other materials provided with the
20
* * Neither the name of Sebastian Bergmann nor the names of his
21
* contributors may be used to endorse or promote products derived
22
* from this software without specific prior written permission.
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
* POSSIBILITY OF SUCH DAMAGE.
38
* @subpackage Framework
39
* @author Sebastian Bergmann <sebastian@phpunit.de>
40
* @copyright 2002-2010 Sebastian Bergmann <sebastian@phpunit.de>
41
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
42
* @link http://www.phpunit.de/
43
* @since File available since Release 2.0.0
46
require_once 'PHP/CodeCoverage.php';
49
* A TestSuite is a composite of Tests. It runs a collection of test cases.
51
* Here is an example using the dynamic test definition.
55
* $suite = new PHPUnit_Framework_TestSuite;
56
* $suite->addTest(new MathTest('testPass'));
60
* Alternatively, a TestSuite can extract the tests to be run automatically.
61
* To do so you pass a ReflectionClass instance for your
62
* PHPUnit_Framework_TestCase class to the PHPUnit_Framework_TestSuite
67
* $suite = new PHPUnit_Framework_TestSuite(
68
* new ReflectionClass('MathTest')
73
* This constructor creates a suite with all the methods starting with
74
* "test" that take no arguments.
77
* @subpackage Framework
78
* @author Sebastian Bergmann <sebastian@phpunit.de>
79
* @copyright 2002-2010 Sebastian Bergmann <sebastian@phpunit.de>
80
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
81
* @version Release: 3.5.5
82
* @link http://www.phpunit.de/
83
* @since Class available since Release 2.0.0
85
class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing, IteratorAggregate
88
* Enable or disable the backup and restoration of the $GLOBALS array.
92
protected $backupGlobals = NULL;
95
* Enable or disable the backup and restoration of static attributes.
99
protected $backupStaticAttributes = NULL;
102
* The name of the test suite.
106
protected $name = '';
109
* The test groups of the test suite.
113
protected $groups = array();
116
* The tests in the test suite.
120
protected $tests = array();
123
* The number of tests in the test suite.
127
protected $numTests = -1;
132
protected $testCase = FALSE;
135
* Constructs a new TestSuite:
137
* - PHPUnit_Framework_TestSuite() constructs an empty TestSuite.
139
* - PHPUnit_Framework_TestSuite(ReflectionClass) constructs a
140
* TestSuite from the given class.
142
* - PHPUnit_Framework_TestSuite(ReflectionClass, String)
143
* constructs a TestSuite from the given class with the given
146
* - PHPUnit_Framework_TestSuite(String) either constructs a
147
* TestSuite from the given class (if the passed string is the
148
* name of an existing class) or constructs an empty TestSuite
149
* with the given name.
151
* @param mixed $theClass
152
* @param string $name
153
* @throws InvalidArgumentException
155
public function __construct($theClass = '', $name = '')
157
$argumentsValid = FALSE;
159
if (is_object($theClass) &&
160
$theClass instanceof ReflectionClass) {
161
$argumentsValid = TRUE;
164
else if (is_string($theClass) &&
166
class_exists($theClass, FALSE)) {
167
$argumentsValid = TRUE;
173
$theClass = new ReflectionClass($theClass);
176
else if (is_string($theClass)) {
177
$this->setName($theClass);
181
if (!$argumentsValid) {
182
throw new InvalidArgumentException;
185
if (!$theClass->isSubclassOf('PHPUnit_Framework_TestCase')) {
186
throw new InvalidArgumentException(
187
'Class does not extend PHPUnit_Framework_TestCase.'
191
$filename = $theClass->getFilename();
193
if (strpos($filename, 'eval()') === FALSE) {
194
PHP_CodeCoverage::getInstance()->filter()->addFileToBlacklist(
195
realpath($filename), 'TESTS'
200
$this->setName($name);
202
$this->setName($theClass->getName());
205
$constructor = $theClass->getConstructor();
207
if ($constructor !== NULL &&
208
!$constructor->isPublic()) {
212
'Class "%s" has no public constructor.',
222
foreach ($theClass->getMethods() as $method) {
223
if (strpos($method->getDeclaringClass()->getName(), 'PHPUnit_') !== 0) {
224
$this->addTestMethod($theClass, $method);
228
if (empty($this->tests)) {
232
'No tests found in class "%s".',
240
$this->testCase = TRUE;
244
* Returns a string representation of the test suite.
248
public function toString()
250
return $this->getName();
254
* Adds a test to the suite.
256
* @param PHPUnit_Framework_Test $test
257
* @param array $groups
259
public function addTest(PHPUnit_Framework_Test $test, $groups = array())
261
$class = new ReflectionClass($test);
263
if (!$class->isAbstract()) {
264
$this->tests[] = $test;
265
$this->numTests = -1;
267
if ($test instanceof PHPUnit_Framework_TestSuite &&
269
$groups = $test->getGroups();
272
if (empty($groups)) {
273
$groups = array('__nogroup__');
276
foreach ($groups as $group) {
277
if (!isset($this->groups[$group])) {
278
$this->groups[$group] = array($test);
280
$this->groups[$group][] = $test;
287
* Adds the tests from the given class to the suite.
289
* @param mixed $testClass
290
* @throws InvalidArgumentException
292
public function addTestSuite($testClass)
294
if (is_string($testClass) && class_exists($testClass)) {
295
$testClass = new ReflectionClass($testClass);
298
if (!is_object($testClass)) {
299
throw PHPUnit_Util_InvalidArgumentHelper::factory(
300
1, 'class name or object'
304
if ($testClass instanceof PHPUnit_Framework_TestSuite) {
305
$this->addTest($testClass);
308
else if ($testClass instanceof ReflectionClass) {
309
$suiteMethod = FALSE;
311
if (!$testClass->isAbstract()) {
312
if ($testClass->hasMethod(PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME)) {
313
$method = $testClass->getMethod(
314
PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME
317
if ($method->isStatic()) {
319
$method->invoke(NULL, $testClass->getName())
327
if (!$suiteMethod && !$testClass->isAbstract()) {
328
$this->addTest(new PHPUnit_Framework_TestSuite($testClass));
333
throw new InvalidArgumentException;
338
* Wraps both <code>addTest()</code> and <code>addTestSuite</code>
339
* as well as the separate import statements for the user's convenience.
341
* If the named file cannot be read or there are no new tests that can be
342
* added, a <code>PHPUnit_Framework_Warning</code> will be created instead,
343
* leaving the current test run untouched.
345
* @param string $filename
346
* @param boolean $syntaxCheck
347
* @param array $phptOptions Array with ini settings for the php instance
348
* run, key being the name if the setting,
349
* value the ini value.
350
* @throws InvalidArgumentException
351
* @since Method available since Release 2.3.0
352
* @author Stefano F. Rausch <stefano@rausch-e.net>
354
public function addTestFile($filename, $syntaxCheck = FALSE, $phptOptions = array())
356
if (!is_string($filename)) {
357
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
360
if (file_exists($filename) && substr($filename, -5) == '.phpt') {
362
new PHPUnit_Extensions_PhptTestCase($filename, $phptOptions)
368
if (!file_exists($filename)) {
369
$includePaths = explode(PATH_SEPARATOR, get_include_path());
371
foreach ($includePaths as $includePath) {
372
$file = $includePath . DIRECTORY_SEPARATOR . $filename;
374
if (file_exists($file)) {
381
PHPUnit_Util_Class::collectStart();
382
PHPUnit_Util_Fileloader::checkAndLoad($filename, $syntaxCheck);
383
$newClasses = PHPUnit_Util_Class::collectEnd();
384
$baseName = str_replace('.php', '', basename($filename));
386
foreach ($newClasses as $className) {
387
if (substr($className, 0 - strlen($baseName)) == $baseName) {
388
$newClasses = array($className);
395
foreach ($newClasses as $className) {
396
$class = new ReflectionClass($className);
398
if (!$class->isAbstract()) {
399
if ($class->hasMethod(PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME)) {
400
$method = $class->getMethod(
401
PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME
404
if ($method->isStatic()) {
405
$this->addTest($method->invoke(NULL, $className));
411
else if ($class->implementsInterface('PHPUnit_Framework_Test')) {
412
$this->addTestSuite($class);
419
$this->numTests = -1;
423
* Wrapper for addTestFile() that adds multiple test files.
425
* @param array|Iterator $filenames
426
* @throws InvalidArgumentException
427
* @since Method available since Release 2.3.0
429
public function addTestFiles($filenames, $syntaxCheck = FALSE)
431
if (!(is_array($filenames) ||
432
(is_object($filenames) && $filenames instanceof Iterator))) {
433
throw PHPUnit_Util_InvalidArgumentHelper::factory(
434
1, 'array or iterator'
438
foreach ($filenames as $filename) {
439
$this->addTestFile((string)$filename, $syntaxCheck);
444
* Counts the number of test cases that will be run by this test.
448
public function count()
450
if ($this->numTests > -1) {
451
return $this->numTests;
456
foreach ($this->tests as $test) {
457
$this->numTests += count($test);
460
return $this->numTests;
464
* @param ReflectionClass $theClass
465
* @param string $name
466
* @return PHPUnit_Framework_Test
467
* @throws RuntimeException
469
public static function createTest(ReflectionClass $theClass, $name)
471
$className = $theClass->getName();
473
if (!$theClass->isInstantiable()) {
474
return self::warning(
475
sprintf('Cannot instantiate class "%s".', $className)
479
$backupSettings = PHPUnit_Util_Test::getBackupSettings(
482
$preserveGlobalState = PHPUnit_Util_Test::getPreserveGlobalStateSettings(
485
$runTestInSeparateProcess = PHPUnit_Util_Test::getProcessIsolationSettings(
489
$constructor = $theClass->getConstructor();
491
if ($constructor !== NULL) {
492
$parameters = $constructor->getParameters();
494
// TestCase() or TestCase($name)
495
if (count($parameters) < 2) {
496
$test = new $className;
499
// TestCase($name, $data)
502
$data = PHPUnit_Util_Test::getProvidedData(
507
catch (Exception $e) {
509
'The data provider specified for %s::%s is invalid.',
514
$_message = $e->getMessage();
516
if (!empty($_message)) {
517
$message .= "\n" . $_message;
520
$data = self::warning($message);
523
// Test method with @dataProvider.
525
$test = new PHPUnit_Framework_TestSuite_DataProvider(
526
$className . '::' . $name
530
$data = self::warning(
532
'No tests found in suite "%s".',
538
if ($data instanceof PHPUnit_Framework_Warning) {
539
$test->addTest($data);
543
$groups = PHPUnit_Util_Test::getGroups($className, $name);
545
foreach ($data as $_dataName => $_data) {
546
$_test = new $className($name, $_data, $_dataName);
548
if ($runTestInSeparateProcess) {
549
$_test->setRunTestInSeparateProcess(TRUE);
551
if ($preserveGlobalState !== NULL) {
552
$_test->setPreserveGlobalState($preserveGlobalState);
556
if ($backupSettings['backupGlobals'] !== NULL) {
557
$_test->setBackupGlobals(
558
$backupSettings['backupGlobals']
562
if ($backupSettings['backupStaticAttributes'] !== NULL) {
563
$_test->setBackupStaticAttributes(
564
$backupSettings['backupStaticAttributes']
568
$test->addTest($_test, $groups);
574
$test = new $className;
580
throw new RuntimeException('No valid test provided.');
583
if ($test instanceof PHPUnit_Framework_TestCase) {
584
$test->setName($name);
586
if ($runTestInSeparateProcess) {
587
$test->setRunTestInSeparateProcess(TRUE);
589
if ($preserveGlobalState !== NULL) {
590
$test->setPreserveGlobalState($preserveGlobalState);
594
if ($backupSettings['backupGlobals'] !== NULL) {
595
$test->setBackupGlobals($backupSettings['backupGlobals']);
598
if ($backupSettings['backupStaticAttributes'] !== NULL) {
599
$test->setBackupStaticAttributes(
600
$backupSettings['backupStaticAttributes']
609
* Creates a default TestResult object.
611
* @return PHPUnit_Framework_TestResult
613
protected function createResult()
615
return new PHPUnit_Framework_TestResult;
619
* Returns the name of the suite.
623
public function getName()
629
* Returns the test groups of the suite.
632
* @since Method available since Release 3.2.0
634
public function getGroups()
636
return array_keys($this->groups);
640
* Runs the tests and collects their result in a TestResult.
642
* @param PHPUnit_Framework_TestResult $result
643
* @param mixed $filter
644
* @param array $groups
645
* @param array $excludeGroups
646
* @param boolean $processIsolation
647
* @return PHPUnit_Framework_TestResult
648
* @throws InvalidArgumentException
650
public function run(PHPUnit_Framework_TestResult $result = NULL, $filter = FALSE, array $groups = array(), array $excludeGroups = array(), $processIsolation = FALSE)
652
if ($result === NULL) {
653
$result = $this->createResult();
656
$result->startTestSuite($this);
661
if ($this->testCase &&
662
method_exists($this->name, 'setUpBeforeClass')) {
663
call_user_func(array($this->name, 'setUpBeforeClass'));
667
catch (PHPUnit_Framework_SkippedTestSuiteError $e) {
668
$numTests = count($this);
670
for ($i = 0; $i < $numTests; $i++) {
671
$result->addFailure($this, $e, 0);
677
if (empty($groups)) {
678
$tests = $this->tests;
680
$tests = new SplObjectStorage;
682
foreach ($groups as $group) {
683
if (isset($this->groups[$group])) {
684
foreach ($this->groups[$group] as $test) {
685
$tests->attach($test);
691
foreach ($tests as $test) {
692
if ($result->shouldStop()) {
696
if ($test instanceof PHPUnit_Framework_TestSuite) {
697
$test->setBackupGlobals($this->backupGlobals);
698
$test->setBackupStaticAttributes($this->backupStaticAttributes);
701
$result, $filter, $groups, $excludeGroups, $processIsolation
706
if ($filter !== FALSE ) {
707
$tmp = PHPUnit_Util_Test::describe($test, FALSE);
710
$name = join('::', $tmp);
715
if (preg_match($filter, $name) == 0) {
720
if ($runTest && !empty($excludeGroups)) {
721
foreach ($this->groups as $_group => $_tests) {
722
if (in_array($_group, $excludeGroups)) {
723
foreach ($_tests as $_test) {
724
if ($test === $_test) {
734
if ($test instanceof PHPUnit_Framework_TestCase) {
735
$test->setBackupGlobals($this->backupGlobals);
736
$test->setBackupStaticAttributes(
737
$this->backupStaticAttributes
739
$test->setRunTestInSeparateProcess($processIsolation);
742
$this->runTest($test, $result);
747
if ($this->testCase &&
748
method_exists($this->name, 'tearDownAfterClass')) {
749
call_user_func(array($this->name, 'tearDownAfterClass'));
753
$result->endTestSuite($this);
761
* @param PHPUnit_Framework_Test $test
762
* @param PHPUnit_Framework_TestResult $testResult
764
public function runTest(PHPUnit_Framework_Test $test, PHPUnit_Framework_TestResult $result)
770
* Sets the name of the suite.
774
public function setName($name)
780
* Returns the test at the given index.
783
* @return PHPUnit_Framework_Test
785
public function testAt($index)
787
if (isset($this->tests[$index])) {
788
return $this->tests[$index];
795
* Returns the tests as an enumeration.
799
public function tests()
805
* Mark the test suite as skipped.
807
* @param string $message
808
* @throws PHPUnit_Framework_SkippedTestSuiteError
809
* @since Method available since Release 3.0.0
811
public function markTestSuiteSkipped($message = '')
813
throw new PHPUnit_Framework_SkippedTestSuiteError($message);
817
* @param ReflectionClass $class
818
* @param ReflectionMethod $method
820
protected function addTestMethod(ReflectionClass $class, ReflectionMethod $method)
822
$name = $method->getName();
824
if ($this->isPublicTestMethod($method)) {
825
$test = self::createTest($class, $name);
827
if ($test instanceof PHPUnit_Framework_TestCase ||
828
$test instanceof PHPUnit_Framework_TestSuite_DataProvider) {
829
$test->setDependencies(
830
PHPUnit_Util_Test::getDependencies($class->getName(), $name)
834
$this->addTest($test, PHPUnit_Util_Test::getGroups(
835
$class->getName(), $name)
839
else if ($this->isTestMethod($method)) {
843
'Test method "%s" is not public.',
853
* @param ReflectionMethod $method
856
public static function isPublicTestMethod(ReflectionMethod $method)
858
return (self::isTestMethod($method) && $method->isPublic());
862
* @param ReflectionMethod $method
865
public static function isTestMethod(ReflectionMethod $method)
867
if (strpos($method->name, 'test') === 0) {
871
// @scenario on TestCase::testMethod()
872
// @test on TestCase::testMethod()
873
return strpos($method->getDocComment(), '@test') !== FALSE ||
874
strpos($method->getDocComment(), '@scenario') !== FALSE;
878
* @param string $message
879
* @return PHPUnit_Framework_Warning
881
protected static function warning($message)
883
return new PHPUnit_Framework_Warning($message);
887
* @param boolean $backupGlobals
888
* @since Method available since Release 3.3.0
890
public function setBackupGlobals($backupGlobals)
892
if (is_null($this->backupGlobals) && is_bool($backupGlobals)) {
893
$this->backupGlobals = $backupGlobals;
898
* @param boolean $backupStaticAttributes
899
* @since Method available since Release 3.4.0
901
public function setBackupStaticAttributes($backupStaticAttributes)
903
if (is_null($this->backupStaticAttributes) &&
904
is_bool($backupStaticAttributes)) {
905
$this->backupStaticAttributes = $backupStaticAttributes;
910
* Returns an iterator for this test suite.
912
* @return RecursiveIteratorIterator
913
* @since Method available since Release 3.1.0
915
public function getIterator()
917
return new RecursiveIteratorIterator(
918
new PHPUnit_Util_TestSuiteIterator($this)
923
* Template Method that is called before the tests
924
* of this test suite are run.
926
* @since Method available since Release 3.1.0
928
protected function setUp()
933
* Template Method that is called after the tests
934
* of this test suite have finished running.
936
* @since Method available since Release 3.1.0
938
protected function tearDown()