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:
86: eval(str_replace(array('<?php', '?>'), '', $result['code']));
87:
88: $suite = new PHPUnit_Framework_TestSuite(
89: $this->arguments['test'] . 'Test'
90: );
91: }
92: }
93:
94: if ($this->arguments['listGroups']) {
95: PHPUnit_TextUI_TestRunner::printVersionString();
96:
97: print "Available test group(s):\n";
98:
99: $groups = $suite->getGroups();
100: sort($groups);
101:
102: foreach ($groups as $group) {
103: print " - $group\n";
104: }
105:
106: exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
107: }
108:
109: unset($this->arguments['test']);
110: unset($this->arguments['testFile']);
111:
112: try {
113: $result = $runner->doRun($suite, $this->arguments);
114: } catch (PHPUnit_Framework_Exception $e) {
115: print $e->getMessage() . "\n";
116: }
117:
118: if ($exit) {
119: if (isset($result) && $result->wasSuccessful()) {
120: exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
121: } elseif (!isset($result) || $result->errorCount() > 0) {
122: exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
123: } else {
124: exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
125: }
126: }
127: }
128:
129: 130: 131: 132: 133: 134:
135: public function getRunner($loader) {
136: return new CakeTestRunner($loader, $this->_params);
137: }
138:
139: 140: 141: 142: 143: 144:
145: public function handleFixture($class) {
146: $this->arguments['fixtureManager'] = $class;
147: }
148:
149: 150: 151: 152: 153:
154: public function handleReporter($reporter) {
155: $object = null;
156:
157: $type = strtolower($reporter);
158: $reporter = ucwords($reporter);
159: $coreClass = 'Cake' . $reporter . 'Reporter';
160: App::uses($coreClass, 'TestSuite/Reporter');
161:
162: $appClass = $reporter . 'Reporter';
163: App::uses($appClass, 'TestSuite/Reporter');
164:
165: if (!class_exists($appClass)) {
166: $object = new $coreClass(null, $this->_params);
167: } else {
168: $object = new $appClass(null, $this->_params);
169: }
170: return $this->arguments['printer'] = $object;
171: }
172:
173: }
174: