/vendor/phpunit/phpunit/src/Framework/TestResult.php

https://gitlab.com/ealexis.t/trends · PHP · 1128 lines · 556 code · 139 blank · 433 comment · 69 complexity · f01f5872e082455edf98ba9bdb5c131b MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of PHPUnit.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * A TestResult collects the results of executing a test case.
  12. *
  13. * @since Class available since Release 2.0.0
  14. */
  15. class PHPUnit_Framework_TestResult implements Countable
  16. {
  17. /**
  18. * @var array
  19. */
  20. protected $passed = array();
  21. /**
  22. * @var array
  23. */
  24. protected $errors = array();
  25. /**
  26. * @var array
  27. */
  28. protected $failures = array();
  29. /**
  30. * @var array
  31. */
  32. protected $notImplemented = array();
  33. /**
  34. * @var array
  35. */
  36. protected $risky = array();
  37. /**
  38. * @var array
  39. */
  40. protected $skipped = array();
  41. /**
  42. * @var array
  43. */
  44. protected $listeners = array();
  45. /**
  46. * @var int
  47. */
  48. protected $runTests = 0;
  49. /**
  50. * @var float
  51. */
  52. protected $time = 0;
  53. /**
  54. * @var PHPUnit_Framework_TestSuite
  55. */
  56. protected $topTestSuite = null;
  57. /**
  58. * Code Coverage information.
  59. *
  60. * @var PHP_CodeCoverage
  61. */
  62. protected $codeCoverage;
  63. /**
  64. * @var bool
  65. */
  66. protected $convertErrorsToExceptions = true;
  67. /**
  68. * @var bool
  69. */
  70. protected $stop = false;
  71. /**
  72. * @var bool
  73. */
  74. protected $stopOnError = false;
  75. /**
  76. * @var bool
  77. */
  78. protected $stopOnFailure = false;
  79. /**
  80. * @var bool
  81. */
  82. protected $beStrictAboutTestsThatDoNotTestAnything = false;
  83. /**
  84. * @var bool
  85. */
  86. protected $beStrictAboutOutputDuringTests = false;
  87. /**
  88. * @var bool
  89. */
  90. protected $beStrictAboutTestSize = false;
  91. /**
  92. * @var bool
  93. */
  94. protected $beStrictAboutTodoAnnotatedTests = false;
  95. /**
  96. * @var bool
  97. */
  98. protected $stopOnRisky = false;
  99. /**
  100. * @var bool
  101. */
  102. protected $stopOnIncomplete = false;
  103. /**
  104. * @var bool
  105. */
  106. protected $stopOnSkipped = false;
  107. /**
  108. * @var bool
  109. */
  110. protected $lastTestFailed = false;
  111. /**
  112. * @var int
  113. */
  114. protected $timeoutForSmallTests = 1;
  115. /**
  116. * @var int
  117. */
  118. protected $timeoutForMediumTests = 10;
  119. /**
  120. * @var int
  121. */
  122. protected $timeoutForLargeTests = 60;
  123. /**
  124. * Registers a TestListener.
  125. *
  126. * @param PHPUnit_Framework_TestListener
  127. */
  128. public function addListener(PHPUnit_Framework_TestListener $listener)
  129. {
  130. $this->listeners[] = $listener;
  131. }
  132. /**
  133. * Unregisters a TestListener.
  134. *
  135. * @param PHPUnit_Framework_TestListener $listener
  136. */
  137. public function removeListener(PHPUnit_Framework_TestListener $listener)
  138. {
  139. foreach ($this->listeners as $key => $_listener) {
  140. if ($listener === $_listener) {
  141. unset($this->listeners[$key]);
  142. }
  143. }
  144. }
  145. /**
  146. * Flushes all flushable TestListeners.
  147. *
  148. * @since Method available since Release 3.0.0
  149. */
  150. public function flushListeners()
  151. {
  152. foreach ($this->listeners as $listener) {
  153. if ($listener instanceof PHPUnit_Util_Printer) {
  154. $listener->flush();
  155. }
  156. }
  157. }
  158. /**
  159. * Adds an error to the list of errors.
  160. *
  161. * @param PHPUnit_Framework_Test $test
  162. * @param Exception $e
  163. * @param float $time
  164. */
  165. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
  166. {
  167. if ($e instanceof PHPUnit_Framework_RiskyTest) {
  168. $this->risky[] = new PHPUnit_Framework_TestFailure($test, $e);
  169. $notifyMethod = 'addRiskyTest';
  170. if ($this->stopOnRisky) {
  171. $this->stop();
  172. }
  173. } elseif ($e instanceof PHPUnit_Framework_IncompleteTest) {
  174. $this->notImplemented[] = new PHPUnit_Framework_TestFailure($test, $e);
  175. $notifyMethod = 'addIncompleteTest';
  176. if ($this->stopOnIncomplete) {
  177. $this->stop();
  178. }
  179. } elseif ($e instanceof PHPUnit_Framework_SkippedTest) {
  180. $this->skipped[] = new PHPUnit_Framework_TestFailure($test, $e);
  181. $notifyMethod = 'addSkippedTest';
  182. if ($this->stopOnSkipped) {
  183. $this->stop();
  184. }
  185. } else {
  186. $this->errors[] = new PHPUnit_Framework_TestFailure($test, $e);
  187. $notifyMethod = 'addError';
  188. if ($this->stopOnError || $this->stopOnFailure) {
  189. $this->stop();
  190. }
  191. }
  192. foreach ($this->listeners as $listener) {
  193. $listener->$notifyMethod($test, $e, $time);
  194. }
  195. $this->lastTestFailed = true;
  196. $this->time += $time;
  197. }
  198. /**
  199. * Adds a failure to the list of failures.
  200. * The passed in exception caused the failure.
  201. *
  202. * @param PHPUnit_Framework_Test $test
  203. * @param PHPUnit_Framework_AssertionFailedError $e
  204. * @param float $time
  205. */
  206. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
  207. {
  208. if ($e instanceof PHPUnit_Framework_RiskyTest ||
  209. $e instanceof PHPUnit_Framework_OutputError) {
  210. $this->risky[] = new PHPUnit_Framework_TestFailure($test, $e);
  211. $notifyMethod = 'addRiskyTest';
  212. if ($this->stopOnRisky) {
  213. $this->stop();
  214. }
  215. } elseif ($e instanceof PHPUnit_Framework_IncompleteTest) {
  216. $this->notImplemented[] = new PHPUnit_Framework_TestFailure($test, $e);
  217. $notifyMethod = 'addIncompleteTest';
  218. if ($this->stopOnIncomplete) {
  219. $this->stop();
  220. }
  221. } elseif ($e instanceof PHPUnit_Framework_SkippedTest) {
  222. $this->skipped[] = new PHPUnit_Framework_TestFailure($test, $e);
  223. $notifyMethod = 'addSkippedTest';
  224. if ($this->stopOnSkipped) {
  225. $this->stop();
  226. }
  227. } else {
  228. $this->failures[] = new PHPUnit_Framework_TestFailure($test, $e);
  229. $notifyMethod = 'addFailure';
  230. if ($this->stopOnFailure) {
  231. $this->stop();
  232. }
  233. }
  234. foreach ($this->listeners as $listener) {
  235. $listener->$notifyMethod($test, $e, $time);
  236. }
  237. $this->lastTestFailed = true;
  238. $this->time += $time;
  239. }
  240. /**
  241. * Informs the result that a testsuite will be started.
  242. *
  243. * @param PHPUnit_Framework_TestSuite $suite
  244. *
  245. * @since Method available since Release 2.2.0
  246. */
  247. public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
  248. {
  249. if ($this->topTestSuite === null) {
  250. $this->topTestSuite = $suite;
  251. }
  252. foreach ($this->listeners as $listener) {
  253. $listener->startTestSuite($suite);
  254. }
  255. }
  256. /**
  257. * Informs the result that a testsuite was completed.
  258. *
  259. * @param PHPUnit_Framework_TestSuite $suite
  260. *
  261. * @since Method available since Release 2.2.0
  262. */
  263. public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
  264. {
  265. foreach ($this->listeners as $listener) {
  266. $listener->endTestSuite($suite);
  267. }
  268. }
  269. /**
  270. * Informs the result that a test will be started.
  271. *
  272. * @param PHPUnit_Framework_Test $test
  273. */
  274. public function startTest(PHPUnit_Framework_Test $test)
  275. {
  276. $this->lastTestFailed = false;
  277. $this->runTests += count($test);
  278. foreach ($this->listeners as $listener) {
  279. $listener->startTest($test);
  280. }
  281. }
  282. /**
  283. * Informs the result that a test was completed.
  284. *
  285. * @param PHPUnit_Framework_Test $test
  286. * @param float $time
  287. */
  288. public function endTest(PHPUnit_Framework_Test $test, $time)
  289. {
  290. foreach ($this->listeners as $listener) {
  291. $listener->endTest($test, $time);
  292. }
  293. if (!$this->lastTestFailed && $test instanceof PHPUnit_Framework_TestCase) {
  294. $class = get_class($test);
  295. $key = $class . '::' . $test->getName();
  296. $this->passed[$key] = array(
  297. 'result' => $test->getResult(),
  298. 'size' => PHPUnit_Util_Test::getSize(
  299. $class,
  300. $test->getName(false)
  301. )
  302. );
  303. $this->time += $time;
  304. }
  305. }
  306. /**
  307. * Returns true if no risky test occurred.
  308. *
  309. * @return bool
  310. *
  311. * @since Method available since Release 4.0.0
  312. */
  313. public function allHarmless()
  314. {
  315. return $this->riskyCount() == 0;
  316. }
  317. /**
  318. * Gets the number of risky tests.
  319. *
  320. * @return int
  321. *
  322. * @since Method available since Release 4.0.0
  323. */
  324. public function riskyCount()
  325. {
  326. return count($this->risky);
  327. }
  328. /**
  329. * Returns true if no incomplete test occurred.
  330. *
  331. * @return bool
  332. */
  333. public function allCompletelyImplemented()
  334. {
  335. return $this->notImplementedCount() == 0;
  336. }
  337. /**
  338. * Gets the number of incomplete tests.
  339. *
  340. * @return int
  341. */
  342. public function notImplementedCount()
  343. {
  344. return count($this->notImplemented);
  345. }
  346. /**
  347. * Returns an Enumeration for the risky tests.
  348. *
  349. * @return array
  350. *
  351. * @since Method available since Release 4.0.0
  352. */
  353. public function risky()
  354. {
  355. return $this->risky;
  356. }
  357. /**
  358. * Returns an Enumeration for the incomplete tests.
  359. *
  360. * @return array
  361. */
  362. public function notImplemented()
  363. {
  364. return $this->notImplemented;
  365. }
  366. /**
  367. * Returns true if no test has been skipped.
  368. *
  369. * @return bool
  370. *
  371. * @since Method available since Release 3.0.0
  372. */
  373. public function noneSkipped()
  374. {
  375. return $this->skippedCount() == 0;
  376. }
  377. /**
  378. * Gets the number of skipped tests.
  379. *
  380. * @return int
  381. *
  382. * @since Method available since Release 3.0.0
  383. */
  384. public function skippedCount()
  385. {
  386. return count($this->skipped);
  387. }
  388. /**
  389. * Returns an Enumeration for the skipped tests.
  390. *
  391. * @return array
  392. *
  393. * @since Method available since Release 3.0.0
  394. */
  395. public function skipped()
  396. {
  397. return $this->skipped;
  398. }
  399. /**
  400. * Gets the number of detected errors.
  401. *
  402. * @return int
  403. */
  404. public function errorCount()
  405. {
  406. return count($this->errors);
  407. }
  408. /**
  409. * Returns an Enumeration for the errors.
  410. *
  411. * @return array
  412. */
  413. public function errors()
  414. {
  415. return $this->errors;
  416. }
  417. /**
  418. * Gets the number of detected failures.
  419. *
  420. * @return int
  421. */
  422. public function failureCount()
  423. {
  424. return count($this->failures);
  425. }
  426. /**
  427. * Returns an Enumeration for the failures.
  428. *
  429. * @return array
  430. */
  431. public function failures()
  432. {
  433. return $this->failures;
  434. }
  435. /**
  436. * Returns the names of the tests that have passed.
  437. *
  438. * @return array
  439. *
  440. * @since Method available since Release 3.4.0
  441. */
  442. public function passed()
  443. {
  444. return $this->passed;
  445. }
  446. /**
  447. * Returns the (top) test suite.
  448. *
  449. * @return PHPUnit_Framework_TestSuite
  450. *
  451. * @since Method available since Release 3.0.0
  452. */
  453. public function topTestSuite()
  454. {
  455. return $this->topTestSuite;
  456. }
  457. /**
  458. * Returns whether code coverage information should be collected.
  459. *
  460. * @return bool If code coverage should be collected
  461. *
  462. * @since Method available since Release 3.2.0
  463. */
  464. public function getCollectCodeCoverageInformation()
  465. {
  466. return $this->codeCoverage !== null;
  467. }
  468. /**
  469. * Runs a TestCase.
  470. *
  471. * @param PHPUnit_Framework_Test $test
  472. */
  473. public function run(PHPUnit_Framework_Test $test)
  474. {
  475. PHPUnit_Framework_Assert::resetCount();
  476. $error = false;
  477. $failure = false;
  478. $incomplete = false;
  479. $risky = false;
  480. $skipped = false;
  481. $this->startTest($test);
  482. $errorHandlerSet = false;
  483. if ($this->convertErrorsToExceptions) {
  484. $oldErrorHandler = set_error_handler(
  485. array('PHPUnit_Util_ErrorHandler', 'handleError'),
  486. E_ALL | E_STRICT
  487. );
  488. if ($oldErrorHandler === null) {
  489. $errorHandlerSet = true;
  490. } else {
  491. restore_error_handler();
  492. }
  493. }
  494. $collectCodeCoverage = $this->codeCoverage !== null &&
  495. !$test instanceof PHPUnit_Extensions_SeleniumTestCase &&
  496. !$test instanceof PHPUnit_Framework_Warning;
  497. if ($collectCodeCoverage) {
  498. // We need to blacklist test source files when no whitelist is used.
  499. if (!$this->codeCoverage->filter()->hasWhitelist()) {
  500. $classes = $this->getHierarchy(get_class($test), true);
  501. foreach ($classes as $class) {
  502. $this->codeCoverage->filter()->addFileToBlacklist(
  503. $class->getFileName()
  504. );
  505. }
  506. }
  507. $this->codeCoverage->start($test);
  508. }
  509. PHP_Timer::start();
  510. try {
  511. if (!$test instanceof PHPUnit_Framework_Warning &&
  512. $test->getSize() != PHPUnit_Util_Test::UNKNOWN &&
  513. $this->beStrictAboutTestSize &&
  514. extension_loaded('pcntl') && class_exists('PHP_Invoker')) {
  515. switch ($test->getSize()) {
  516. case PHPUnit_Util_Test::SMALL:
  517. $_timeout = $this->timeoutForSmallTests;
  518. break;
  519. case PHPUnit_Util_Test::MEDIUM:
  520. $_timeout = $this->timeoutForMediumTests;
  521. break;
  522. case PHPUnit_Util_Test::LARGE:
  523. $_timeout = $this->timeoutForLargeTests;
  524. break;
  525. }
  526. $invoker = new PHP_Invoker;
  527. $invoker->invoke(array($test, 'runBare'), array(), $_timeout);
  528. } else {
  529. $test->runBare();
  530. }
  531. } catch (PHPUnit_Framework_AssertionFailedError $e) {
  532. $failure = true;
  533. if ($e instanceof PHPUnit_Framework_RiskyTestError) {
  534. $risky = true;
  535. } elseif ($e instanceof PHPUnit_Framework_IncompleteTestError) {
  536. $incomplete = true;
  537. } elseif ($e instanceof PHPUnit_Framework_SkippedTestError) {
  538. $skipped = true;
  539. }
  540. } catch (PHPUnit_Framework_Exception $e) {
  541. $error = true;
  542. } catch (Throwable $e) {
  543. $e = new PHPUnit_Framework_ExceptionWrapper($e);
  544. $error = true;
  545. } catch (Exception $e) {
  546. $e = new PHPUnit_Framework_ExceptionWrapper($e);
  547. $error = true;
  548. }
  549. $time = PHP_Timer::stop();
  550. $test->addToAssertionCount(PHPUnit_Framework_Assert::getCount());
  551. if ($this->beStrictAboutTestsThatDoNotTestAnything &&
  552. $test->getNumAssertions() == 0) {
  553. $risky = true;
  554. }
  555. if ($collectCodeCoverage) {
  556. $append = !$risky && !$incomplete && !$skipped;
  557. $linesToBeCovered = array();
  558. $linesToBeUsed = array();
  559. if ($append && $test instanceof PHPUnit_Framework_TestCase) {
  560. $linesToBeCovered = PHPUnit_Util_Test::getLinesToBeCovered(
  561. get_class($test),
  562. $test->getName(false)
  563. );
  564. $linesToBeUsed = PHPUnit_Util_Test::getLinesToBeUsed(
  565. get_class($test),
  566. $test->getName(false)
  567. );
  568. }
  569. try {
  570. $this->codeCoverage->stop(
  571. $append,
  572. $linesToBeCovered,
  573. $linesToBeUsed
  574. );
  575. } catch (PHP_CodeCoverage_Exception_UnintentionallyCoveredCode $cce) {
  576. $this->addFailure(
  577. $test,
  578. new PHPUnit_Framework_UnintentionallyCoveredCodeError(
  579. 'This test executed code that is not listed as code to be covered or used:' .
  580. PHP_EOL . $cce->getMessage()
  581. ),
  582. $time
  583. );
  584. } catch (PHPUnit_Framework_InvalidCoversTargetException $cce) {
  585. $this->addFailure(
  586. $test,
  587. new PHPUnit_Framework_InvalidCoversTargetError(
  588. $cce->getMessage()
  589. ),
  590. $time
  591. );
  592. } catch (PHP_CodeCoverage_Exception $cce) {
  593. $error = true;
  594. if (!isset($e)) {
  595. $e = $cce;
  596. }
  597. }
  598. }
  599. if ($errorHandlerSet === true) {
  600. restore_error_handler();
  601. }
  602. if ($error === true) {
  603. $this->addError($test, $e, $time);
  604. } elseif ($failure === true) {
  605. $this->addFailure($test, $e, $time);
  606. } elseif ($this->beStrictAboutTestsThatDoNotTestAnything &&
  607. $test->getNumAssertions() == 0) {
  608. $this->addFailure(
  609. $test,
  610. new PHPUnit_Framework_RiskyTestError(
  611. 'This test did not perform any assertions'
  612. ),
  613. $time
  614. );
  615. } elseif ($this->beStrictAboutOutputDuringTests && $test->hasOutput()) {
  616. $this->addFailure(
  617. $test,
  618. new PHPUnit_Framework_OutputError(
  619. sprintf(
  620. 'This test printed output: %s',
  621. $test->getActualOutput()
  622. )
  623. ),
  624. $time
  625. );
  626. } elseif ($this->beStrictAboutTodoAnnotatedTests && $test instanceof PHPUnit_Framework_TestCase) {
  627. $annotations = $test->getAnnotations();
  628. if (isset($annotations['method']['todo'])) {
  629. $this->addFailure(
  630. $test,
  631. new PHPUnit_Framework_RiskyTestError(
  632. 'Test method is annotated with @todo'
  633. ),
  634. $time
  635. );
  636. }
  637. }
  638. $this->endTest($test, $time);
  639. }
  640. /**
  641. * Gets the number of run tests.
  642. *
  643. * @return int
  644. */
  645. public function count()
  646. {
  647. return $this->runTests;
  648. }
  649. /**
  650. * Checks whether the test run should stop.
  651. *
  652. * @return bool
  653. */
  654. public function shouldStop()
  655. {
  656. return $this->stop;
  657. }
  658. /**
  659. * Marks that the test run should stop.
  660. */
  661. public function stop()
  662. {
  663. $this->stop = true;
  664. }
  665. /**
  666. * Returns the PHP_CodeCoverage object.
  667. *
  668. * @return PHP_CodeCoverage
  669. *
  670. * @since Method available since Release 3.5.0
  671. */
  672. public function getCodeCoverage()
  673. {
  674. return $this->codeCoverage;
  675. }
  676. /**
  677. * Sets the PHP_CodeCoverage object.
  678. *
  679. * @param PHP_CodeCoverage $codeCoverage
  680. *
  681. * @since Method available since Release 3.6.0
  682. */
  683. public function setCodeCoverage(PHP_CodeCoverage $codeCoverage)
  684. {
  685. $this->codeCoverage = $codeCoverage;
  686. }
  687. /**
  688. * Enables or disables the error-to-exception conversion.
  689. *
  690. * @param bool $flag
  691. *
  692. * @throws PHPUnit_Framework_Exception
  693. *
  694. * @since Method available since Release 3.2.14
  695. */
  696. public function convertErrorsToExceptions($flag)
  697. {
  698. if (!is_bool($flag)) {
  699. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  700. }
  701. $this->convertErrorsToExceptions = $flag;
  702. }
  703. /**
  704. * Returns the error-to-exception conversion setting.
  705. *
  706. * @return bool
  707. *
  708. * @since Method available since Release 3.4.0
  709. */
  710. public function getConvertErrorsToExceptions()
  711. {
  712. return $this->convertErrorsToExceptions;
  713. }
  714. /**
  715. * Enables or disables the stopping when an error occurs.
  716. *
  717. * @param bool $flag
  718. *
  719. * @throws PHPUnit_Framework_Exception
  720. *
  721. * @since Method available since Release 3.5.0
  722. */
  723. public function stopOnError($flag)
  724. {
  725. if (!is_bool($flag)) {
  726. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  727. }
  728. $this->stopOnError = $flag;
  729. }
  730. /**
  731. * Enables or disables the stopping when a failure occurs.
  732. *
  733. * @param bool $flag
  734. *
  735. * @throws PHPUnit_Framework_Exception
  736. *
  737. * @since Method available since Release 3.1.0
  738. */
  739. public function stopOnFailure($flag)
  740. {
  741. if (!is_bool($flag)) {
  742. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  743. }
  744. $this->stopOnFailure = $flag;
  745. }
  746. /**
  747. * @param bool $flag
  748. *
  749. * @throws PHPUnit_Framework_Exception
  750. *
  751. * @since Method available since Release 4.0.0
  752. */
  753. public function beStrictAboutTestsThatDoNotTestAnything($flag)
  754. {
  755. if (!is_bool($flag)) {
  756. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  757. }
  758. $this->beStrictAboutTestsThatDoNotTestAnything = $flag;
  759. }
  760. /**
  761. * @return bool
  762. *
  763. * @since Method available since Release 4.0.0
  764. */
  765. public function isStrictAboutTestsThatDoNotTestAnything()
  766. {
  767. return $this->beStrictAboutTestsThatDoNotTestAnything;
  768. }
  769. /**
  770. * @param bool $flag
  771. *
  772. * @throws PHPUnit_Framework_Exception
  773. *
  774. * @since Method available since Release 4.0.0
  775. */
  776. public function beStrictAboutOutputDuringTests($flag)
  777. {
  778. if (!is_bool($flag)) {
  779. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  780. }
  781. $this->beStrictAboutOutputDuringTests = $flag;
  782. }
  783. /**
  784. * @return bool
  785. *
  786. * @since Method available since Release 4.0.0
  787. */
  788. public function isStrictAboutOutputDuringTests()
  789. {
  790. return $this->beStrictAboutOutputDuringTests;
  791. }
  792. /**
  793. * @param bool $flag
  794. *
  795. * @throws PHPUnit_Framework_Exception
  796. *
  797. * @since Method available since Release 4.0.0
  798. */
  799. public function beStrictAboutTestSize($flag)
  800. {
  801. if (!is_bool($flag)) {
  802. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  803. }
  804. $this->beStrictAboutTestSize = $flag;
  805. }
  806. /**
  807. * @return bool
  808. *
  809. * @since Method available since Release 4.0.0
  810. */
  811. public function isStrictAboutTestSize()
  812. {
  813. return $this->beStrictAboutTestSize;
  814. }
  815. /**
  816. * @param bool $flag
  817. *
  818. * @throws PHPUnit_Framework_Exception
  819. *
  820. * @since Method available since Release 4.2.0
  821. */
  822. public function beStrictAboutTodoAnnotatedTests($flag)
  823. {
  824. if (!is_bool($flag)) {
  825. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  826. }
  827. $this->beStrictAboutTodoAnnotatedTests = $flag;
  828. }
  829. /**
  830. * @return bool
  831. *
  832. * @since Method available since Release 4.2.0
  833. */
  834. public function isStrictAboutTodoAnnotatedTests()
  835. {
  836. return $this->beStrictAboutTodoAnnotatedTests;
  837. }
  838. /**
  839. * Enables or disables the stopping for risky tests.
  840. *
  841. * @param bool $flag
  842. *
  843. * @throws PHPUnit_Framework_Exception
  844. *
  845. * @since Method available since Release 4.0.0
  846. */
  847. public function stopOnRisky($flag)
  848. {
  849. if (!is_bool($flag)) {
  850. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  851. }
  852. $this->stopOnRisky = $flag;
  853. }
  854. /**
  855. * Enables or disables the stopping for incomplete tests.
  856. *
  857. * @param bool $flag
  858. *
  859. * @throws PHPUnit_Framework_Exception
  860. *
  861. * @since Method available since Release 3.5.0
  862. */
  863. public function stopOnIncomplete($flag)
  864. {
  865. if (!is_bool($flag)) {
  866. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  867. }
  868. $this->stopOnIncomplete = $flag;
  869. }
  870. /**
  871. * Enables or disables the stopping for skipped tests.
  872. *
  873. * @param bool $flag
  874. *
  875. * @throws PHPUnit_Framework_Exception
  876. *
  877. * @since Method available since Release 3.1.0
  878. */
  879. public function stopOnSkipped($flag)
  880. {
  881. if (!is_bool($flag)) {
  882. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  883. }
  884. $this->stopOnSkipped = $flag;
  885. }
  886. /**
  887. * Returns the time spent running the tests.
  888. *
  889. * @return float
  890. */
  891. public function time()
  892. {
  893. return $this->time;
  894. }
  895. /**
  896. * Returns whether the entire test was successful or not.
  897. *
  898. * @return bool
  899. */
  900. public function wasSuccessful()
  901. {
  902. return empty($this->errors) && empty($this->failures);
  903. }
  904. /**
  905. * Sets the timeout for small tests.
  906. *
  907. * @param int $timeout
  908. *
  909. * @throws PHPUnit_Framework_Exception
  910. *
  911. * @since Method available since Release 3.6.0
  912. */
  913. public function setTimeoutForSmallTests($timeout)
  914. {
  915. if (!is_integer($timeout)) {
  916. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'integer');
  917. }
  918. $this->timeoutForSmallTests = $timeout;
  919. }
  920. /**
  921. * Sets the timeout for medium tests.
  922. *
  923. * @param int $timeout
  924. *
  925. * @throws PHPUnit_Framework_Exception
  926. *
  927. * @since Method available since Release 3.6.0
  928. */
  929. public function setTimeoutForMediumTests($timeout)
  930. {
  931. if (!is_integer($timeout)) {
  932. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'integer');
  933. }
  934. $this->timeoutForMediumTests = $timeout;
  935. }
  936. /**
  937. * Sets the timeout for large tests.
  938. *
  939. * @param int $timeout
  940. *
  941. * @throws PHPUnit_Framework_Exception
  942. *
  943. * @since Method available since Release 3.6.0
  944. */
  945. public function setTimeoutForLargeTests($timeout)
  946. {
  947. if (!is_integer($timeout)) {
  948. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'integer');
  949. }
  950. $this->timeoutForLargeTests = $timeout;
  951. }
  952. /**
  953. * Returns the class hierarchy for a given class.
  954. *
  955. * @param string $className
  956. * @param bool $asReflectionObjects
  957. *
  958. * @return array
  959. */
  960. protected function getHierarchy($className, $asReflectionObjects = false)
  961. {
  962. if ($asReflectionObjects) {
  963. $classes = array(new ReflectionClass($className));
  964. } else {
  965. $classes = array($className);
  966. }
  967. $done = false;
  968. while (!$done) {
  969. if ($asReflectionObjects) {
  970. $class = new ReflectionClass(
  971. $classes[count($classes) - 1]->getName()
  972. );
  973. } else {
  974. $class = new ReflectionClass($classes[count($classes) - 1]);
  975. }
  976. $parent = $class->getParentClass();
  977. if ($parent !== false) {
  978. if ($asReflectionObjects) {
  979. $classes[] = $parent;
  980. } else {
  981. $classes[] = $parent->getName();
  982. }
  983. } else {
  984. $done = true;
  985. }
  986. }
  987. return $classes;
  988. }
  989. }