CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CakeTestCase
  • CakeTestLoader
  • CakeTestRunner
  • CakeTestSuite
  • CakeTestSuiteCommand
  • CakeTestSuiteDispatcher
  • ControllerTestCase
  • ControllerTestDispatcher
  • InterceptContentHelper
  1: <?php
  2: /**
  3:  * CakeTestSuiteDispatcher controls dispatching TestSuite web based requests.
  4:  *
  5:  * PHP 5
  6:  *
  7:  * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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:  * @package       Cake.TestSuite
 16:  * @since         CakePHP(tm) v 1.3
 17:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 18:  */
 19: 
 20: define('CORE_TEST_CASES', CAKE . 'Test' . DS . 'Case');
 21: define('APP_TEST_CASES', TESTS . 'Case');
 22: 
 23: App::uses('CakeTestSuiteCommand', 'TestSuite');
 24: 
 25: /**
 26:  * CakeTestSuiteDispatcher handles web requests to the test suite and runs the correct action.
 27:  *
 28:  * @package       Cake.TestSuite
 29:  */
 30: class CakeTestSuiteDispatcher {
 31: 
 32: /**
 33:  * 'Request' parameters
 34:  *
 35:  * @var array
 36:  */
 37:     public $params = array(
 38:         'codeCoverage' => false,
 39:         'case' => null,
 40:         'core' => false,
 41:         'app' => true,
 42:         'plugin' => null,
 43:         'output' => 'html',
 44:         'show' => 'groups',
 45:         'show_passes' => false,
 46:         'filter' => false,
 47:         'fixture' => null
 48:     );
 49: 
 50: /**
 51:  * Baseurl for the request
 52:  *
 53:  * @var string
 54:  */
 55:     protected $_baseUrl;
 56: 
 57: /**
 58:  * Base dir of the request.  Used for accessing assets.
 59:  *
 60:  * @var string
 61:  */
 62:     protected $_baseDir;
 63: 
 64: /**
 65:  * boolean to set auto parsing of params.
 66:  *
 67:  * @var boolean
 68:  */
 69:     protected $_paramsParsed = false;
 70: 
 71: /**
 72:  * reporter instance used for the request
 73:  *
 74:  * @var CakeBaseReporter
 75:  */
 76:     protected static $_Reporter = null;
 77: 
 78: /**
 79:  * constructor
 80:  *
 81:  * @return void
 82:  */
 83:     public function __construct() {
 84:         $this->_baseUrl = $_SERVER['PHP_SELF'];
 85:         $dir = rtrim(dirname($this->_baseUrl), '\\');
 86:         $this->_baseDir = ($dir === '/') ? $dir : $dir . '/';
 87:     }
 88: 
 89: /**
 90:  * Runs the actions required by the URL parameters.
 91:  *
 92:  * @return void
 93:  */
 94:     public function dispatch() {
 95:         $this->_checkPHPUnit();
 96:         $this->_parseParams();
 97: 
 98:         if ($this->params['case']) {
 99:             $value = $this->_runTestCase();
100:         } else {
101:             $value = $this->_testCaseList();
102:         }
103: 
104:         $output = ob_get_clean();
105:         echo $output;
106:         return $value;
107:     }
108: 
109: /**
110:  * Static method to initialize the test runner, keeps global space clean
111:  *
112:  * @return void
113:  */
114:     public static function run() {
115:         $dispatcher = new CakeTestSuiteDispatcher();
116:         $dispatcher->dispatch();
117:     }
118: 
119: /**
120:  * Checks that PHPUnit is installed.  Will exit if it doesn't
121:  *
122:  * @return void
123:  */
124:     protected function _checkPHPUnit() {
125:         $found = $this->loadTestFramework();
126:         if (!$found) {
127:             $baseDir = $this->_baseDir;
128:             include CAKE . 'TestSuite' . DS . 'templates' . DS . 'phpunit.php';
129:             exit();
130:         }
131:     }
132: 
133: /**
134:  * Checks for the existence of the test framework files
135:  *
136:  * @return boolean true if found, false otherwise
137:  */
138:     public function loadTestFramework() {
139:         foreach (App::path('vendors') as $vendor) {
140:             if (is_dir($vendor . 'PHPUnit')) {
141:                 ini_set('include_path', $vendor . PATH_SEPARATOR . ini_get('include_path'));
142:                 break;
143:             }
144:         }
145: 
146:         return include 'PHPUnit' . DS . 'Autoload.php';
147:     }
148: 
149: /**
150:  * Checks for the xdebug extension required to do code coverage. Displays an error
151:  * if xdebug isn't installed.
152:  *
153:  * @return void
154:  */
155:     protected function _checkXdebug() {
156:         if (!extension_loaded('xdebug')) {
157:             $baseDir = $this->_baseDir;
158:             include CAKE . 'TestSuite' . DS . 'templates' . DS . 'xdebug.php';
159:             exit();
160:         }
161:     }
162: 
163: /**
164:  * Generates a page containing the a list of test cases that could be run.
165:  *
166:  * @return void
167:  */
168:     protected function _testCaseList() {
169:         $command = new CakeTestSuiteCommand('', $this->params);
170:         $Reporter = $command->handleReporter($this->params['output']);
171:         $Reporter->paintDocumentStart();
172:         $Reporter->paintTestMenu();
173:         $Reporter->testCaseList();
174:         $Reporter->paintDocumentEnd();
175:     }
176: 
177: /**
178:  * Sets the params, calling this will bypass the auto parameter parsing.
179:  *
180:  * @param array $params Array of parameters for the dispatcher
181:  * @return void
182:  */
183:     public function setParams($params) {
184:         $this->params = $params;
185:         $this->_paramsParsed = true;
186:     }
187: 
188: /**
189:  * Parse url params into a 'request'
190:  *
191:  * @return void
192:  */
193:     protected function _parseParams() {
194:         if (!$this->_paramsParsed) {
195:             if (!isset($_SERVER['SERVER_NAME'])) {
196:                 $_SERVER['SERVER_NAME'] = '';
197:             }
198:             foreach ($this->params as $key => $value) {
199:                 if (isset($_GET[$key])) {
200:                     $this->params[$key] = $_GET[$key];
201:                 }
202:             }
203:             if (isset($_GET['code_coverage'])) {
204:                 $this->params['codeCoverage'] = true;
205:                 $this->_checkXdebug();
206:             }
207:         }
208:         if (empty($this->params['plugin']) && empty($this->params['core'])) {
209:             $this->params['app'] = true;
210:         }
211:         $this->params['baseUrl'] = $this->_baseUrl;
212:         $this->params['baseDir'] = $this->_baseDir;
213:     }
214: 
215: /**
216:  * Runs a test case file.
217:  *
218:  * @return void
219:  */
220:     protected function _runTestCase() {
221:         $commandArgs = array(
222:             'case' => $this->params['case'],
223:             'core' => $this->params['core'],
224:             'app' => $this->params['app'],
225:             'plugin' => $this->params['plugin'],
226:             'codeCoverage' => $this->params['codeCoverage'],
227:             'showPasses' => !empty($this->params['show_passes']),
228:             'baseUrl' => $this->_baseUrl,
229:             'baseDir' => $this->_baseDir,
230:         );
231: 
232:         $options = array(
233:             '--filter', $this->params['filter'],
234:             '--output', $this->params['output'],
235:             '--fixture', $this->params['fixture']
236:         );
237:         restore_error_handler();
238: 
239:         try {
240:             self::time();
241:             $command = new CakeTestSuiteCommand('CakeTestLoader', $commandArgs);
242:             $result = $command->run($options);
243:         } catch (MissingConnectionException $exception) {
244:             ob_end_clean();
245:             $baseDir = $this->_baseDir;
246:             include CAKE . 'TestSuite' . DS . 'templates' . DS . 'missing_connection.php';
247:             exit();
248:         }
249:     }
250: 
251: /**
252:  * Sets a static timestamp
253:  *
254:  * @param boolean $reset to set new static timestamp.
255:  * @return integer timestamp
256:  */
257:     public static function time($reset = false) {
258:         static $now;
259:         if ($reset || !$now) {
260:             $now = time();
261:         }
262:         return $now;
263:     }
264: 
265: /**
266:  * Returns formatted date string using static time
267:  * This method is being used as formatter for created, modified and updated fields in Model::save()
268:  *
269:  * @param string $format format to be used.
270:  * @return string formatted date
271:  */
272:     public static function date($format) {
273:         return date($format, self::time());
274:     }
275: 
276: }
277: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs