~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/tests/lib/cake_test_suite_dispatcher.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CakeTestSuiteDispatcher controls dispatching TestSuite web based requests.
 
4
 *
 
5
 * PHP versions 4 and 5
 
6
 *
 
7
 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
 
8
 * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
9
 *
 
10
 *  Licensed under The Open Group Test Suite License
 
11
 *  Redistributions of files must retain the above copyright notice.
 
12
 *
 
13
 * @copyright     Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
14
 * @link          http://cakephp.org CakePHP(tm) Project
 
15
 * @package       cake
 
16
 * @subpackage    cake.cake.tests.lib
 
17
 * @since         CakePHP(tm) v 1.3
 
18
 * @license       http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
 
19
 */
 
20
require_once CAKE_TESTS_LIB . 'test_manager.php';
 
21
 
 
22
/**
 
23
 * CakeTestSuiteDispatcher handles web requests to the test suite and runs the correct action.
 
24
 *
 
25
 * @package cake.tests.libs
 
26
 */
 
27
class CakeTestSuiteDispatcher {
 
28
/**
 
29
 * 'Request' parameters
 
30
 *
 
31
 * @var array
 
32
 */
 
33
        var $params = array(
 
34
                'codeCoverage' => false,
 
35
                'group' => null,
 
36
                'case' => null,
 
37
                'app' => false,
 
38
                'plugin' => null,
 
39
                'output' => 'html',
 
40
                'show' => 'groups',
 
41
                'show_passes' => false
 
42
        );
 
43
 
 
44
/**
 
45
 * The classname for the TestManager being used
 
46
 *
 
47
 * @var string
 
48
 */
 
49
        var $_managerClass = 'TestManager';
 
50
 
 
51
/**
 
52
 * The Instance of the Manager being used.
 
53
 *
 
54
 * @var TestManager subclass
 
55
 */
 
56
        var $Manager;
 
57
 
 
58
/**
 
59
 * Baseurl for the request
 
60
 *
 
61
 * @var string
 
62
 */
 
63
        var $_baseUrl;
 
64
 
 
65
/**
 
66
 * Base dir of the request.  Used for accessing assets.
 
67
 *
 
68
 * @var string
 
69
 */
 
70
        var $_baseDir;
 
71
 
 
72
/**
 
73
 * constructor
 
74
 *
 
75
 * @return void
 
76
 */
 
77
        function CakeTestSuiteDispatcher() {
 
78
                $this->_baseUrl = $_SERVER['PHP_SELF'];
 
79
                $dir = rtrim(dirname($this->_baseUrl), '\\');
 
80
                $this->_baseDir = ($dir === '/') ? $dir : $dir . '/';
 
81
        }
 
82
 
 
83
/**
 
84
 * Runs the actions required by the URL parameters.
 
85
 *
 
86
 * @return void
 
87
 */
 
88
        function dispatch() {
 
89
                $this->_checkSimpleTest();
 
90
                $this->_parseParams();
 
91
 
 
92
                if ($this->params['group']) {
 
93
                        $this->_runGroupTest();
 
94
                } elseif ($this->params['case']) {
 
95
                        $this->_runTestCase();
 
96
                } elseif (isset($_GET['show']) && $_GET['show'] == 'cases') {
 
97
                        $this->_testCaseList();
 
98
                } else {
 
99
                        $this->_groupTestList();
 
100
                }
 
101
 
 
102
                $output = ob_get_clean();
 
103
                echo $output;
 
104
        }
 
105
 
 
106
/**
 
107
 * Checks that simpleTest is installed.  Will exit if it doesn't
 
108
 *
 
109
 * @return void
 
110
 */
 
111
        function _checkSimpleTest() {
 
112
                if (!App::import('Vendor', 'simpletest' . DS . 'reporter')) {
 
113
                        $baseDir = $this->_baseDir;
 
114
                        include CAKE_TESTS_LIB . 'templates' . DS . 'simpletest.php';
 
115
                        exit();
 
116
                }
 
117
        }
 
118
 
 
119
/**
 
120
 * Checks for the xdebug extension required to do code coverage. Displays an error
 
121
 * if xdebug isn't installed.
 
122
 *
 
123
 * @return void
 
124
 */
 
125
        function _checkXdebug() {
 
126
                if (!extension_loaded('xdebug')) {
 
127
                        $baseDir = $this->_baseDir;
 
128
                        include CAKE_TESTS_LIB . 'templates' . DS . 'xdebug.php';
 
129
                        exit();
 
130
                }
 
131
        }
 
132
 
 
133
/**
 
134
 * Generates a page containing the a list of test cases that could be run.
 
135
 *
 
136
 * @return void
 
137
 */
 
138
        function _testCaseList() {
 
139
                $Reporter =& $this->getReporter();
 
140
                $Reporter->paintDocumentStart();
 
141
                $Reporter->paintTestMenu();
 
142
                $Reporter->testCaseList();
 
143
                $Reporter->paintDocumentEnd();
 
144
        }
 
145
 
 
146
/**
 
147
 * Generates a page containing a list of group tests that could be run.
 
148
 *
 
149
 * @return void
 
150
 */
 
151
        function _groupTestList() {
 
152
                $Reporter =& $this->getReporter();
 
153
                $Reporter->paintDocumentStart();
 
154
                $Reporter->paintTestMenu();
 
155
                $Reporter->groupTestList();
 
156
                $Reporter->paintDocumentEnd();
 
157
        }
 
158
 
 
159
/**
 
160
 * Sets the Manager to use for the request.
 
161
 *
 
162
 * @return string The manager class name
 
163
 * @static
 
164
 */
 
165
        function &getManager() {
 
166
                if (empty($this->Manager)) {
 
167
                        $this->Manager = new $this->_managerClass();
 
168
                }
 
169
                return $this->Manager;
 
170
        }
 
171
 
 
172
/**
 
173
 * Gets the reporter based on the request parameters
 
174
 *
 
175
 * @return void
 
176
 * @static
 
177
 */
 
178
        function &getReporter() {
 
179
                static $Reporter = NULL;
 
180
                if (!$Reporter) {
 
181
                        $type = strtolower($this->params['output']);
 
182
                        $coreClass = 'Cake' . ucwords($this->params['output']) . 'Reporter';
 
183
                        $coreFile = CAKE_TESTS_LIB . 'reporter' . DS . 'cake_' . $type . '_reporter.php';
 
184
 
 
185
                        $appClass = $this->params['output'] . 'Reporter';
 
186
                        $appFile = APPLIBS . 'test_suite' . DS . 'reporter' . DS . $type . '_reporter.php';
 
187
                        if (include_once $coreFile) {
 
188
                                $Reporter =& new $coreClass(null, $this->params);
 
189
                        } elseif (include_once $appFile) {
 
190
                                $Reporter =& new $appClass(null, $this->params);
 
191
                        }
 
192
                }
 
193
                return $Reporter;
 
194
        }
 
195
 
 
196
/**
 
197
 * Parse url params into a 'request'
 
198
 *
 
199
 * @return void
 
200
 */
 
201
        function _parseParams() {
 
202
                if (!isset($_SERVER['SERVER_NAME'])) {
 
203
                        $_SERVER['SERVER_NAME'] = '';
 
204
                }
 
205
                foreach ($this->params as $key => $value) {
 
206
                        if (isset($_GET[$key])) {
 
207
                                $this->params[$key] = $_GET[$key];
 
208
                        }
 
209
                }
 
210
                if (isset($_GET['code_coverage'])) {
 
211
                        $this->params['codeCoverage'] = true;
 
212
                        require_once CAKE_TESTS_LIB . 'code_coverage_manager.php';
 
213
                        $this->_checkXdebug();
 
214
                }
 
215
                $this->params['baseUrl'] = $this->_baseUrl;
 
216
                $this->params['baseDir'] = $this->_baseDir;
 
217
                $this->getManager();
 
218
        }
 
219
 
 
220
/**
 
221
 * Runs the group test case.
 
222
 *
 
223
 * @return void
 
224
 */
 
225
        function _runGroupTest() {
 
226
                $Reporter =& CakeTestSuiteDispatcher::getReporter();
 
227
                if ($this->params['codeCoverage']) {
 
228
                        CodeCoverageManager::init($this->params['group'], $Reporter);
 
229
                }
 
230
                if ('all' == $this->params['group']) {
 
231
                        $this->Manager->runAllTests($Reporter);
 
232
                } else {
 
233
                        $this->Manager->runGroupTest(ucfirst($this->params['group']), $Reporter);
 
234
                }
 
235
        }
 
236
 
 
237
/**
 
238
 * Runs a test case file.
 
239
 *
 
240
 * @return void
 
241
 */
 
242
        function _runTestCase() {
 
243
                $Reporter =& CakeTestSuiteDispatcher::getReporter();
 
244
                if ($this->params['codeCoverage']) {
 
245
                        CodeCoverageManager::init($this->params['case'], $Reporter);
 
246
                }
 
247
                $this->Manager->runTestCase($this->params['case'], $Reporter);
 
248
        }
 
249
}