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