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: public function __construct($loader, $params = array()) {
42: if ($loader && !class_exists($loader)) {
43: throw new MissingTestLoaderException(array('class' => $loader));
44: }
45: $this->arguments['loader'] = $loader;
46: $this->arguments['test'] = $params['case'];
47: $this->arguments['testFile'] = $params;
48: $this->_params = $params;
49:
50: $this->longOptions['fixture='] = 'handleFixture';
51: $this->longOptions['output='] = 'handleReporter';
52: }
53:
54: 55: 56: 57: 58: 59:
60: public function run(array $argv, $exit = true) {
61: $this->handleArguments($argv);
62:
63: $runner = $this->getRunner($this->arguments['loader']);
64:
65: if (is_object($this->arguments['test']) &&
66: $this->arguments['test'] instanceof PHPUnit_Framework_Test) {
67: $suite = $this->arguments['test'];
68: } else {
69: $suite = $runner->getTest(
70: $this->arguments['test'],
71: $this->arguments['testFile']
72: );
73: }
74:
75: if (count($suite) == 0) {
76: $skeleton = new PHPUnit_Util_Skeleton_Test(
77: $suite->getName(),
78: $this->arguments['testFile']
79: );
80:
81: $result = $skeleton->generate(true);
82:
83: if (!$result['incomplete']) {
84: eval(str_replace(array('<?php', '?>'), '', $result['code']));
85: $suite = new PHPUnit_Framework_TestSuite(
86: $this->arguments['test'] . 'Test'
87: );
88: }
89: }
90:
91: if ($this->arguments['listGroups']) {
92: PHPUnit_TextUI_TestRunner::printVersionString();
93:
94: print "Available test group(s):\n";
95:
96: $groups = $suite->getGroups();
97: sort($groups);
98:
99: foreach ($groups as $group) {
100: print " - $group\n";
101: }
102:
103: exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
104: }
105:
106: unset($this->arguments['test']);
107: unset($this->arguments['testFile']);
108:
109: try {
110: $result = $runner->doRun($suite, $this->arguments);
111: } catch (PHPUnit_Framework_Exception $e) {
112: print $e->getMessage() . "\n";
113: }
114:
115: if ($exit) {
116: if (isset($result) && $result->wasSuccessful()) {
117: exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
118: }
119:
120: else if (!isset($result) || $result->errorCount() > 0) {
121: exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
122: }
123:
124: else {
125: exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
126: }
127: }
128: }
129:
130: 131: 132: 133: 134: 135:
136: public function getRunner($loader) {
137: return new CakeTestRunner($loader, $this->_params);
138: }
139:
140: 141: 142: 143: 144: 145:
146: public function handleFixture($class) {
147: $this->arguments['fixtureManager'] = $class;
148: }
149:
150: 151: 152: 153: 154:
155: public function handleReporter($reporter) {
156: $object = null;
157:
158: $type = strtolower($reporter);
159: $reporter = ucwords($reporter);
160: $coreClass = 'Cake' . $reporter . 'Reporter';
161: App::uses($coreClass, 'TestSuite/Reporter');
162:
163: $appClass = $reporter . 'Reporter';
164: App::uses($appClass, 'TestSuite/Reporter');
165:
166: if (!class_exists($appClass)) {
167: $object = new $coreClass(null, $this->_params);
168: } else {
169: $object = new $appClass(null, $this->_params);
170: }
171: return $this->arguments['printer'] = $object;
172: }
173: }
174: