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

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.10
      • 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
  • None

Classes

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