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.3 API

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