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