CakeTestRunner.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * TestRunner for CakePHP Test suite.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @since CakePHP(tm) v 2.0
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. require_once 'PHPUnit/TextUI/TestRunner.php';
  19. App::uses('CakeFixtureManager', 'TestSuite/Fixture');
  20. /**
  21. * A custom test runner for Cake's use of PHPUnit.
  22. *
  23. * @package Cake.TestSuite
  24. */
  25. class CakeTestRunner extends PHPUnit_TextUI_TestRunner {
  26. /**
  27. * Lets us pass in some options needed for cake's webrunner.
  28. *
  29. * @param mixed $loader
  30. * @param array $params list of options to be used for this run
  31. * @return void
  32. */
  33. public function __construct($loader, $params) {
  34. parent::__construct($loader);
  35. $this->_params = $params;
  36. }
  37. /**
  38. * Actually run a suite of tests. Cake initializes fixtures here using the chosen fixture manager
  39. *
  40. * @param PHPUnit_Framework_Test $suite
  41. * @param array $arguments
  42. * @return void
  43. */
  44. public function doRun(PHPUnit_Framework_Test $suite, array $arguments = array()) {
  45. if (isset($arguments['printer'])) {
  46. self::$versionStringPrinted = true;
  47. }
  48. $fixture = $this->_getFixtureManager($arguments);
  49. foreach ($suite->getIterator() as $test) {
  50. if ($test instanceof CakeTestCase) {
  51. $fixture->fixturize($test);
  52. $test->fixtureManager = $fixture;
  53. }
  54. }
  55. $return = parent::doRun($suite, $arguments);
  56. $fixture->shutdown();
  57. return $return;
  58. }
  59. // @codingStandardsIgnoreStart PHPUnit overrides don't match CakePHP
  60. /**
  61. * Create the test result and splice on our code coverage reports.
  62. *
  63. * @return PHPUnit_Framework_TestResult
  64. */
  65. protected function createTestResult() {
  66. $result = new PHPUnit_Framework_TestResult;
  67. if (!empty($this->_params['codeCoverage'])) {
  68. if (method_exists($result, 'collectCodeCoverageInformation')) {
  69. $result->collectCodeCoverageInformation(true);
  70. }
  71. if (method_exists($result, 'setCodeCoverage')) {
  72. $result->setCodeCoverage(new PHP_CodeCoverage());
  73. }
  74. }
  75. return $result;
  76. }
  77. // @codingStandardsIgnoreEnd
  78. /**
  79. * Get the fixture manager class specified or use the default one.
  80. *
  81. * @param array $arguments
  82. * @return mixed instance of a fixture manager.
  83. * @throws RuntimeException When fixture manager class cannot be loaded.
  84. */
  85. protected function _getFixtureManager($arguments) {
  86. if (isset($arguments['fixtureManager'])) {
  87. App::uses($arguments['fixtureManager'], 'TestSuite');
  88. if (class_exists($arguments['fixtureManager'])) {
  89. return new $arguments['fixtureManager'];
  90. }
  91. throw new RuntimeException(__d('cake_dev', 'Could not find fixture manager %s.', $arguments['fixtureManager']));
  92. }
  93. App::uses('AppFixtureManager', 'TestSuite');
  94. if (class_exists('AppFixtureManager')) {
  95. return new AppFixtureManager();
  96. }
  97. return new CakeFixtureManager();
  98. }
  99. }