pictcode / lib / Cake / TestSuite / CakeTestSuiteCommand.php @ master
履歴 | 表示 | アノテート | ダウンロード (4.269 KB)
| 1 | 635eef61 | spyder1211 | <?php
 | 
      
|---|---|---|---|
| 2 | /**
 | 
      ||
| 3 |  * TestRunner for CakePHP Test suite.
 | 
      ||
| 4 |  *
 | 
      ||
| 5 |  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 | 
      ||
| 6 |  * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
 | 
      ||
| 13 |  * @link          http://cakephp.org CakePHP(tm) Project
 | 
      ||
| 14 |  * @package       Cake.TestSuite
 | 
      ||
| 15 |  * @since         CakePHP(tm) v 2.0
 | 
      ||
| 16 |  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 | 
      ||
| 17 |  */
 | 
      ||
| 18 | |||
| 19 | if (!defined('__PHPUNIT_PHAR__')) {  | 
      ||
| 20 | require_once 'PHPUnit/TextUI/Command.php';  | 
      ||
| 21 | }  | 
      ||
| 22 | |||
| 23 | App::uses('CakeTestRunner', 'TestSuite');  | 
      ||
| 24 | App::uses('CakeTestLoader', 'TestSuite');  | 
      ||
| 25 | App::uses('CakeTestSuite', 'TestSuite');  | 
      ||
| 26 | App::uses('CakeTestCase', 'TestSuite');  | 
      ||
| 27 | App::uses('ControllerTestCase', 'TestSuite');  | 
      ||
| 28 | App::uses('CakeTestModel', 'TestSuite/Fixture');  | 
      ||
| 29 | |||
| 30 | /**
 | 
      ||
| 31 |  * Class to customize loading of test suites from CLI
 | 
      ||
| 32 |  *
 | 
      ||
| 33 |  * @package       Cake.TestSuite
 | 
      ||
| 34 |  */
 | 
      ||
| 35 | class CakeTestSuiteCommand extends PHPUnit_TextUI_Command {  | 
      ||
| 36 | |||
| 37 | /**
 | 
      ||
| 38 |  * Construct method
 | 
      ||
| 39 |  *
 | 
      ||
| 40 |  * @param mixed $loader The loader instance to use.
 | 
      ||
| 41 |  * @param array $params list of options to be used for this run
 | 
      ||
| 42 |  * @throws MissingTestLoaderException When a loader class could not be found.
 | 
      ||
| 43 |  */
 | 
      ||
| 44 | public function __construct($loader, $params = array()) {  | 
      ||
| 45 | if ($loader && !class_exists($loader)) {  | 
      ||
| 46 | throw new MissingTestLoaderException(array('class' => $loader));  | 
      ||
| 47 | }  | 
      ||
| 48 | $this->arguments['loader'] = $loader;  | 
      ||
| 49 | $this->arguments['test'] = $params['case'];  | 
      ||
| 50 | $this->arguments['testFile'] = $params;  | 
      ||
| 51 | $this->_params = $params;  | 
      ||
| 52 | |||
| 53 | $this->longOptions['fixture='] = 'handleFixture';  | 
      ||
| 54 | $this->longOptions['output='] = 'handleReporter';  | 
      ||
| 55 | }  | 
      ||
| 56 | |||
| 57 | /**
 | 
      ||
| 58 |  * Ugly hack to get around PHPUnit having a hard coded class name for the Runner. :(
 | 
      ||
| 59 |  *
 | 
      ||
| 60 |  * @param array $argv The command arguments
 | 
      ||
| 61 |  * @param bool $exit The exit mode.
 | 
      ||
| 62 |  * @return void
 | 
      ||
| 63 |  */
 | 
      ||
| 64 | public function run(array $argv, $exit = true) {  | 
      ||
| 65 | $this->handleArguments($argv);  | 
      ||
| 66 | |||
| 67 | $runner = $this->getRunner($this->arguments['loader']);  | 
      ||
| 68 | |||
| 69 | if (is_object($this->arguments['test']) &&  | 
      ||
| 70 | $this->arguments['test'] instanceof PHPUnit_Framework_Test) {  | 
      ||
| 71 | $suite = $this->arguments['test'];  | 
      ||
| 72 |                 } else {
 | 
      ||
| 73 | $suite = $runner->getTest(  | 
      ||
| 74 | $this->arguments['test'],  | 
      ||
| 75 | $this->arguments['testFile']  | 
      ||
| 76 | );  | 
      ||
| 77 | }  | 
      ||
| 78 | |||
| 79 | if ($this->arguments['listGroups']) {  | 
      ||
| 80 |                         PHPUnit_TextUI_TestRunner::printVersionString();
 | 
      ||
| 81 | |||
| 82 | print "Available test group(s):\n";  | 
      ||
| 83 | |||
| 84 | $groups = $suite->getGroups();  | 
      ||
| 85 | sort($groups);  | 
      ||
| 86 | |||
| 87 | foreach ($groups as $group) {  | 
      ||
| 88 | print " - $group\n";  | 
      ||
| 89 | }  | 
      ||
| 90 | |||
| 91 | exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);  | 
      ||
| 92 | }  | 
      ||
| 93 | |||
| 94 | unset($this->arguments['test']);  | 
      ||
| 95 | unset($this->arguments['testFile']);  | 
      ||
| 96 | |||
| 97 |                 try {
 | 
      ||
| 98 | $result = $runner->doRun($suite, $this->arguments);  | 
      ||
| 99 | } catch (PHPUnit_Framework_Exception $e) {  | 
      ||
| 100 | print $e->getMessage() . "\n";  | 
      ||
| 101 | }  | 
      ||
| 102 | |||
| 103 | if ($exit) {  | 
      ||
| 104 | if (isset($result) && $result->wasSuccessful()) {  | 
      ||
| 105 | exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);  | 
      ||
| 106 | } elseif (!isset($result) || $result->errorCount() > 0) {  | 
      ||
| 107 | exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);  | 
      ||
| 108 | }  | 
      ||
| 109 | exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);  | 
      ||
| 110 | }  | 
      ||
| 111 | }  | 
      ||
| 112 | |||
| 113 | /**
 | 
      ||
| 114 |  * Create a runner for the command.
 | 
      ||
| 115 |  *
 | 
      ||
| 116 |  * @param mixed $loader The loader to be used for the test run.
 | 
      ||
| 117 |  * @return CakeTestRunner
 | 
      ||
| 118 |  */
 | 
      ||
| 119 | public function getRunner($loader) {  | 
      ||
| 120 | return new CakeTestRunner($loader, $this->_params);  | 
      ||
| 121 | }  | 
      ||
| 122 | |||
| 123 | /**
 | 
      ||
| 124 |  * Handler for customizing the FixtureManager class/
 | 
      ||
| 125 |  *
 | 
      ||
| 126 |  * @param string $class Name of the class that will be the fixture manager
 | 
      ||
| 127 |  * @return void
 | 
      ||
| 128 |  */
 | 
      ||
| 129 | public function handleFixture($class) {  | 
      ||
| 130 | $this->arguments['fixtureManager'] = $class;  | 
      ||
| 131 | }  | 
      ||
| 132 | |||
| 133 | /**
 | 
      ||
| 134 |  * Handles output flag used to change printing on webrunner.
 | 
      ||
| 135 |  *
 | 
      ||
| 136 |  * @param string $reporter The reporter class to use.
 | 
      ||
| 137 |  * @return void
 | 
      ||
| 138 |  */
 | 
      ||
| 139 | public function handleReporter($reporter) {  | 
      ||
| 140 | $object = null;  | 
      ||
| 141 | |||
| 142 | $reporter = ucwords($reporter);  | 
      ||
| 143 | $coreClass = 'Cake' . $reporter . 'Reporter';  | 
      ||
| 144 | App::uses($coreClass, 'TestSuite/Reporter');  | 
      ||
| 145 | |||
| 146 | $appClass = $reporter . 'Reporter';  | 
      ||
| 147 | App::uses($appClass, 'TestSuite/Reporter');  | 
      ||
| 148 | |||
| 149 | if (!class_exists($appClass)) {  | 
      ||
| 150 | $object = new $coreClass(null, $this->_params);  | 
      ||
| 151 |                 } else {
 | 
      ||
| 152 | $object = new $appClass(null, $this->_params);  | 
      ||
| 153 | }  | 
      ||
| 154 | return $this->arguments['printer'] = $object;  | 
      ||
| 155 | }  | 
      ||
| 156 | |||
| 157 | }  |