1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18:
19: require_once 'PHPUnit/TextUI/Command.php';
20:
21: App::uses('CakeTestRunner', 'TestSuite');
22: App::uses('CakeTestLoader', 'TestSuite');
23: App::uses('CakeTestSuite', 'TestSuite');
24: App::uses('CakeTestCase', 'TestSuite');
25: App::uses('ControllerTestCase', 'TestSuite');
26: App::uses('CakeTestModel', 'TestSuite/Fixture');
27:
28: 29: 30: 31: 32:
33: class CakeTestSuiteCommand extends PHPUnit_TextUI_Command {
34:
35: 36: 37: 38: 39: 40: 41:
42: public function __construct($loader, $params = array()) {
43: if ($loader && !class_exists($loader)) {
44: throw new MissingTestLoaderException(array('class' => $loader));
45: }
46: $this->arguments['loader'] = $loader;
47: $this->arguments['test'] = $params['case'];
48: $this->arguments['testFile'] = $params;
49: $this->_params = $params;
50:
51: $this->longOptions['fixture='] = 'handleFixture';
52: $this->longOptions['output='] = 'handleReporter';
53: }
54:
55: 56: 57: 58: 59: 60: 61:
62: public function run(array $argv, $exit = true) {
63: $this->handleArguments($argv);
64:
65: $runner = $this->getRunner($this->arguments['loader']);
66:
67: if (is_object($this->arguments['test']) &&
68: $this->arguments['test'] instanceof PHPUnit_Framework_Test) {
69: $suite = $this->arguments['test'];
70: } else {
71: $suite = $runner->getTest(
72: $this->arguments['test'],
73: $this->arguments['testFile']
74: );
75: }
76:
77: if ($this->arguments['listGroups']) {
78: PHPUnit_TextUI_TestRunner::printVersionString();
79:
80: print "Available test group(s):\n";
81:
82: $groups = $suite->getGroups();
83: sort($groups);
84:
85: foreach ($groups as $group) {
86: print " - $group\n";
87: }
88:
89: exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
90: }
91:
92: unset($this->arguments['test']);
93: unset($this->arguments['testFile']);
94:
95: try {
96: $result = $runner->doRun($suite, $this->arguments);
97: } catch (PHPUnit_Framework_Exception $e) {
98: print $e->getMessage() . "\n";
99: }
100:
101: if ($exit) {
102: if (isset($result) && $result->wasSuccessful()) {
103: exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
104: } elseif (!isset($result) || $result->errorCount() > 0) {
105: exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
106: }
107: exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
108: }
109: }
110:
111: 112: 113: 114: 115: 116:
117: public function getRunner($loader) {
118: return new CakeTestRunner($loader, $this->_params);
119: }
120:
121: 122: 123: 124: 125: 126:
127: public function handleFixture($class) {
128: $this->arguments['fixtureManager'] = $class;
129: }
130:
131: 132: 133: 134: 135: 136:
137: public function handleReporter($reporter) {
138: $object = null;
139:
140: $reporter = ucwords($reporter);
141: $coreClass = 'Cake' . $reporter . 'Reporter';
142: App::uses($coreClass, 'TestSuite/Reporter');
143:
144: $appClass = $reporter . 'Reporter';
145: App::uses($appClass, 'TestSuite/Reporter');
146:
147: if (!class_exists($appClass)) {
148: $object = new $coreClass(null, $this->_params);
149: } else {
150: $object = new $appClass(null, $this->_params);
151: }
152: return $this->arguments['printer'] = $object;
153: }
154:
155: }
156: