1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18:
19: if (!class_exists('PHPUnit_TextUI_Command')) {
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, false);
99: } catch (PHPUnit_Framework_Exception $e) {
100: print $e->getMessage() . "\n";
101: }
102:
103: if ($exit) {
104: if (!isset($result) || $result->errorCount() > 0) {
105: exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
106: }
107: if ($result->failureCount() > 0) {
108: exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
109: }
110:
111:
112: exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
113: }
114: }
115:
116: 117: 118: 119: 120: 121:
122: public function getRunner($loader) {
123: return new CakeTestRunner($loader, $this->_params);
124: }
125:
126: 127: 128: 129: 130: 131:
132: public function handleFixture($class) {
133: $this->arguments['fixtureManager'] = $class;
134: }
135:
136: 137: 138: 139: 140: 141:
142: public function handleReporter($reporter) {
143: $object = null;
144:
145: $reporter = ucwords($reporter);
146: $coreClass = 'Cake' . $reporter . 'Reporter';
147: App::uses($coreClass, 'TestSuite/Reporter');
148:
149: $appClass = $reporter . 'Reporter';
150: App::uses($appClass, 'TestSuite/Reporter');
151:
152: if (!class_exists($appClass)) {
153: $object = new $coreClass(null, $this->_params);
154: } else {
155: $object = new $appClass(null, $this->_params);
156: }
157: return $this->arguments['printer'] = $object;
158: }
159:
160: }
161: