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