1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 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: 32: 33: 34:
35: class CakeTestSuiteCommand extends PHPUnit_TextUI_Command {
36:
37: 38: 39: 40: 41: 42: 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: 59: 60: 61: 62: 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: 115: 116: 117: 118:
119: public function getRunner($loader) {
120: return new CakeTestRunner($loader, $this->_params);
121: }
122:
123: 124: 125: 126: 127: 128:
129: public function handleFixture($class) {
130: $this->arguments['fixtureManager'] = $class;
131: }
132:
133: 134: 135: 136: 137: 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: }
158: