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

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.0
      • 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
        • Auth
    • Core
    • Error
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • AclShell
  • ApiShell
  • BakeShell
  • CommandListShell
  • ConsoleShell
  • I18nShell
  • SchemaShell
  • TestsuiteShell
  • UpgradeShell
  1: <?php
  2: /**
  3:  * Test Suite 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/view/1196/Testing>
 10:  * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 16:  * @link          http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
 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('AppShell', 'Console/Command');
 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 TestsuiteShell extends AppShell {
 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:             __d('cake_console', 'If run with no command line arguments, a list of available core test cases will be shown')
 51:         ))->addArgument('category', array(
 52:             'help' => __d('cake_console', 'app, core or name of a plugin.'),
 53:             'required' => true
 54:         ))->addArgument('file', array(
 55:             'help' => __d('cake_console', 'file name with folder prefix and without the test.php suffix.'),
 56:             'required' => false,
 57:         ))->addOption('log-junit', array(
 58:             'help' => __d('cake_console', '<file> Log test execution in JUnit XML format to file.'),
 59:             'default' => false
 60:         ))->addOption('log-json', array(
 61:             'help' => __d('cake_console', '<file> Log test execution in TAP format to file.'),
 62:             'default' => false
 63:         ))->addOption('log-tap', array(
 64:             'help' => __d('cake_console', '<file> Log test execution in TAP format to file.'),
 65:             'default' => false
 66:         ))->addOption('log-dbus', array(
 67:             'help' => __d('cake_console', 'Log test execution to DBUS.'),
 68:             'default' => false
 69:         ))->addOption('coverage-html', array(
 70:             'help' => __d('cake_console', '<dir> Generate code coverage report in HTML format.'),
 71:             'default' => false
 72:         ))->addOption('coverage-clover', array(
 73:             'help' => __d('cake_console', '<file> Write code coverage data in Clover XML format.'),
 74:             'default' => false
 75:         ))->addOption('testdox-html', array(
 76:             'help' => __d('cake_console', '<file> Write agile documentation in HTML format to file.'),
 77:             'default' => false
 78:         ))->addOption('testdox-text', array(
 79:             'help' => __d('cake_console', '<file> Write agile documentation in Text format to file.'),
 80:             'default' => false
 81:         ))->addOption('filter', array(
 82:             'help' => __d('cake_console', '<pattern> Filter which tests to run.'),
 83:             'default' => false
 84:         ))->addOption('group', array(
 85:             'help' => __d('cake_console', '<name> Only runs tests from the specified group(s).'),
 86:             'default' => false
 87:         ))->addOption('exclude-group', array(
 88:             'help' => __d('cake_console', '<name> Exclude tests from the specified group(s).'),
 89:             'default' => false
 90:         ))->addOption('list-groups', array(
 91:             'help' => __d('cake_console', 'List available test groups.'),
 92:             'boolean' => true
 93:         ))->addOption('loader', array(
 94:             'help' => __d('cake_console', 'TestSuiteLoader implementation to use.'),
 95:             'default' => false
 96:         ))->addOption('repeat', array(
 97:             'help' => __d('cake_console', '<times> Runs the test(s) repeatedly.'),
 98:             'default' => false
 99:         ))->addOption('tap', array(
100:             'help' => __d('cake_console', 'Report test execution progress in TAP format.'),
101:             'boolean' => true
102:         ))->addOption('testdox', array(
103:             'help' => __d('cake_console', 'Report test execution progress in TestDox format.'),
104:             'default' => false,
105:             'boolean' => true
106:         ))->addOption('no-colors', array(
107:             'help' => __d('cake_console', 'Do not use colors in output.'),
108:             'boolean' => true
109:         ))->addOption('stderr', array(
110:             'help' => __d('cake_console', 'Write to STDERR instead of STDOUT.'),
111:             'boolean' => true
112:         ))->addOption('stop-on-error', array(
113:             'help' => __d('cake_console', 'Stop execution upon first error or failure.'),
114:             'boolean' => true
115:         ))->addOption('stop-on-failure', array(
116:             'help' => __d('cake_console', 'Stop execution upon first failure.'),
117:             'boolean' => true
118:         ))->addOption('stop-on-skipped ', array(
119:             'help' => __d('cake_console', 'Stop execution upon first skipped test.'),
120:             'boolean' => true
121:         ))->addOption('stop-on-incomplete', array(
122:             'help' => __d('cake_console', 'Stop execution upon first incomplete test.'),
123:             'boolean' => true
124:         ))->addOption('strict', array(
125:             'help' => __d('cake_console', 'Mark a test as incomplete if no assertions are made.'),
126:             'boolean' => true
127:         ))->addOption('wait', array(
128:             'help' => __d('cake_console', 'Waits for a keystroke after each test.'),
129:             'boolean' => true
130:         ))->addOption('process-isolation', array(
131:             'help' => __d('cake_console', 'Run each test in a separate PHP process.'),
132:             'boolean' => true
133:         ))->addOption('no-globals-backup', array(
134:             'help' => __d('cake_console', 'Do not backup and restore $GLOBALS for each test.'),
135:             'boolean' => true
136:         ))->addOption('static-backup ', array(
137:             'help' => __d('cake_console', 'Backup and restore static attributes for each test.'),
138:             'boolean' => true
139:         ))->addOption('syntax-check', array(
140:             'help' => __d('cake_console', 'Try to check source files for syntax errors.'),
141:             'boolean' => true
142:         ))->addOption('bootstrap', array(
143:             'help' => __d('cake_console', '<file> A "bootstrap" PHP file that is run before the tests.'),
144:             'default' => false
145:         ))->addOption('configuration', array(
146:             'help' => __d('cake_console', '<file> Read configuration from XML file.'),
147:             'default' => false
148:         ))->addOption('no-configuration', array(
149:             'help' => __d('cake_console', 'Ignore default configuration file (phpunit.xml).'),
150:             'boolean' => true
151:         ))->addOption('include-path', array(
152:             'help' => __d('cake_console', '<path(s)> Prepend PHP include_path with given path(s).'),
153:             'default' => false
154:         ))->addOption('directive', array(
155:             'help' => __d('cake_console', 'key[=value] Sets a php.ini value.'),
156:             'default' => false
157:         ))->addOption('fixture', array(
158:             'help' => __d('cake_console', 'Choose a custom fixture manager.'),
159:         ))->addOption('debug', array(
160:             'help' => __d('cake_console', 'Enable full output of testsuite. (supported in PHPUnit 3.6.0 and greater)'),
161:         ));
162: 
163:         return $parser;
164:     }
165: 
166: /**
167:  * Initialization method installs PHPUnit and loads all plugins
168:  *
169:  * @return void
170:  * @throws Exception
171:  */
172:     public function initialize() {
173:         $this->_dispatcher = new CakeTestSuiteDispatcher();
174:         $sucess = $this->_dispatcher->loadTestFramework();
175:         if (!$sucess) {
176:             throw new Exception(__d('cake_dev', 'Please install PHPUnit framework <info>(http://www.phpunit.de)</info>'));
177:         }
178:     }
179: 
180: /**
181:  * Parse the CLI options into an array CakeTestDispatcher can use.
182:  *
183:  * @return array Array of params for CakeTestDispatcher
184:  */
185:     protected function _parseArgs() {
186:         if (empty($this->args)) {
187:             return;
188:         }
189:         $params = array(
190:             'core' => false,
191:             'app' => false,
192:             'plugin' => null,
193:             'output' => 'text',
194:         );
195: 
196:         $category = $this->args[0];
197: 
198:         if ($category == 'core') {
199:             $params['core'] = true;
200:         } elseif ($category == 'app') {
201:             $params['app'] = true;
202:         } elseif ($category != 'core') {
203:             $params['plugin'] = $category;
204:         }
205: 
206:         if (isset($this->args[1])) {
207:             $params['case'] = $this->args[1];
208:         }
209:         return $params;
210:     }
211: 
212: /**
213:  * Converts the options passed to the shell as options for the PHPUnit cli runner
214:  *
215:  * @return array Array of params for CakeTestDispatcher
216:  */
217:     protected function _runnerOptions() {
218:         $options = array();
219:         $params = $this->params;
220:         unset($params['help']);
221: 
222:         if (!empty($params['no-colors'])) {
223:             unset($params['no-colors'], $params['colors']);
224:         } else {
225:             $params['colors'] = true;
226:         }
227: 
228:         foreach ($params as $param => $value) {
229:             if ($value === false) {
230:                 continue;
231:             }
232:             $options[] = '--' . $param;
233:             if (is_string($value)) {
234:                 $options[] = $value;
235:             }
236:         }
237:         return $options;
238:     }
239: 
240: /**
241:  * Main entry point to this shell
242:  *
243:  * @return void
244:  */
245:     public function main() {
246:         $this->out(__d('cake_console', 'CakePHP Test Shell'));
247:         $this->hr();
248: 
249:         $args = $this->_parseArgs();
250: 
251:         if (empty($args['case'])) {
252:             return $this->available();
253:         }
254: 
255:         $this->_run($args, $this->_runnerOptions());
256:     }
257: 
258: /**
259:  * Runs the test case from $runnerArgs
260:  *
261:  * @param array $runnerArgs list of arguments as obtained from _parseArgs()
262:  * @param array $options list of options as constructed by _runnerOptions()
263:  * @return void
264:  */
265:     protected function _run($runnerArgs, $options = array()) {
266:         restore_error_handler();
267:         restore_error_handler();
268: 
269:         $testCli = new CakeTestSuiteCommand('CakeTestLoader', $runnerArgs);
270:         $testCli->run($options);
271:     }
272: 
273: /**
274:  * Shows a list of available test cases and gives the option to run one of them
275:  *
276:  * @return void
277:  */
278:     public function available() {
279:         $params = $this->_parseArgs();
280:         $testCases = CakeTestLoader::generateTestList($params);
281:         $app = $params['app'];
282:         $plugin = $params['plugin'];
283: 
284:         $title = "Core Test Cases:";
285:         $category = 'core';
286:         if ($app) {
287:             $title = "App Test Cases:";
288:             $category = 'app';
289:         } elseif ($plugin) {
290:             $title = Inflector::humanize($plugin) . " Test Cases:";
291:             $category = $plugin;
292:         }
293: 
294:         if (empty($testCases)) {
295:             $this->out(__d('cake_console', "No test cases available \n\n"));
296:             return $this->out($this->OptionParser->help());
297:         }
298: 
299:         $this->out($title);
300:         $i = 1;
301:         $cases = array();
302:         foreach ($testCases as $testCaseFile => $testCase) {
303:             $case = str_replace('Test.php', '', $testCase);
304:             $this->out("[$i] $case");
305:             $cases[$i] = $case;
306:             $i++;
307:         }
308: 
309:         while ($choice = $this->in(__d('cake_console', 'What test case would you like to run?'), null, 'q')) {
310:             if (is_numeric($choice)  && isset($cases[$choice])) {
311:                 $this->args[0] = $category;
312:                 $this->args[1] = $cases[$choice];
313:                 $this->_run($this->_parseArgs(), $this->_runnerOptions());
314:                 break;
315:             }
316: 
317:             if (is_string($choice) && in_array($choice, $cases)) {
318:                 $this->args[0] = $category;
319:                 $this->args[1] = $choice;
320:                 $this->_run($this->_parseArgs(), $this->_runnerOptions());
321:                 break;
322:             }
323: 
324:             if ($choice == 'q') {
325:                 break;
326:             }
327:         }
328:     }
329: }
330: 
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