CakeTestRunner.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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-2011, 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-2011, 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 'PHPUnit/TextUI/TestRunner.php';
  19. PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'DEFAULT');
  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. /**
  58. * Create the test result and splice on our code coverage reports.
  59. *
  60. * @return PHPUnit_Framework_TestResult
  61. */
  62. protected function createTestResult() {
  63. $result = new PHPUnit_Framework_TestResult;
  64. if (isset($this->_params['codeCoverage'])) {
  65. $result->collectCodeCoverageInformation(true);
  66. }
  67. return $result;
  68. }
  69. /**
  70. * Get the fixture manager class specified or use the default one.
  71. *
  72. * @return instance of a fixture manager.
  73. */
  74. protected function _getFixtureManager($arguments) {
  75. if (isset($arguments['fixtureManager'])) {
  76. App::uses($arguments['fixtureManager'], 'TestSuite');
  77. if (class_exists($arguments['fixtureManager'])) {
  78. return new $arguments['fixtureManager'];
  79. }
  80. throw new RuntimeException(__d('cake_dev', 'Could not find fixture manager %s.', $arguments['fixtureManager']));
  81. }
  82. App::uses('AppFixtureManager', 'TestSuite');
  83. if (class_exists('AppFixtureManager')) {
  84. return new AppFixtureManager();
  85. }
  86. return new CakeFixtureManager();
  87. }
  88. }