1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20:
21: require_once 'PHPUnit/TextUI/Command.php';
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: public function run(array $argv, $exit = true) {
64: $this->handleArguments($argv);
65:
66: $runner = $this->getRunner($this->arguments['loader']);
67:
68: if (is_object($this->arguments['test']) &&
69: $this->arguments['test'] instanceof PHPUnit_Framework_Test) {
70: $suite = $this->arguments['test'];
71: } else {
72: $suite = $runner->getTest(
73: $this->arguments['test'],
74: $this->arguments['testFile']
75: );
76: }
77:
78: if ($this->arguments['listGroups']) {
79: PHPUnit_TextUI_TestRunner::printVersionString();
80:
81: print "Available test group(s):\n";
82:
83: $groups = $suite->getGroups();
84: sort($groups);
85:
86: foreach ($groups as $group) {
87: print " - $group\n";
88: }
89:
90: exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
91: }
92:
93: unset($this->arguments['test']);
94: unset($this->arguments['testFile']);
95:
96: try {
97: $result = $runner->doRun($suite, $this->arguments);
98: } catch (PHPUnit_Framework_Exception $e) {
99: print $e->getMessage() . "\n";
100: }
101:
102: if ($exit) {
103: if (isset($result) && $result->wasSuccessful()) {
104: exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
105: } elseif (!isset($result) || $result->errorCount() > 0) {
106: exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
107: }
108: exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
109: }
110: }
111:
112: 113: 114: 115: 116: 117:
118: public function getRunner($loader) {
119: return new CakeTestRunner($loader, $this->_params);
120: }
121:
122: 123: 124: 125: 126: 127:
128: public function handleFixture($class) {
129: $this->arguments['fixtureManager'] = $class;
130: }
131:
132: 133: 134: 135: 136: 137:
138: public function handleReporter($reporter) {
139: $object = null;
140:
141: $reporter = ucwords($reporter);
142: $coreClass = 'Cake' . $reporter . 'Reporter';
143: App::uses($coreClass, 'TestSuite/Reporter');
144:
145: $appClass = $reporter . 'Reporter';
146: App::uses($appClass, 'TestSuite/Reporter');
147:
148: if (!class_exists($appClass)) {
149: $object = new $coreClass(null, $this->_params);
150: } else {
151: $object = new $appClass(null, $this->_params);
152: }
153: return $this->arguments['printer'] = $object;
154: }
155:
156: }
157: