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

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

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