5 * Copyright (c) 2002-2009, 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-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @link http://www.phpunit.de/
44 * @since File available since Release 2.0.0
47 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/Filter.php';
52 require_once 'PHPUnit/Util/Test.php';
53 require_once 'PHPUnit/Util/TestSuiteIterator.php';
55 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
57 if (!class_exists('PHPUnit_Framework_TestSuite', FALSE)) {
60 * A TestSuite is a composite of Tests. It runs a collection of test cases.
62 * Here is an example using the dynamic test definition.
66 * $suite = new PHPUnit_Framework_TestSuite;
67 * $suite->addTest(new MathTest('testPass'));
71 * Alternatively, a TestSuite can extract the tests to be run automatically.
72 * To do so you pass a ReflectionClass instance for your
73 * PHPUnit_Framework_TestCase class to the PHPUnit_Framework_TestSuite
78 * $suite = new PHPUnit_Framework_TestSuite(
79 * new ReflectionClass('MathTest')
84 * This constructor creates a suite with all the methods starting with
85 * "test" that take no arguments.
89 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
90 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
91 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
92 * @version Release: 3.3.17
93 * @link http://www.phpunit.de/
94 * @since Class available since Release 2.0.0
96 class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing, IteratorAggregate
99 * Enable or disable the backup and restoration of the $GLOBALS array.
103 protected $backupGlobals = NULL;
106 * Fixture that is shared between the tests of this test suite.
110 protected $sharedFixture;
113 * The name of the test suite.
117 protected $name = '';
120 * The test groups of the test suite.
124 protected $groups = array();
127 * The tests in the test suite.
131 protected $tests = array();
134 * The number of tests in the test suite.
138 protected $numTests = -1;
141 * Constructs a new TestSuite:
143 * - PHPUnit_Framework_TestSuite() constructs an empty TestSuite.
145 * - PHPUnit_Framework_TestSuite(ReflectionClass) constructs a
146 * TestSuite from the given class.
148 * - PHPUnit_Framework_TestSuite(ReflectionClass, String)
149 * constructs a TestSuite from the given class with the given
152 * - PHPUnit_Framework_TestSuite(String) either constructs a
153 * TestSuite from the given class (if the passed string is the
154 * name of an existing class) or constructs an empty TestSuite
155 * with the given name.
157 * @param mixed $theClass
158 * @param string $name
159 * @throws InvalidArgumentException
161 public function __construct($theClass = '', $name = '')
163 $argumentsValid = FALSE;
165 if (is_object($theClass) &&
166 $theClass instanceof ReflectionClass) {
167 $argumentsValid = TRUE;
170 else if (is_string($theClass) && $theClass !== ''
171 && class_exists($theClass, FALSE)) {
172 $argumentsValid = TRUE;
178 $theClass = new ReflectionClass($theClass);
181 else if (is_string($theClass)) {
182 $this->setName($theClass);
186 if (!$argumentsValid) {
187 throw new InvalidArgumentException;
190 $filename = $theClass->getFilename();
192 if (strpos($filename, 'eval()') === FALSE) {
193 PHPUnit_Util_Filter::addFileToFilter(realpath($filename), 'TESTS');
197 $this->setName($name);
199 $this->setName($theClass->getName());
202 $constructor = $theClass->getConstructor();
204 if ($constructor !== NULL &&
205 !$constructor->isPublic()) {
209 'Class "%s" has no public constructor.',
219 $className = $theClass->getName();
220 $classDocComment = $theClass->getDocComment();
222 $classGroups = PHPUnit_Util_Test::getGroups($classDocComment);
224 foreach ($theClass->getMethods() as $method) {
225 if (strpos($method->getDeclaringClass()->getName(), 'PHPUnit_') !== 0) {
226 $methodDocComment = $method->getDocComment();
228 $this->addTestMethod(
231 PHPUnit_Util_Test::getGroups($methodDocComment, $classGroups),
237 if (empty($this->tests)) {
241 'No tests found in class "%s".',
251 * Returns a string representation of the test suite.
255 public function toString()
257 return $this->getName();
261 * Adds a test to the suite.
263 * @param PHPUnit_Framework_Test $test
264 * @param array $groups
266 public function addTest(PHPUnit_Framework_Test $test, $groups = array())
268 $class = new ReflectionClass($test);
270 if (!$class->isAbstract()) {
271 $this->tests[] = $test;
272 $this->numTests = -1;
274 if ($test instanceof PHPUnit_Framework_TestSuite && empty($groups)) {
275 $groups = $test->getGroups();
278 if (empty($groups)) {
279 $groups = array('__nogroup__');
282 foreach ($groups as $group) {
283 if (!isset($this->groups[$group])) {
284 $this->groups[$group] = array($test);
286 $this->groups[$group][] = $test;
293 * Adds the tests from the given class to the suite.
295 * @param mixed $testClass
296 * @throws InvalidArgumentException
298 public function addTestSuite($testClass)
300 if (is_string($testClass) && class_exists($testClass)) {
301 $testClass = new ReflectionClass($testClass);
304 if (!is_object($testClass)) {
305 throw new InvalidArgumentException;
308 if ($testClass instanceof PHPUnit_Framework_TestSuite) {
309 $this->addTest($testClass);
312 else if ($testClass instanceof ReflectionClass) {
313 $suiteMethod = FALSE;
315 if (!$testClass->isAbstract()) {
316 if ($testClass->hasMethod(PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME)) {
317 $method = $testClass->getMethod(
318 PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME
321 if ($method->isStatic()) {
322 $this->addTest($method->invoke(NULL, $testClass->getName()));
328 if (!$suiteMethod && !$testClass->isAbstract()) {
329 $this->addTest(new PHPUnit_Framework_TestSuite($testClass));
334 throw new InvalidArgumentException;
339 * Wraps both <code>addTest()</code> and <code>addTestSuite</code>
340 * as well as the separate import statements for the user's convenience.
342 * If the named file cannot be read or there are no new tests that can be
343 * added, a <code>PHPUnit_Framework_Warning</code> will be created instead,
344 * leaving the current test run untouched.
346 * @param string $filename
347 * @param boolean $syntaxCheck
348 * @param array $phptOptions Array with ini settings for the php instance
349 * run, key being the name if the setting,
350 * value the ini value.
351 * @throws InvalidArgumentException
352 * @since Method available since Release 2.3.0
353 * @author Stefano F. Rausch <stefano@rausch-e.net>
355 public function addTestFile($filename, $syntaxCheck = TRUE, $phptOptions = array())
357 if (!is_string($filename)) {
358 throw new InvalidArgumentException;
361 if (file_exists($filename) && substr($filename, -5) == '.phpt') {
362 require_once 'PHPUnit/Extensions/PhptTestCase.php';
365 new PHPUnit_Extensions_PhptTestCase($filename, $phptOptions)
371 if (!file_exists($filename)) {
372 $includePaths = explode(PATH_SEPARATOR, get_include_path());
374 foreach ($includePaths as $includePath) {
375 $file = $includePath . DIRECTORY_SEPARATOR . $filename;
377 if (file_exists($file)) {
384 PHPUnit_Util_Class::collectStart();
385 PHPUnit_Util_Fileloader::checkAndLoad($filename, $syntaxCheck);
386 $newClasses = PHPUnit_Util_Class::collectEnd();
390 foreach ($newClasses as $className) {
391 $class = new ReflectionClass($className);
393 if (!$class->isAbstract()) {
394 if ($class->hasMethod(PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME)) {
395 $method = $class->getMethod(
396 PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME
399 if ($method->isStatic()) {
400 $this->addTest($method->invoke(NULL, $className));
406 else if ($class->implementsInterface('PHPUnit_Framework_Test')) {
407 $this->addTestSuite($class);
416 new PHPUnit_Framework_Warning(
417 'No tests found in file "' . $filename . '".'
422 $this->numTests = -1;
426 * Wrapper for addTestFile() that adds multiple test files.
428 * @param array|Iterator $filenames
429 * @throws InvalidArgumentException
430 * @since Method available since Release 2.3.0
432 public function addTestFiles($filenames, $syntaxCheck = TRUE)
434 if (!(is_array($filenames) ||
435 (is_object($filenames) && $filenames instanceof Iterator))) {
436 throw new InvalidArgumentException;
439 foreach ($filenames as $filename) {
440 $this->addTestFile((string)$filename, $syntaxCheck);
445 * Counts the number of test cases that will be run by this test.
449 public function count()
451 if ($this->numTests > -1) {
452 return $this->numTests;
457 foreach ($this->tests as $test) {
458 $this->numTests += count($test);
461 return $this->numTests;
465 * @param ReflectionClass $theClass
466 * @param string $name
467 * @param array $classGroups
468 * @return PHPUnit_Framework_Test
470 public static function createTest(ReflectionClass $theClass, $name, array $classGroups = array())
472 $className = $theClass->getName();
473 $method = new ReflectionMethod($className, $name);
474 $methodDocComment = $method->getDocComment();
476 if (!$theClass->isInstantiable()) {
477 return self::warning(
478 sprintf('Cannot instantiate class "%s".', $className)
482 $constructor = $theClass->getConstructor();
483 $expectedException = PHPUnit_Util_Test::getExpectedException($methodDocComment);
485 if ($constructor !== NULL) {
486 $parameters = $constructor->getParameters();
488 // TestCase() or TestCase($name)
489 if (count($parameters) < 2) {
490 $test = new $className;
493 // TestCase($name, $data)
495 $data = PHPUnit_Util_Test::getProvidedData($className, $name, $methodDocComment);
496 $groups = PHPUnit_Util_Test::getGroups($methodDocComment, $classGroups);
498 if (is_array($data) || $data instanceof Iterator) {
499 $test = new PHPUnit_Framework_TestSuite(
500 $className . '::' . $name
503 foreach ($data as $_dataName => $_data) {
504 $_test = new $className($name, $_data, $_dataName);
506 if ($_test instanceof PHPUnit_Framework_TestCase &&
507 isset($expectedException)) {
508 $_test->setExpectedException(
509 $expectedException['class'],
510 $expectedException['message'],
511 $expectedException['code']
515 $test->addTest($_test, $groups);
518 $test = new $className;
523 if ($test instanceof PHPUnit_Framework_TestCase) {
524 $test->setName($name);
526 if (isset($expectedException)) {
527 $test->setExpectedException(
528 $expectedException['class'],
529 $expectedException['message'],
530 $expectedException['code']
539 * Creates a default TestResult object.
541 * @return PHPUnit_Framework_TestResult
543 protected function createResult()
545 return new PHPUnit_Framework_TestResult;
549 * Returns the name of the suite.
553 public function getName()
559 * Returns the test groups of the suite.
562 * @since Method available since Release 3.2.0
564 public function getGroups()
566 return array_keys($this->groups);
570 * Runs the tests and collects their result in a TestResult.
572 * @param PHPUnit_Framework_TestResult $result
573 * @param mixed $filter
574 * @param array $groups
575 * @param array $excludeGroups
576 * @return PHPUnit_Framework_TestResult
577 * @throws InvalidArgumentException
579 public function run(PHPUnit_Framework_TestResult $result = NULL, $filter = FALSE, array $groups = array(), array $excludeGroups = array())
581 if ($result === NULL) {
582 $result = $this->createResult();
589 catch (PHPUnit_Framework_SkippedTestSuiteError $e) {
590 $numTests = count($this);
592 for ($i = 0; $i < $numTests; $i++) {
593 $result->addFailure($this, $e, 0);
599 $result->startTestSuite($this);
601 if (empty($groups)) {
602 $tests = $this->tests;
604 $tests = new SplObjectStorage;
606 foreach ($groups as $group) {
607 if (isset($this->groups[$group])) {
608 foreach ($this->groups[$group] as $test) {
609 $tests->attach($test);
615 foreach ($tests as $test) {
616 if ($result->shouldStop()) {
620 if ($test instanceof PHPUnit_Framework_TestSuite) {
621 $test->setBackupGlobals($this->backupGlobals);
622 $test->setSharedFixture($this->sharedFixture);
623 $test->run($result, $filter, $groups, $excludeGroups);
627 if ($filter !== FALSE ) {
628 $tmp = PHPUnit_Util_Test::describe($test, FALSE);
631 $name = join('::', $tmp);
636 if (preg_match($filter, $name) == 0) {
641 if ($runTest && !empty($excludeGroups)) {
642 foreach ($this->groups as $_group => $_tests) {
643 if (in_array($_group, $excludeGroups)) {
644 foreach ($_tests as $_test) {
645 if ($test === $_test) {
655 if ($test instanceof PHPUnit_Framework_TestCase) {
656 $test->setBackupGlobals($this->backupGlobals);
657 $test->setSharedFixture($this->sharedFixture);
660 $this->runTest($test, $result);
665 $result->endTestSuite($this);
674 * @param PHPUnit_Framework_Test $test
675 * @param PHPUnit_Framework_TestResult $testResult
677 public function runTest(PHPUnit_Framework_Test $test, PHPUnit_Framework_TestResult $result)
683 * Sets the name of the suite.
687 public function setName($name)
693 * Returns the test at the given index.
696 * @return PHPUnit_Framework_Test
698 public function testAt($index)
700 if (isset($this->tests[$index])) {
701 return $this->tests[$index];
708 * Returns the tests as an enumeration.
712 public function tests()
718 * Mark the test suite as skipped.
720 * @param string $message
721 * @throws PHPUnit_Framework_SkippedTestSuiteError
722 * @since Method available since Release 3.0.0
724 public function markTestSuiteSkipped($message = '')
726 throw new PHPUnit_Framework_SkippedTestSuiteError($message);
730 * @param ReflectionClass $class
731 * @param ReflectionMethod $method
732 * @param string $groups
733 * @param array $names
735 protected function addTestMethod(ReflectionClass $class, ReflectionMethod $method, array $groups, array &$names)
737 $name = $method->getName();
739 if (in_array($name, $names)) {
743 if ($this->isPublicTestMethod($method)) {
746 $test = self::createTest($class, $name, $groups);
748 $this->addTest($test, $groups);
751 else if ($this->isTestMethod($method)) {
755 'Test method "%s" is not public.',
765 * @param ReflectionMethod $method
768 public static function isPublicTestMethod(ReflectionMethod $method)
770 return (self::isTestMethod($method) && $method->isPublic());
774 * @param ReflectionMethod $method
777 public static function isTestMethod(ReflectionMethod $method)
779 if (strpos($method->name, 'test') === 0) {
783 // @scenario on TestCase::testMethod()
784 // @test on TestCase::testMethod()
785 return strpos($method->getDocComment(), '@test') !== FALSE ||
786 strpos($method->getDocComment(), '@scenario') !== FALSE;
790 * @param string $message
791 * @return PHPUnit_Framework_Warning
793 protected static function warning($message)
795 return new PHPUnit_Framework_Warning($message);
799 * @param boolean $backupGlobals
800 * @since Method available since Release 3.3.0
802 public function setBackupGlobals($backupGlobals)
804 if (is_null($this->backupGlobals) && is_bool($backupGlobals)) {
805 $this->backupGlobals = $backupGlobals;
810 * Sets the shared fixture for the tests of this test suite.
812 * @param mixed $sharedFixture
813 * @since Method available since Release 3.1.0
815 public function setSharedFixture($sharedFixture)
817 $this->sharedFixture = $sharedFixture;
821 * Returns an iterator for this test suite.
823 * @return RecursiveIteratorIterator
824 * @since Method available since Release 3.1.0
826 public function getIterator()
828 return new RecursiveIteratorIterator(
829 new PHPUnit_Util_TestSuiteIterator($this)
834 * Template Method that is called before the tests
835 * of this test suite are run.
837 * @since Method available since Release 3.1.0
839 protected function setUp()
844 * Template Method that is called after the tests
845 * of this test suite have finished running.
847 * @since Method available since Release 3.1.0
849 protected function tearDown()