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

  • AclShell
  • ApiShell
  • BakeShell
  • CommandListShell
  • ConsoleShell
  • I18nShell
  • SchemaShell
  • TestShell
  • TestsuiteShell
  • UpgradeShell
  1: <?php
  2: /**
  3:  * Test Shell
  4:  *
  5:  * This Shell allows the running of test suites via the cake command line
  6:  *
  7:  * PHP 5
  8:  *
  9:  * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
 10:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 11:  *
 12:  * Licensed under The MIT License
 13:  * Redistributions of files must retain the above copyright notice
 14:  *
 15:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 16:  * @link          http://book.cakephp.org/2.0/en/development/testing.html
 17:  * @since         CakePHP(tm) v 1.2.0.4433
 18:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 19:  */
 20: 
 21: App::uses('Shell', 'Console');
 22: App::uses('CakeTestSuiteDispatcher', 'TestSuite');
 23: App::uses('CakeTestSuiteCommand', 'TestSuite');
 24: App::uses('CakeTestLoader', 'TestSuite');
 25: 
 26: /**
 27:  * Provides a CakePHP wrapper around PHPUnit.
 28:  * Adds in CakePHP's fixtures and gives access to plugin, app and core test cases
 29:  *
 30:  * @package       Cake.Console.Command
 31:  */
 32: class TestShell extends Shell {
 33: 
 34: /**
 35:  * Dispatcher object for the run.
 36:  *
 37:  * @var CakeTestDispatcher
 38:  */
 39:     protected $_dispatcher = null;
 40: 
 41: /**
 42:  * get the option parser for the test suite.
 43:  *
 44:  * @return void
 45:  */
 46:     public function getOptionParser() {
 47:         $parser = new ConsoleOptionParser($this->name);
 48:         $parser->description(array(
 49:             __d('cake_console', 'The CakePHP Testsuite allows you to run test cases from the command line'),
 50:         ))->addArgument('category', array(
 51:             'help' => __d('cake_console', 'The category for the test, or test file, to test.'),
 52:             'required' => false,
 53:         ))->addArgument('file', array(
 54:             'help' => __d('cake_console', 'The path to the file, or test file, to test.'),
 55:             'required' => false,
 56:         ))->addOption('log-junit', array(
 57:             'help' => __d('cake_console', '<file> Log test execution in JUnit XML format to file.'),
 58:             'default' => false
 59:         ))->addOption('log-json', array(
 60:             'help' => __d('cake_console', '<file> Log test execution in JSON format to file.'),
 61:             'default' => false
 62:         ))->addOption('log-tap', array(
 63:             'help' => __d('cake_console', '<file> Log test execution in TAP format to file.'),
 64:             'default' => false
 65:         ))->addOption('log-dbus', array(
 66:             'help' => __d('cake_console', 'Log test execution to DBUS.'),
 67:             'default' => false
 68:         ))->addOption('coverage-html', array(
 69:             'help' => __d('cake_console', '<dir> Generate code coverage report in HTML format.'),
 70:             'default' => false
 71:         ))->addOption('coverage-clover', array(
 72:             'help' => __d('cake_console', '<file> Write code coverage data in Clover XML format.'),
 73:             'default' => false
 74:         ))->addOption('testdox-html', array(
 75:             'help' => __d('cake_console', '<file> Write agile documentation in HTML format to file.'),
 76:             'default' => false
 77:         ))->addOption('testdox-text', array(
 78:             'help' => __d('cake_console', '<file> Write agile documentation in Text format to file.'),
 79:             'default' => false
 80:         ))->addOption('filter', array(
 81:             'help' => __d('cake_console', '<pattern> Filter which tests to run.'),
 82:             'default' => false
 83:         ))->addOption('group', array(
 84:             'help' => __d('cake_console', '<name> Only runs tests from the specified group(s).'),
 85:             'default' => false
 86:         ))->addOption('exclude-group', array(
 87:             'help' => __d('cake_console', '<name> Exclude tests from the specified group(s).'),
 88:             'default' => false
 89:         ))->addOption('list-groups', array(
 90:             'help' => __d('cake_console', 'List available test groups.'),
 91:             'boolean' => true
 92:         ))->addOption('loader', array(
 93:             'help' => __d('cake_console', 'TestSuiteLoader implementation to use.'),
 94:             'default' => false
 95:         ))->addOption('repeat', array(
 96:             'help' => __d('cake_console', '<times> Runs the test(s) repeatedly.'),
 97:             'default' => false
 98:         ))->addOption('tap', array(
 99:             'help' => __d('cake_console', 'Report test execution progress in TAP format.'),
100:             'boolean' => true
101:         ))->addOption('testdox', array(
102:             'help' => __d('cake_console', 'Report test execution progress in TestDox format.'),
103:             'default' => false,
104:             'boolean' => true
105:         ))->addOption('no-colors', array(
106:             'help' => __d('cake_console', 'Do not use colors in output.'),
107:             'boolean' => true
108:         ))->addOption('stderr', array(
109:             'help' => __d('cake_console', 'Write to STDERR instead of STDOUT.'),
110:             'boolean' => true
111:         ))->addOption('stop-on-error', array(
112:             'help' => __d('cake_console', 'Stop execution upon first error or failure.'),
113:             'boolean' => true
114:         ))->addOption('stop-on-failure', array(
115:             'help' => __d('cake_console', 'Stop execution upon first failure.'),
116:             'boolean' => true
117:         ))->addOption('stop-on-skipped', array(
118:             'help' => __d('cake_console', 'Stop execution upon first skipped test.'),
119:             'boolean' => true
120:         ))->addOption('stop-on-incomplete', array(
121:             'help' => __d('cake_console', 'Stop execution upon first incomplete test.'),
122:             'boolean' => true
123:         ))->addOption('strict', array(
124:             'help' => __d('cake_console', 'Mark a test as incomplete if no assertions are made.'),
125:             'boolean' => true
126:         ))->addOption('wait', array(
127:             'help' => __d('cake_console', 'Waits for a keystroke after each test.'),
128:             'boolean' => true
129:         ))->addOption('process-isolation', array(
130:             'help' => __d('cake_console', 'Run each test in a separate PHP process.'),
131:             'boolean' => true
132:         ))->addOption('no-globals-backup', array(
133:             'help' => __d('cake_console', 'Do not backup and restore $GLOBALS for each test.'),
134:             'boolean' => true
135:         ))->addOption('static-backup', array(
136:             'help' => __d('cake_console', 'Backup and restore static attributes for each test.'),
137:             'boolean' => true
138:         ))->addOption('syntax-check', array(
139:             'help' => __d('cake_console', 'Try to check source files for syntax errors.'),
140:             'boolean' => true
141:         ))->addOption('bootstrap', array(
142:             'help' => __d('cake_console', '<file> A "bootstrap" PHP file that is run before the tests.'),
143:             'default' => false
144:         ))->addOption('configuration', array(
145:             'help' => __d('cake_console', '<file> Read configuration from XML file.'),
146:             'default' => false
147:         ))->addOption('no-configuration', array(
148:             'help' => __d('cake_console', 'Ignore default configuration file (phpunit.xml).'),
149:             'boolean' => true
150:         ))->addOption('include-path', array(
151:             'help' => __d('cake_console', '<path(s)> Prepend PHP include_path with given path(s).'),
152:             'default' => false
153:         ))->addOption('directive', array(
154:             'help' => __d('cake_console', 'key[=value] Sets a php.ini value.'),
155:             'default' => false
156:         ))->addOption('fixture', array(
157:             'help' => __d('cake_console', 'Choose a custom fixture manager.'),
158:         ))->addOption('debug', array(
159:             'help' => __d('cake_console', 'More verbose output.'),
160:         ));
161: 
162:         return $parser;
163:     }
164: 
165: /**
166:  * Initialization method installs PHPUnit and loads all plugins
167:  *
168:  * @return void
169:  * @throws Exception
170:  */
171:     public function initialize() {
172:         $this->_dispatcher = new CakeTestSuiteDispatcher();
173:         $sucess = $this->_dispatcher->loadTestFramework();
174:         if (!$sucess) {
175:             throw new Exception(__d('cake_dev', 'Please install PHPUnit framework <info>(http://www.phpunit.de)</info>'));
176:         }
177:     }
178: 
179: /**
180:  * Parse the CLI options into an array CakeTestDispatcher can use.
181:  *
182:  * @return array Array of params for CakeTestDispatcher
183:  */
184:     protected function _parseArgs() {
185:         if (empty($this->args)) {
186:             return;
187:         }
188:         $params = array(
189:             'core' => false,
190:             'app' => false,
191:             'plugin' => null,
192:             'output' => 'text',
193:         );
194: 
195:         if (strpos($this->args[0], '.php')) {
196:             $category = $this->_mapFileToCategory($this->args[0]);
197:             $params['case'] = $this->_mapFileToCase($this->args[0], $category);
198:         } else {
199:             $category = $this->args[0];
200:             if (isset($this->args[1])) {
201:                 $params['case'] = $this->args[1];
202:             }
203:         }
204: 
205:         if ($category === 'core') {
206:             $params['core'] = true;
207:         } elseif ($category === 'app') {
208:             $params['app'] = true;
209:         } else {
210:             $params['plugin'] = $category;
211:         }
212: 
213:         return $params;
214:     }
215: 
216: /**
217:  * Converts the options passed to the shell as options for the PHPUnit cli runner
218:  *
219:  * @return array Array of params for CakeTestDispatcher
220:  */
221:     protected function _runnerOptions() {
222:         $options = array();
223:         $params = $this->params;
224:         unset($params['help']);
225: 
226:         if (!empty($params['no-colors'])) {
227:             unset($params['no-colors'], $params['colors']);
228:         } else {
229:             $params['colors'] = true;
230:         }
231: 
232:         foreach ($params as $param => $value) {
233:             if ($value === false) {
234:                 continue;
235:             }
236:             $options[] = '--' . $param;
237:             if (is_string($value)) {
238:                 $options[] = $value;
239:             }
240:         }
241:         return $options;
242:     }
243: 
244: /**
245:  * Main entry point to this shell
246:  *
247:  * @return void
248:  */
249:     public function main() {
250:         $this->out(__d('cake_console', 'CakePHP Test Shell'));
251:         $this->hr();
252: 
253:         $args = $this->_parseArgs();
254: 
255:         if (empty($args['case'])) {
256:             return $this->available();
257:         }
258: 
259:         $this->_run($args, $this->_runnerOptions());
260:     }
261: 
262: /**
263:  * Runs the test case from $runnerArgs
264:  *
265:  * @param array $runnerArgs list of arguments as obtained from _parseArgs()
266:  * @param array $options list of options as constructed by _runnerOptions()
267:  * @return void
268:  */
269:     protected function _run($runnerArgs, $options = array()) {
270:         restore_error_handler();
271:         restore_error_handler();
272: 
273:         $testCli = new CakeTestSuiteCommand('CakeTestLoader', $runnerArgs);
274:         $testCli->run($options);
275:     }
276: 
277: /**
278:  * Shows a list of available test cases and gives the option to run one of them
279:  *
280:  * @return void
281:  */
282:     public function available() {
283:         $params = $this->_parseArgs();
284:         $testCases = CakeTestLoader::generateTestList($params);
285:         $app = $params['app'];
286:         $plugin = $params['plugin'];
287: 
288:         $title = "Core Test Cases:";
289:         $category = 'core';
290:         if ($app) {
291:             $title = "App Test Cases:";
292:             $category = 'app';
293:         } elseif ($plugin) {
294:             $title = Inflector::humanize($plugin) . " Test Cases:";
295:             $category = $plugin;
296:         }
297: 
298:         if (empty($testCases)) {
299:             $this->out(__d('cake_console', "No test cases available \n\n"));
300:             return $this->out($this->OptionParser->help());
301:         }
302: 
303:         $this->out($title);
304:         $i = 1;
305:         $cases = array();
306:         foreach ($testCases as $testCaseFile => $testCase) {
307:             $case = str_replace('Test.php', '', $testCase);
308:             $this->out("[$i] $case");
309:             $cases[$i] = $case;
310:             $i++;
311:         }
312: 
313:         while ($choice = $this->in(__d('cake_console', 'What test case would you like to run?'), null, 'q')) {
314:             if (is_numeric($choice) && isset($cases[$choice])) {
315:                 $this->args[0] = $category;
316:                 $this->args[1] = $cases[$choice];
317:                 $this->_run($this->_parseArgs(), $this->_runnerOptions());
318:                 break;
319:             }
320: 
321:             if (is_string($choice) && in_array($choice, $cases)) {
322:                 $this->args[0] = $category;
323:                 $this->args[1] = $choice;
324:                 $this->_run($this->_parseArgs(), $this->_runnerOptions());
325:                 break;
326:             }
327: 
328:             if ($choice == 'q') {
329:                 break;
330:             }
331:         }
332:     }
333: 
334: /**
335:  * Find the test case for the passed file. The file could itself be a test.
336:  *
337:  * @param string $file
338:  * @param string $category
339:  * @param boolean $throwOnMissingFile
340:  * @access protected
341:  * @return array(type, case)
342:  * @throws Exception
343:  */
344:     protected function _mapFileToCase($file, $category, $throwOnMissingFile = true) {
345:         if (!$category || (substr($file, -4) !== '.php')) {
346:             return false;
347:         }
348: 
349:         $_file = realpath($file);
350:         if ($_file) {
351:             $file = $_file;
352:         }
353: 
354:         $testFile = $testCase = null;
355: 
356:         if (preg_match('@Test[\\\/]@', $file)) {
357: 
358:             if (substr($file, -8) === 'Test.php') {
359: 
360:                 $testCase = substr($file, 0, -8);
361:                 $testCase = str_replace(DS, '/', $testCase);
362: 
363:                 if ($testCase = preg_replace('@.*Test\/Case\/@', '', $testCase)) {
364: 
365:                     if ($category === 'core') {
366:                         $testCase = str_replace('lib/Cake', '', $testCase);
367:                     }
368: 
369:                     return $testCase;
370:                 }
371: 
372:                 throw new Exception(__d('cake_dev', 'Test case %s cannot be run via this shell', $testFile));
373:             }
374:         }
375: 
376:         $file = substr($file, 0, -4);
377:         if ($category === 'core') {
378: 
379:             $testCase = str_replace(DS, '/', $file);
380:             $testCase = preg_replace('@.*lib/Cake/@', '', $file);
381:             $testCase[0] = strtoupper($testCase[0]);
382:             $testFile = CAKE . 'Test/Case/' . $testCase . 'Test.php';
383: 
384:             if (!file_exists($testFile) && $throwOnMissingFile) {
385:                 throw new Exception(__d('cake_dev', 'Test case %s not found', $testFile));
386:             }
387: 
388:             return $testCase;
389:         }
390: 
391:         if ($category === 'app') {
392:             $testFile = str_replace(APP, APP . 'Test/Case/', $file) . 'Test.php';
393:         } else {
394:             $testFile = preg_replace(
395:                 "@((?:plugins|Plugin)[\\/]{$category}[\\/])(.*)$@",
396:                 '\1Test/Case/\2Test.php',
397:                 $file
398:             );
399:         }
400: 
401:         if (!file_exists($testFile) && $throwOnMissingFile) {
402:             throw new Exception(__d('cake_dev', 'Test case %s not found', $testFile));
403:         }
404: 
405:         $testCase = substr($testFile, 0, -8);
406:         $testCase = str_replace(DS, '/', $testCase);
407:         $testCase = preg_replace('@.*Test/Case/@', '', $testCase);
408: 
409:         return $testCase;
410:     }
411: 
412: /**
413:  * For the given file, what category of test is it? returns app, core or the name of the plugin
414:  *
415:  * @param string $file
416:  * @access protected
417:  * @return string
418:  */
419:     protected function _mapFileToCategory($file) {
420:         $_file = realpath($file);
421:         if ($_file) {
422:             $file = $_file;
423:         }
424: 
425:         $file = str_replace(DS, '/', $file);
426:         if (strpos($file, 'lib/Cake/') !== false) {
427:             return 'core';
428:         } elseif (preg_match('@(?:plugins|Plugin)/([^/]*)@', $file, $match)) {
429:             return $match[1];
430:         }
431:         return 'app';
432:     }
433: 
434: }
435: 
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