CakeTestRunner.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * @return void
  30. */
  31. public function __construct($loader, $params) {
  32. parent::__construct($loader);
  33. $this->_params = $params;
  34. }
  35. /**
  36. * Actually run a suite of tests. Cake initializes fixtures here using the chosen fixture manager
  37. *
  38. * @param PHPUnit_Framework_Test $suite
  39. * @param array $arguments
  40. * @return void
  41. */
  42. public function doRun(PHPUnit_Framework_Test $suite, array $arguments = array()) {
  43. if (isset($arguments['printer'])) {
  44. self::$versionStringPrinted = true;
  45. }
  46. $fixture = $this->_getFixtureManager($arguments);
  47. foreach ($suite->getIterator() as $test) {
  48. if ($test instanceof CakeTestCase) {
  49. $fixture->fixturize($test);
  50. $test->fixtureManager = $fixture;
  51. }
  52. }
  53. $return = parent::doRun($suite, $arguments);
  54. $fixture->shutdown();
  55. return $return;
  56. }
  57. // @codingStandardsIgnoreStart PHPUnit overrides don't match CakePHP
  58. /**
  59. * Create the test result and splice on our code coverage reports.
  60. *
  61. * @return PHPUnit_Framework_TestResult
  62. */
  63. protected function createTestResult() {
  64. $result = new PHPUnit_Framework_TestResult;
  65. if (!empty($this->_params['codeCoverage'])) {
  66. if (method_exists($result, 'collectCodeCoverageInformation')) {
  67. $result->collectCodeCoverageInformation(true);
  68. }
  69. if (method_exists($result, 'setCodeCoverage')) {
  70. $result->setCodeCoverage(new PHP_CodeCoverage());
  71. }
  72. }
  73. return $result;
  74. }
  75. // @codingStandardsIgnoreEnd
  76. /**
  77. * Get the fixture manager class specified or use the default one.
  78. *
  79. * @return instance of a fixture manager.
  80. * @throws RuntimeException When fixture manager class cannot be loaded.
  81. */
  82. protected function _getFixtureManager($arguments) {
  83. if (isset($arguments['fixtureManager'])) {
  84. App::uses($arguments['fixtureManager'], 'TestSuite');
  85. if (class_exists($arguments['fixtureManager'])) {
  86. return new $arguments['fixtureManager'];
  87. }
  88. throw new RuntimeException(__d('cake_dev', 'Could not find fixture manager %s.', $arguments['fixtureManager']));
  89. }
  90. App::uses('AppFixtureManager', 'TestSuite');
  91. if (class_exists('AppFixtureManager')) {
  92. return new AppFixtureManager();
  93. }
  94. return new CakeFixtureManager();
  95. }
  96. }