5
* Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.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.
39
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.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 'PHPUnit/Framework.php';
48
require_once 'PHPUnit/Runner/BaseTestRunner.php';
49
require_once 'PHPUnit/Util/Class.php';
50
require_once 'PHPUnit/Util/Fileloader.php';
51
require_once 'PHPUnit/Util/InvalidArgumentHelper.php';
52
require_once 'PHPUnit/Util/Test.php';
53
require_once 'PHPUnit/Util/TestSuiteIterator.php';
55
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
58
* A TestSuite is a composite of Tests. It runs a collection of test cases.
60
* Here is an example using the dynamic test definition.
64
* $suite = new PHPUnit_Framework_TestSuite;
65
* $suite->addTest(new MathTest('testPass'));
69
* Alternatively, a TestSuite can extract the tests to be run automatically.
70
* To do so you pass a ReflectionClass instance for your
71
* PHPUnit_Framework_TestCase class to the PHPUnit_Framework_TestSuite
76
* $suite = new PHPUnit_Framework_TestSuite(
77
* new ReflectionClass('MathTest')
82
* This constructor creates a suite with all the methods starting with
83
* "test" that take no arguments.
87
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
88
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
89
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
90
* @version Release: 3.4.14
91
* @link http://www.phpunit.de/
92
* @since Class available since Release 2.0.0
94
class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing, IteratorAggregate
97
* Enable or disable the backup and restoration of the $GLOBALS array.
101
protected $backupGlobals = NULL;
104
* Enable or disable the backup and restoration of static attributes.
108
protected $backupStaticAttributes = NULL;
111
* Fixture that is shared between the tests of this test suite.
115
protected $sharedFixture;
118
* The name of the test suite.
122
protected $name = '';
125
* The test groups of the test suite.
129
protected $groups = array();
132
* The tests in the test suite.
136
protected $tests = array();
139
* The number of tests in the test suite.
143
protected $numTests = -1;
148
protected $testCase = FALSE;
151
* Constructs a new TestSuite:
153
* - PHPUnit_Framework_TestSuite() constructs an empty TestSuite.
155
* - PHPUnit_Framework_TestSuite(ReflectionClass) constructs a
156
* TestSuite from the given class.
158
* - PHPUnit_Framework_TestSuite(ReflectionClass, String)
159
* constructs a TestSuite from the given class with the given
162
* - PHPUnit_Framework_TestSuite(String) either constructs a
163
* TestSuite from the given class (if the passed string is the
164
* name of an existing class) or constructs an empty TestSuite
165
* with the given name.
167
* @param mixed $theClass
168
* @param string $name
169
* @throws InvalidArgumentException
171
public function __construct($theClass = '', $name = '')
173
$argumentsValid = FALSE;
175
if (is_object($theClass) &&
176
$theClass instanceof ReflectionClass) {
177
$argumentsValid = TRUE;
180
else if (is_string($theClass) && $theClass !== ''
181
&& class_exists($theClass, FALSE)) {
182
$argumentsValid = TRUE;
188
$theClass = new ReflectionClass($theClass);
191
else if (is_string($theClass)) {
192
$this->setName($theClass);
196
if (!$argumentsValid) {
197
throw new InvalidArgumentException;
200
$filename = $theClass->getFilename();
202
if (strpos($filename, 'eval()') === FALSE) {
203
PHPUnit_Util_Filter::addFileToFilter(realpath($filename), 'TESTS');
207
$this->setName($name);
209
$this->setName($theClass->getName());
212
$constructor = $theClass->getConstructor();
214
if ($constructor !== NULL &&
215
!$constructor->isPublic()) {
219
'Class "%s" has no public constructor.',
231
foreach ($theClass->getMethods() as $method) {
232
if (strpos($method->getDeclaringClass()->getName(), 'PHPUnit_') !== 0) {
233
$this->addTestMethod($theClass, $method, $names);
237
if (empty($this->tests)) {
241
'No tests found in class "%s".',
249
$this->testCase = TRUE;
253
* Returns a string representation of the test suite.
257
public function toString()
259
return $this->getName();
263
* Adds a test to the suite.
265
* @param PHPUnit_Framework_Test $test
266
* @param array $groups
268
public function addTest(PHPUnit_Framework_Test $test, $groups = array())
270
$class = new ReflectionClass($test);
272
if (!$class->isAbstract()) {
273
$this->tests[] = $test;
274
$this->numTests = -1;
276
if ($test instanceof PHPUnit_Framework_TestSuite &&
278
$groups = $test->getGroups();
281
if (empty($groups)) {
282
$groups = array('__nogroup__');
285
foreach ($groups as $group) {
286
if (!isset($this->groups[$group])) {
287
$this->groups[$group] = array($test);
289
$this->groups[$group][] = $test;
296
* Adds the tests from the given class to the suite.
298
* @param mixed $testClass
299
* @throws InvalidArgumentException
301
public function addTestSuite($testClass)
303
if (is_string($testClass) && class_exists($testClass)) {
304
$testClass = new ReflectionClass($testClass);
307
if (!is_object($testClass)) {
308
throw PHPUnit_Util_InvalidArgumentHelper::factory(
309
1, 'class name or object'
313
if ($testClass instanceof PHPUnit_Framework_TestSuite) {
314
$this->addTest($testClass);
317
else if ($testClass instanceof ReflectionClass) {
318
$suiteMethod = FALSE;
320
if (!$testClass->isAbstract()) {
321
if ($testClass->hasMethod(PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME)) {
322
$method = $testClass->getMethod(
323
PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME
326
if ($method->isStatic()) {
328
$method->invoke(NULL, $testClass->getName())
336
if (!$suiteMethod && !$testClass->isAbstract()) {
337
$this->addTest(new PHPUnit_Framework_TestSuite($testClass));
342
throw new InvalidArgumentException;
347
* Wraps both <code>addTest()</code> and <code>addTestSuite</code>
348
* as well as the separate import statements for the user's convenience.
350
* If the named file cannot be read or there are no new tests that can be
351
* added, a <code>PHPUnit_Framework_Warning</code> will be created instead,
352
* leaving the current test run untouched.
354
* @param string $filename
355
* @param boolean $syntaxCheck
356
* @param array $phptOptions Array with ini settings for the php instance
357
* run, key being the name if the setting,
358
* value the ini value.
359
* @throws InvalidArgumentException
360
* @since Method available since Release 2.3.0
361
* @author Stefano F. Rausch <stefano@rausch-e.net>
363
public function addTestFile($filename, $syntaxCheck = FALSE, $phptOptions = array())
365
if (!is_string($filename)) {
366
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
369
if (file_exists($filename) && substr($filename, -5) == '.phpt') {
370
require_once 'PHPUnit/Extensions/PhptTestCase.php';
373
new PHPUnit_Extensions_PhptTestCase($filename, $phptOptions)
379
if (!file_exists($filename)) {
380
$includePaths = explode(PATH_SEPARATOR, get_include_path());
382
foreach ($includePaths as $includePath) {
383
$file = $includePath . DIRECTORY_SEPARATOR . $filename;
385
if (file_exists($file)) {
392
PHPUnit_Util_Class::collectStart();
393
PHPUnit_Util_Fileloader::checkAndLoad($filename, $syntaxCheck);
394
$newClasses = PHPUnit_Util_Class::collectEnd();
395
$baseName = str_replace('.php', '', basename($filename));
397
foreach ($newClasses as $className) {
398
if (substr($className, 0 - strlen($baseName)) == $baseName) {
399
$newClasses = array($className);
406
foreach ($newClasses as $className) {
407
$class = new ReflectionClass($className);
409
if (!$class->isAbstract()) {
410
if ($class->hasMethod(PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME)) {
411
$method = $class->getMethod(
412
PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME
415
if ($method->isStatic()) {
416
$this->addTest($method->invoke(NULL, $className));
422
else if ($class->implementsInterface('PHPUnit_Framework_Test')) {
423
$this->addTestSuite($class);
430
$this->numTests = -1;
434
* Wrapper for addTestFile() that adds multiple test files.
436
* @param array|Iterator $filenames
437
* @throws InvalidArgumentException
438
* @since Method available since Release 2.3.0
440
public function addTestFiles($filenames, $syntaxCheck = FALSE)
442
if (!(is_array($filenames) ||
443
(is_object($filenames) && $filenames instanceof Iterator))) {
444
throw PHPUnit_Util_InvalidArgumentHelper::factory(
445
1, 'array or iterator'
449
foreach ($filenames as $filename) {
450
$this->addTestFile((string)$filename, $syntaxCheck);
455
* Counts the number of test cases that will be run by this test.
459
public function count()
461
if ($this->numTests > -1) {
462
return $this->numTests;
467
foreach ($this->tests as $test) {
468
$this->numTests += count($test);
471
return $this->numTests;
475
* @param ReflectionClass $theClass
476
* @param string $name
477
* @param array $classGroups
478
* @return PHPUnit_Framework_Test
480
public static function createTest(ReflectionClass $theClass, $name, array $classGroups = array())
482
$className = $theClass->getName();
484
if (!$theClass->isInstantiable()) {
485
return self::warning(
486
sprintf('Cannot instantiate class "%s".', $className)
490
$classDocComment = $theClass->getDocComment();
491
$method = new ReflectionMethod($className, $name);
492
$methodDocComment = $method->getDocComment();
493
$backupSettings = PHPUnit_Util_Test::getBackupSettings(
496
$preserveGlobalState = PHPUnit_Util_Test::getPreserveGlobalStateSettings(
499
$runTestInSeparateProcess = PHPUnit_Util_Test::getProcessIsolationSettings(
503
$constructor = $theClass->getConstructor();
505
if ($constructor !== NULL) {
506
$parameters = $constructor->getParameters();
508
// TestCase() or TestCase($name)
509
if (count($parameters) < 2) {
510
$test = new $className;
513
// TestCase($name, $data)
516
$data = PHPUnit_Util_Test::getProvidedData(
521
catch (Exception $e) {
523
'The data provider specified for %s::%s is invalid.',
528
$_message = $e->getMessage();
530
if (!empty($_message)) {
531
$message .= "\n" . $_message;
534
return new PHPUnit_Framework_Warning($message);
537
$groups = PHPUnit_Util_Test::getGroups($className, $name);
539
// Test method with @dataProvider.
541
$test = new PHPUnit_Framework_TestSuite_DataProvider(
542
$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);
573
$test = new $className;
578
if ($test instanceof PHPUnit_Framework_TestCase) {
579
$test->setName($name);
581
if ($runTestInSeparateProcess) {
582
$test->setRunTestInSeparateProcess(TRUE);
584
if ($preserveGlobalState !== NULL) {
585
$test->setPreserveGlobalState($preserveGlobalState);
589
if ($backupSettings['backupGlobals'] !== NULL) {
590
$test->setBackupGlobals($backupSettings['backupGlobals']);
593
if ($backupSettings['backupStaticAttributes'] !== NULL) {
594
$test->setBackupStaticAttributes(
595
$backupSettings['backupStaticAttributes']
604
* Creates a default TestResult object.
606
* @return PHPUnit_Framework_TestResult
608
protected function createResult()
610
return new PHPUnit_Framework_TestResult;
614
* Returns the name of the suite.
618
public function getName()
624
* Returns the test groups of the suite.
627
* @since Method available since Release 3.2.0
629
public function getGroups()
631
return array_keys($this->groups);
635
* Runs the tests and collects their result in a TestResult.
637
* @param PHPUnit_Framework_TestResult $result
638
* @param mixed $filter
639
* @param array $groups
640
* @param array $excludeGroups
641
* @param boolean $processIsolation
642
* @return PHPUnit_Framework_TestResult
643
* @throws InvalidArgumentException
645
public function run(PHPUnit_Framework_TestResult $result = NULL, $filter = FALSE, array $groups = array(), array $excludeGroups = array(), $processIsolation = FALSE)
647
if ($result === NULL) {
648
$result = $this->createResult();
651
$result->startTestSuite($this);
656
if ($this->testCase &&
657
method_exists($this->name, 'setUpBeforeClass')) {
658
call_user_func(array($this->name, 'setUpBeforeClass'));
662
catch (PHPUnit_Framework_SkippedTestSuiteError $e) {
663
$numTests = count($this);
665
for ($i = 0; $i < $numTests; $i++) {
666
$result->addFailure($this, $e, 0);
672
if (empty($groups)) {
673
$tests = $this->tests;
675
$tests = new SplObjectStorage;
677
foreach ($groups as $group) {
678
if (isset($this->groups[$group])) {
679
foreach ($this->groups[$group] as $test) {
680
$tests->attach($test);
686
foreach ($tests as $test) {
687
if ($result->shouldStop()) {
691
if ($test instanceof PHPUnit_Framework_TestSuite) {
692
$test->setBackupGlobals($this->backupGlobals);
693
$test->setBackupStaticAttributes($this->backupStaticAttributes);
694
$test->setSharedFixture($this->sharedFixture);
697
$result, $filter, $groups, $excludeGroups, $processIsolation
702
if ($filter !== FALSE ) {
703
$tmp = PHPUnit_Util_Test::describe($test, FALSE);
706
$name = join('::', $tmp);
711
if (preg_match($filter, $name) == 0) {
716
if ($runTest && !empty($excludeGroups)) {
717
foreach ($this->groups as $_group => $_tests) {
718
if (in_array($_group, $excludeGroups)) {
719
foreach ($_tests as $_test) {
720
if ($test === $_test) {
730
if ($test instanceof PHPUnit_Framework_TestCase) {
731
$test->setBackupGlobals($this->backupGlobals);
732
$test->setBackupStaticAttributes(
733
$this->backupStaticAttributes
735
$test->setSharedFixture($this->sharedFixture);
736
$test->setRunTestInSeparateProcess($processIsolation);
739
$this->runTest($test, $result);
744
if ($this->testCase &&
745
method_exists($this->name, 'tearDownAfterClass')) {
746
call_user_func(array($this->name, 'tearDownAfterClass'));
750
$result->endTestSuite($this);
758
* @param PHPUnit_Framework_Test $test
759
* @param PHPUnit_Framework_TestResult $testResult
761
public function runTest(PHPUnit_Framework_Test $test, PHPUnit_Framework_TestResult $result)
767
* Sets the name of the suite.
771
public function setName($name)
777
* Returns the test at the given index.
780
* @return PHPUnit_Framework_Test
782
public function testAt($index)
784
if (isset($this->tests[$index])) {
785
return $this->tests[$index];
792
* Returns the tests as an enumeration.
796
public function tests()
802
* Mark the test suite as skipped.
804
* @param string $message
805
* @throws PHPUnit_Framework_SkippedTestSuiteError
806
* @since Method available since Release 3.0.0
808
public function markTestSuiteSkipped($message = '')
810
throw new PHPUnit_Framework_SkippedTestSuiteError($message);
814
* @param ReflectionClass $class
815
* @param ReflectionMethod $method
816
* @param array $names
818
protected function addTestMethod(ReflectionClass $class, ReflectionMethod $method, array &$names)
820
$name = $method->getName();
822
if (in_array($name, $names)) {
826
if ($this->isPublicTestMethod($method)) {
829
$test = self::createTest($class, $name);
831
if ($test instanceof PHPUnit_Framework_TestCase ||
832
$test instanceof PHPUnit_Framework_TestSuite_DataProvider) {
833
$test->setDependencies(
834
PHPUnit_Util_Test::getDependencies($class->getName(), $name)
838
$this->addTest($test, PHPUnit_Util_Test::getGroups(
839
$class->getName(), $name)
843
else if ($this->isTestMethod($method)) {
847
'Test method "%s" is not public.',
857
* @param ReflectionMethod $method
860
public static function isPublicTestMethod(ReflectionMethod $method)
862
return (self::isTestMethod($method) && $method->isPublic());
866
* @param ReflectionMethod $method
869
public static function isTestMethod(ReflectionMethod $method)
871
if (strpos($method->name, 'test') === 0) {
875
// @scenario on TestCase::testMethod()
876
// @test on TestCase::testMethod()
877
return strpos($method->getDocComment(), '@test') !== FALSE ||
878
strpos($method->getDocComment(), '@scenario') !== FALSE;
882
* @param string $message
883
* @return PHPUnit_Framework_Warning
885
protected static function warning($message)
887
return new PHPUnit_Framework_Warning($message);
891
* @param boolean $backupGlobals
892
* @since Method available since Release 3.3.0
894
public function setBackupGlobals($backupGlobals)
896
if (is_null($this->backupGlobals) && is_bool($backupGlobals)) {
897
$this->backupGlobals = $backupGlobals;
902
* @param boolean $backupStaticAttributes
903
* @since Method available since Release 3.4.0
905
public function setBackupStaticAttributes($backupStaticAttributes)
907
if (is_null($this->backupStaticAttributes) &&
908
is_bool($backupStaticAttributes)) {
909
$this->backupStaticAttributes = $backupStaticAttributes;
914
* Sets the shared fixture for the tests of this test suite.
916
* @param mixed $sharedFixture
917
* @since Method available since Release 3.1.0
919
public function setSharedFixture($sharedFixture)
921
$this->sharedFixture = $sharedFixture;
925
* Returns an iterator for this test suite.
927
* @return RecursiveIteratorIterator
928
* @since Method available since Release 3.1.0
930
public function getIterator()
932
return new RecursiveIteratorIterator(
933
new PHPUnit_Util_TestSuiteIterator($this)
938
* Template Method that is called before the tests
939
* of this test suite are run.
941
* @since Method available since Release 3.1.0
943
protected function setUp()
948
* Template Method that is called after the tests
949
* of this test suite have finished running.
951
* @since Method available since Release 3.1.0
953
protected function tearDown()
b'\\ No newline at end of file'