PageRenderTime 66ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/phpunit/phpunit/src/Framework/TestSuite.php

https://gitlab.com/jhonn/rest
PHP | 1012 lines | 542 code | 139 blank | 331 comment | 87 complexity | 615d53f21bae8498e2622c00baa582c5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2001-2014, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  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
  18. * distribution.
  19. *
  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.
  23. *
  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.
  36. *
  37. * @package PHPUnit
  38. * @subpackage Framework
  39. * @author Sebastian Bergmann <sebastian@phpunit.de>
  40. * @copyright 2001-2014 Sebastian Bergmann <sebastian@phpunit.de>
  41. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  42. * @link http://www.phpunit.de/
  43. * @since File available since Release 2.0.0
  44. */
  45. /**
  46. * A TestSuite is a composite of Tests. It runs a collection of test cases.
  47. *
  48. * Here is an example using the dynamic test definition.
  49. *
  50. * <code>
  51. * <?php
  52. * $suite = new PHPUnit_Framework_TestSuite;
  53. * $suite->addTest(new MathTest('testPass'));
  54. * ?>
  55. * </code>
  56. *
  57. * Alternatively, a TestSuite can extract the tests to be run automatically.
  58. * To do so you pass a ReflectionClass instance for your
  59. * PHPUnit_Framework_TestCase class to the PHPUnit_Framework_TestSuite
  60. * constructor.
  61. *
  62. * <code>
  63. * <?php
  64. * $suite = new PHPUnit_Framework_TestSuite(
  65. * new ReflectionClass('MathTest')
  66. * );
  67. * ?>
  68. * </code>
  69. *
  70. * This constructor creates a suite with all the methods starting with
  71. * "test" that take no arguments.
  72. *
  73. * @package PHPUnit
  74. * @subpackage Framework
  75. * @author Sebastian Bergmann <sebastian@phpunit.de>
  76. * @copyright 2001-2014 Sebastian Bergmann <sebastian@phpunit.de>
  77. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  78. * @link http://www.phpunit.de/
  79. * @since Class available since Release 2.0.0
  80. */
  81. class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing, IteratorAggregate
  82. {
  83. /**
  84. * Enable or disable the backup and restoration of the $GLOBALS array.
  85. *
  86. * @var boolean
  87. */
  88. protected $backupGlobals = null;
  89. /**
  90. * Enable or disable the backup and restoration of static attributes.
  91. *
  92. * @var boolean
  93. */
  94. protected $backupStaticAttributes = null;
  95. /**
  96. * @var boolean
  97. */
  98. protected $runTestInSeparateProcess = false;
  99. /**
  100. * The name of the test suite.
  101. *
  102. * @var string
  103. */
  104. protected $name = '';
  105. /**
  106. * The test groups of the test suite.
  107. *
  108. * @var array
  109. */
  110. protected $groups = array();
  111. /**
  112. * The tests in the test suite.
  113. *
  114. * @var array
  115. */
  116. protected $tests = array();
  117. /**
  118. * The number of tests in the test suite.
  119. *
  120. * @var integer
  121. */
  122. protected $numTests = -1;
  123. /**
  124. * @var boolean
  125. */
  126. protected $testCase = false;
  127. /**
  128. * @var array
  129. */
  130. protected $foundClasses = array();
  131. /**
  132. * @var PHPUnit_Runner_Filter_Factory
  133. */
  134. private $iteratorFilter = null;
  135. /**
  136. * Constructs a new TestSuite:
  137. *
  138. * - PHPUnit_Framework_TestSuite() constructs an empty TestSuite.
  139. *
  140. * - PHPUnit_Framework_TestSuite(ReflectionClass) constructs a
  141. * TestSuite from the given class.
  142. *
  143. * - PHPUnit_Framework_TestSuite(ReflectionClass, String)
  144. * constructs a TestSuite from the given class with the given
  145. * name.
  146. *
  147. * - PHPUnit_Framework_TestSuite(String) either constructs a
  148. * TestSuite from the given class (if the passed string is the
  149. * name of an existing class) or constructs an empty TestSuite
  150. * with the given name.
  151. *
  152. * @param mixed $theClass
  153. * @param string $name
  154. * @throws PHPUnit_Framework_Exception
  155. */
  156. public function __construct($theClass = '', $name = '')
  157. {
  158. $argumentsValid = false;
  159. if (is_object($theClass) &&
  160. $theClass instanceof ReflectionClass) {
  161. $argumentsValid = true;
  162. } elseif (is_string($theClass) &&
  163. $theClass !== '' &&
  164. class_exists($theClass, false)) {
  165. $argumentsValid = true;
  166. if ($name == '') {
  167. $name = $theClass;
  168. }
  169. $theClass = new ReflectionClass($theClass);
  170. } elseif (is_string($theClass)) {
  171. $this->setName($theClass);
  172. return;
  173. }
  174. if (!$argumentsValid) {
  175. throw new PHPUnit_Framework_Exception;
  176. }
  177. if (!$theClass->isSubclassOf('PHPUnit_Framework_TestCase')) {
  178. throw new PHPUnit_Framework_Exception(
  179. 'Class "' . $theClass->name . '" does not extend PHPUnit_Framework_TestCase.'
  180. );
  181. }
  182. if ($name != '') {
  183. $this->setName($name);
  184. } else {
  185. $this->setName($theClass->getName());
  186. }
  187. $constructor = $theClass->getConstructor();
  188. if ($constructor !== null &&
  189. !$constructor->isPublic()) {
  190. $this->addTest(
  191. self::warning(
  192. sprintf(
  193. 'Class "%s" has no public constructor.',
  194. $theClass->getName()
  195. )
  196. )
  197. );
  198. return;
  199. }
  200. foreach ($theClass->getMethods() as $method) {
  201. $this->addTestMethod($theClass, $method);
  202. }
  203. if (empty($this->tests)) {
  204. $this->addTest(
  205. self::warning(
  206. sprintf(
  207. 'No tests found in class "%s".',
  208. $theClass->getName()
  209. )
  210. )
  211. );
  212. }
  213. $this->testCase = true;
  214. }
  215. /**
  216. * Returns a string representation of the test suite.
  217. *
  218. * @return string
  219. */
  220. public function toString()
  221. {
  222. return $this->getName();
  223. }
  224. /**
  225. * Adds a test to the suite.
  226. *
  227. * @param PHPUnit_Framework_Test $test
  228. * @param array $groups
  229. */
  230. public function addTest(PHPUnit_Framework_Test $test, $groups = array())
  231. {
  232. $class = new ReflectionClass($test);
  233. if (!$class->isAbstract()) {
  234. $this->tests[] = $test;
  235. $this->numTests = -1;
  236. if ($test instanceof PHPUnit_Framework_TestSuite &&
  237. empty($groups)) {
  238. $groups = $test->getGroups();
  239. }
  240. if (empty($groups)) {
  241. $groups = array('__nogroup__');
  242. }
  243. foreach ($groups as $group) {
  244. if (!isset($this->groups[$group])) {
  245. $this->groups[$group] = array($test);
  246. } else {
  247. $this->groups[$group][] = $test;
  248. }
  249. }
  250. }
  251. }
  252. /**
  253. * Adds the tests from the given class to the suite.
  254. *
  255. * @param mixed $testClass
  256. * @throws PHPUnit_Framework_Exception
  257. */
  258. public function addTestSuite($testClass)
  259. {
  260. if (is_string($testClass) && class_exists($testClass)) {
  261. $testClass = new ReflectionClass($testClass);
  262. }
  263. if (!is_object($testClass)) {
  264. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  265. 1,
  266. 'class name or object'
  267. );
  268. }
  269. if ($testClass instanceof PHPUnit_Framework_TestSuite) {
  270. $this->addTest($testClass);
  271. } elseif ($testClass instanceof ReflectionClass) {
  272. $suiteMethod = false;
  273. if (!$testClass->isAbstract()) {
  274. if ($testClass->hasMethod(PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME)) {
  275. $method = $testClass->getMethod(
  276. PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME
  277. );
  278. if ($method->isStatic()) {
  279. $this->addTest(
  280. $method->invoke(null, $testClass->getName())
  281. );
  282. $suiteMethod = true;
  283. }
  284. }
  285. }
  286. if (!$suiteMethod && !$testClass->isAbstract()) {
  287. $this->addTest(new PHPUnit_Framework_TestSuite($testClass));
  288. }
  289. } else {
  290. throw new PHPUnit_Framework_Exception;
  291. }
  292. }
  293. /**
  294. * Wraps both <code>addTest()</code> and <code>addTestSuite</code>
  295. * as well as the separate import statements for the user's convenience.
  296. *
  297. * If the named file cannot be read or there are no new tests that can be
  298. * added, a <code>PHPUnit_Framework_Warning</code> will be created instead,
  299. * leaving the current test run untouched.
  300. *
  301. * @param string $filename
  302. * @param array $phptOptions Array with ini settings for the php instance
  303. * run, key being the name if the setting,
  304. * value the ini value.
  305. * @throws PHPUnit_Framework_Exception
  306. * @since Method available since Release 2.3.0
  307. * @author Stefano F. Rausch <stefano@rausch-e.net>
  308. */
  309. public function addTestFile($filename, $phptOptions = array())
  310. {
  311. if (!is_string($filename)) {
  312. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  313. }
  314. if (file_exists($filename) && substr($filename, -5) == '.phpt') {
  315. $this->addTest(
  316. new PHPUnit_Extensions_PhptTestCase($filename, $phptOptions)
  317. );
  318. return;
  319. }
  320. // The given file may contain further stub classes in addition to the
  321. // test class itself. Figure out the actual test class.
  322. $classes = get_declared_classes();
  323. $filename = PHPUnit_Util_Fileloader::checkAndLoad($filename);
  324. $newClasses = array_diff(get_declared_classes(), $classes);
  325. // The diff is empty in case a parent class (with test methods) is added
  326. // AFTER a child class that inherited from it. To account for that case,
  327. // cumulate all discovered classes, so the parent class may be found in
  328. // a later invocation.
  329. if ($newClasses) {
  330. // On the assumption that test classes are defined first in files,
  331. // process discovered classes in approximate LIFO order, so as to
  332. // avoid unnecessary reflection.
  333. $this->foundClasses = array_merge($newClasses, $this->foundClasses);
  334. }
  335. // The test class's name must match the filename, either in full, or as
  336. // a PEAR/PSR-0 prefixed shortname ('NameSpace_ShortName'), or as a
  337. // PSR-1 local shortname ('NameSpace\ShortName'). The comparison must be
  338. // anchored to prevent false-positive matches (e.g., 'OtherShortName').
  339. $shortname = basename($filename, '.php');
  340. $shortnameRegEx = '/(?:^|_|\\\\)' . preg_quote($shortname, '/') . '$/';
  341. foreach ($this->foundClasses as $i => $className) {
  342. if (preg_match($shortnameRegEx, $className)) {
  343. $class = new ReflectionClass($className);
  344. if ($class->getFileName() == $filename) {
  345. $newClasses = array($className);
  346. unset($this->foundClasses[$i]);
  347. break;
  348. }
  349. }
  350. }
  351. foreach ($newClasses as $className) {
  352. $class = new ReflectionClass($className);
  353. if (!$class->isAbstract()) {
  354. if ($class->hasMethod(PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME)) {
  355. $method = $class->getMethod(
  356. PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME
  357. );
  358. if ($method->isStatic()) {
  359. $this->addTest($method->invoke(null, $className));
  360. }
  361. } elseif ($class->implementsInterface('PHPUnit_Framework_Test')) {
  362. $this->addTestSuite($class);
  363. }
  364. }
  365. }
  366. $this->numTests = -1;
  367. }
  368. /**
  369. * Wrapper for addTestFile() that adds multiple test files.
  370. *
  371. * @param array|Iterator $filenames
  372. * @throws PHPUnit_Framework_Exception
  373. * @since Method available since Release 2.3.0
  374. */
  375. public function addTestFiles($filenames)
  376. {
  377. if (!(is_array($filenames) ||
  378. (is_object($filenames) && $filenames instanceof Iterator))) {
  379. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  380. 1,
  381. 'array or iterator'
  382. );
  383. }
  384. foreach ($filenames as $filename) {
  385. $this->addTestFile((string) $filename);
  386. }
  387. }
  388. /**
  389. * Counts the number of test cases that will be run by this test.
  390. *
  391. * @return integer
  392. */
  393. public function count()
  394. {
  395. $numTests = 0;
  396. foreach ($this as $test) {
  397. $numTests += count($test);
  398. }
  399. return $numTests;
  400. }
  401. /**
  402. * @param ReflectionClass $theClass
  403. * @param string $name
  404. * @return PHPUnit_Framework_Test
  405. * @throws PHPUnit_Framework_Exception
  406. */
  407. public static function createTest(ReflectionClass $theClass, $name)
  408. {
  409. $className = $theClass->getName();
  410. if (!$theClass->isInstantiable()) {
  411. return self::warning(
  412. sprintf('Cannot instantiate class "%s".', $className)
  413. );
  414. }
  415. $backupSettings = PHPUnit_Util_Test::getBackupSettings(
  416. $className,
  417. $name
  418. );
  419. $preserveGlobalState = PHPUnit_Util_Test::getPreserveGlobalStateSettings(
  420. $className,
  421. $name
  422. );
  423. $runTestInSeparateProcess = PHPUnit_Util_Test::getProcessIsolationSettings(
  424. $className,
  425. $name
  426. );
  427. $constructor = $theClass->getConstructor();
  428. if ($constructor !== null) {
  429. $parameters = $constructor->getParameters();
  430. // TestCase() or TestCase($name)
  431. if (count($parameters) < 2) {
  432. $test = new $className;
  433. } // TestCase($name, $data)
  434. else {
  435. try {
  436. $data = PHPUnit_Util_Test::getProvidedData(
  437. $className,
  438. $name
  439. );
  440. } catch (Exception $e) {
  441. if (!($e instanceof PHPUnit_Framework_SkippedTestError || $e instanceof PHPUnit_Framework_IncompleteTestError)) {
  442. $message = sprintf(
  443. 'The data provider specified for %s::%s is invalid.',
  444. $className,
  445. $name
  446. );
  447. $_message = $e->getMessage();
  448. if (!empty($_message)) {
  449. $message .= "\n" . $_message;
  450. }
  451. $data = self::warning($message);
  452. } elseif ($e instanceof PHPUnit_Framework_SkippedTestError) {
  453. $message = sprintf(
  454. 'Test for %s::%s skipped by data provider',
  455. $className,
  456. $name
  457. );
  458. $_message = $e->getMessage();
  459. if (!empty($_message)) {
  460. $message .= "\n" . $_message;
  461. }
  462. $data = self::skipTest($className, $name, $message);
  463. } elseif ($e instanceof PHPUnit_Framework_IncompleteTestError) {
  464. $message = sprintf(
  465. 'Test for %s::%s marked incomplete by data provider',
  466. $className,
  467. $name
  468. );
  469. $_message = $e->getMessage();
  470. if (!empty($_message)) {
  471. $message .= "\n" . $_message;
  472. }
  473. $data = self::incompleteTest($className, $name, $message);
  474. }
  475. }
  476. // Test method with @dataProvider.
  477. if (isset($data)) {
  478. $test = new PHPUnit_Framework_TestSuite_DataProvider(
  479. $className . '::' . $name
  480. );
  481. if (empty($data)) {
  482. $data = self::warning(
  483. sprintf(
  484. 'No tests found in suite "%s".',
  485. $test->getName()
  486. )
  487. );
  488. }
  489. $groups = PHPUnit_Util_Test::getGroups($className, $name);
  490. if ($data instanceof PHPUnit_Framework_Warning ||
  491. $data instanceof PHPUnit_Framework_SkippedTestCase ||
  492. $data instanceof PHPUnit_Framework_IncompleteTestCase) {
  493. $test->addTest($data, $groups);
  494. } else {
  495. foreach ($data as $_dataName => $_data) {
  496. $_test = new $className($name, $_data, $_dataName);
  497. if ($runTestInSeparateProcess) {
  498. $_test->setRunTestInSeparateProcess(true);
  499. if ($preserveGlobalState !== null) {
  500. $_test->setPreserveGlobalState($preserveGlobalState);
  501. }
  502. }
  503. if ($backupSettings['backupGlobals'] !== null) {
  504. $_test->setBackupGlobals(
  505. $backupSettings['backupGlobals']
  506. );
  507. }
  508. if ($backupSettings['backupStaticAttributes'] !== null) {
  509. $_test->setBackupStaticAttributes(
  510. $backupSettings['backupStaticAttributes']
  511. );
  512. }
  513. $test->addTest($_test, $groups);
  514. }
  515. }
  516. } else {
  517. $test = new $className;
  518. }
  519. }
  520. }
  521. if (!isset($test)) {
  522. throw new PHPUnit_Framework_Exception('No valid test provided.');
  523. }
  524. if ($test instanceof PHPUnit_Framework_TestCase) {
  525. $test->setName($name);
  526. if ($runTestInSeparateProcess) {
  527. $test->setRunTestInSeparateProcess(true);
  528. if ($preserveGlobalState !== null) {
  529. $test->setPreserveGlobalState($preserveGlobalState);
  530. }
  531. }
  532. if ($backupSettings['backupGlobals'] !== null) {
  533. $test->setBackupGlobals($backupSettings['backupGlobals']);
  534. }
  535. if ($backupSettings['backupStaticAttributes'] !== null) {
  536. $test->setBackupStaticAttributes(
  537. $backupSettings['backupStaticAttributes']
  538. );
  539. }
  540. }
  541. return $test;
  542. }
  543. /**
  544. * Creates a default TestResult object.
  545. *
  546. * @return PHPUnit_Framework_TestResult
  547. */
  548. protected function createResult()
  549. {
  550. return new PHPUnit_Framework_TestResult;
  551. }
  552. /**
  553. * Returns the name of the suite.
  554. *
  555. * @return string
  556. */
  557. public function getName()
  558. {
  559. return $this->name;
  560. }
  561. /**
  562. * Returns the test groups of the suite.
  563. *
  564. * @return array
  565. * @since Method available since Release 3.2.0
  566. */
  567. public function getGroups()
  568. {
  569. return array_keys($this->groups);
  570. }
  571. public function getGroupDetails()
  572. {
  573. return $this->groups;
  574. }
  575. /**
  576. * Set tests groups of the test case
  577. *
  578. * @param array $groups
  579. * @since Method available since Release 4.0.0
  580. */
  581. public function setGroupDetails(array $groups)
  582. {
  583. $this->groups = $groups;
  584. }
  585. /**
  586. * Runs the tests and collects their result in a TestResult.
  587. *
  588. * @param PHPUnit_Framework_TestResult $result
  589. * @return PHPUnit_Framework_TestResult
  590. */
  591. public function run(PHPUnit_Framework_TestResult $result = null)
  592. {
  593. if ($result === null) {
  594. $result = $this->createResult();
  595. }
  596. if (count($this) == 0) {
  597. return $result;
  598. }
  599. $hookMethods = PHPUnit_Util_Test::getHookMethods($this->name);
  600. $result->startTestSuite($this);
  601. try {
  602. $this->setUp();
  603. foreach ($hookMethods['beforeClass'] as $beforeClassMethod) {
  604. if ($this->testCase === true &&
  605. class_exists($this->name, false) &&
  606. method_exists($this->name, $beforeClassMethod)) {
  607. if ($missingRequirements = PHPUnit_Util_Test::getMissingRequirements($this->name, $beforeClassMethod)) {
  608. $this->markTestSuiteSkipped(implode(PHP_EOL, $missingRequirements));
  609. }
  610. call_user_func(array($this->name, $beforeClassMethod));
  611. }
  612. }
  613. } catch (PHPUnit_Framework_SkippedTestSuiteError $e) {
  614. $numTests = count($this);
  615. for ($i = 0; $i < $numTests; $i++) {
  616. $result->startTest($this);
  617. $result->addFailure($this, $e, 0);
  618. $result->endTest($this, 0);
  619. }
  620. $this->tearDown();
  621. $result->endTestSuite($this);
  622. return $result;
  623. } catch (Exception $e) {
  624. $numTests = count($this);
  625. for ($i = 0; $i < $numTests; $i++) {
  626. $result->startTest($this);
  627. $result->addError($this, $e, 0);
  628. $result->endTest($this, 0);
  629. }
  630. $this->tearDown();
  631. $result->endTestSuite($this);
  632. return $result;
  633. }
  634. foreach ($this as $test) {
  635. if ($result->shouldStop()) {
  636. break;
  637. }
  638. if ($test instanceof PHPUnit_Framework_TestCase ||
  639. $test instanceof PHPUnit_Framework_TestSuite) {
  640. $test->setBackupGlobals($this->backupGlobals);
  641. $test->setBackupStaticAttributes($this->backupStaticAttributes);
  642. $test->setRunTestInSeparateProcess($this->runTestInSeparateProcess);
  643. }
  644. $test->run($result);
  645. }
  646. foreach ($hookMethods['afterClass'] as $afterClassMethod) {
  647. if ($this->testCase === true && class_exists($this->name, false) && method_exists($this->name, $afterClassMethod)) {
  648. call_user_func(array($this->name, $afterClassMethod));
  649. }
  650. }
  651. $this->tearDown();
  652. $result->endTestSuite($this);
  653. return $result;
  654. }
  655. /**
  656. * @param boolean $runTestInSeparateProcess
  657. * @throws PHPUnit_Framework_Exception
  658. * @since Method available since Release 3.7.0
  659. */
  660. public function setRunTestInSeparateProcess($runTestInSeparateProcess)
  661. {
  662. if (is_bool($runTestInSeparateProcess)) {
  663. $this->runTestInSeparateProcess = $runTestInSeparateProcess;
  664. } else {
  665. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  666. }
  667. }
  668. /**
  669. * Runs a test.
  670. *
  671. * @deprecated
  672. * @param PHPUnit_Framework_Test $test
  673. * @param PHPUnit_Framework_TestResult $result
  674. */
  675. public function runTest(PHPUnit_Framework_Test $test, PHPUnit_Framework_TestResult $result)
  676. {
  677. $test->run($result);
  678. }
  679. /**
  680. * Sets the name of the suite.
  681. *
  682. * @param string
  683. */
  684. public function setName($name)
  685. {
  686. $this->name = $name;
  687. }
  688. /**
  689. * Returns the test at the given index.
  690. *
  691. * @param integer
  692. * @return PHPUnit_Framework_Test
  693. */
  694. public function testAt($index)
  695. {
  696. if (isset($this->tests[$index])) {
  697. return $this->tests[$index];
  698. } else {
  699. return false;
  700. }
  701. }
  702. /**
  703. * Returns the tests as an enumeration.
  704. *
  705. * @return array
  706. */
  707. public function tests()
  708. {
  709. return $this->tests;
  710. }
  711. /**
  712. * Set tests of the test suite
  713. *
  714. * @param array $tests
  715. * @since Method available since Release 4.0.0
  716. */
  717. public function setTests(array $tests)
  718. {
  719. $this->tests = $tests;
  720. }
  721. /**
  722. * Mark the test suite as skipped.
  723. *
  724. * @param string $message
  725. * @throws PHPUnit_Framework_SkippedTestSuiteError
  726. * @since Method available since Release 3.0.0
  727. */
  728. public function markTestSuiteSkipped($message = '')
  729. {
  730. throw new PHPUnit_Framework_SkippedTestSuiteError($message);
  731. }
  732. /**
  733. * @param ReflectionClass $class
  734. * @param ReflectionMethod $method
  735. */
  736. protected function addTestMethod(ReflectionClass $class, ReflectionMethod $method)
  737. {
  738. if (!$this->isTestMethod($method)) {
  739. return;
  740. }
  741. $name = $method->getName();
  742. if (!$method->isPublic()) {
  743. $this->addTest(
  744. self::warning(
  745. sprintf(
  746. 'Test method "%s" in test class "%s" is not public.',
  747. $name,
  748. $class->getName()
  749. )
  750. )
  751. );
  752. return;
  753. }
  754. $test = self::createTest($class, $name);
  755. if ($test instanceof PHPUnit_Framework_TestCase ||
  756. $test instanceof PHPUnit_Framework_TestSuite_DataProvider) {
  757. $test->setDependencies(
  758. PHPUnit_Util_Test::getDependencies($class->getName(), $name)
  759. );
  760. }
  761. $this->addTest(
  762. $test,
  763. PHPUnit_Util_Test::getGroups($class->getName(), $name)
  764. );
  765. }
  766. /**
  767. * @param ReflectionMethod $method
  768. * @return boolean
  769. */
  770. public static function isTestMethod(ReflectionMethod $method)
  771. {
  772. if (strpos($method->name, 'test') === 0) {
  773. return true;
  774. }
  775. // @scenario on TestCase::testMethod()
  776. // @test on TestCase::testMethod()
  777. $doc_comment = $method->getDocComment();
  778. return strpos($doc_comment, '@test') !== false ||
  779. strpos($doc_comment, '@scenario') !== false;
  780. }
  781. /**
  782. * @param string $message
  783. * @return PHPUnit_Framework_Warning
  784. */
  785. protected static function warning($message)
  786. {
  787. return new PHPUnit_Framework_Warning($message);
  788. }
  789. /**
  790. * @param string $class
  791. * @param string $methodName
  792. * @param string $message
  793. * @return PHPUnit_Framework_SkippedTestCase
  794. * @since Method available since Release 4.3.0
  795. */
  796. protected static function skipTest($class, $methodName, $message)
  797. {
  798. return new PHPUnit_Framework_SkippedTestCase($class, $methodName, $message);
  799. }
  800. /**
  801. * @param string $class
  802. * @param string $methodName
  803. * @param string $message
  804. * @return PHPUnit_Framework_IncompleteTestCase
  805. * @since Method available since Release 4.3.0
  806. */
  807. protected static function incompleteTest($class, $methodName, $message)
  808. {
  809. return new PHPUnit_Framework_IncompleteTestCase($class, $methodName, $message);
  810. }
  811. /**
  812. * @param boolean $backupGlobals
  813. * @since Method available since Release 3.3.0
  814. */
  815. public function setBackupGlobals($backupGlobals)
  816. {
  817. if (is_null($this->backupGlobals) && is_bool($backupGlobals)) {
  818. $this->backupGlobals = $backupGlobals;
  819. }
  820. }
  821. /**
  822. * @param boolean $backupStaticAttributes
  823. * @since Method available since Release 3.4.0
  824. */
  825. public function setBackupStaticAttributes($backupStaticAttributes)
  826. {
  827. if (is_null($this->backupStaticAttributes) &&
  828. is_bool($backupStaticAttributes)) {
  829. $this->backupStaticAttributes = $backupStaticAttributes;
  830. }
  831. }
  832. /**
  833. * Returns an iterator for this test suite.
  834. *
  835. * @return RecursiveIteratorIterator
  836. * @since Method available since Release 3.1.0
  837. */
  838. public function getIterator()
  839. {
  840. $iterator = new PHPUnit_Util_TestSuiteIterator($this);
  841. if ($this->iteratorFilter !== null) {
  842. $iterator = $this->iteratorFilter->factory($iterator, $this);
  843. }
  844. return $iterator;
  845. }
  846. public function injectFilter(PHPUnit_Runner_Filter_Factory $filter)
  847. {
  848. $this->iteratorFilter = $filter;
  849. foreach ($this as $test) {
  850. if ($test instanceof PHPUnit_Framework_TestSuite) {
  851. $test->injectFilter($filter);
  852. }
  853. }
  854. }
  855. /**
  856. * Template Method that is called before the tests
  857. * of this test suite are run.
  858. *
  859. * @since Method available since Release 3.1.0
  860. */
  861. protected function setUp()
  862. {
  863. }
  864. /**
  865. * Template Method that is called after the tests
  866. * of this test suite have finished running.
  867. *
  868. * @since Method available since Release 3.1.0
  869. */
  870. protected function tearDown()
  871. {
  872. }
  873. }