Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * TestRunner for CakePHP Test suite.
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.TestSuite
15
 * @since         CakePHP(tm) v 2.0
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
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
 * Class to customize loading of test suites from CLI
30
 *
31
 * @package       Cake.TestSuite
32
 */
33
class CakeTestSuiteCommand extends PHPUnit_TextUI_Command {
34
 
35
/**
36
 * Construct method
37
 *
38
 * @param mixed $loader
39
 * @param array $params list of options to be used for this run
40
 * @throws MissingTestLoaderException When a loader class could not be found.
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
 * Ugly hack to get around PHPUnit having a hard coded class name for the Runner. :(
57
 *
58
 * @param array   $argv
59
 * @param boolean $exit
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 ($this->arguments['listGroups']) {
77
			PHPUnit_TextUI_TestRunner::printVersionString();
78
 
79
			print "Available test group(s):\n";
80
 
81
			$groups = $suite->getGroups();
82
			sort($groups);
83
 
84
			foreach ($groups as $group) {
85
				print " - $group\n";
86
			}
87
 
88
			exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
89
		}
90
 
91
		unset($this->arguments['test']);
92
		unset($this->arguments['testFile']);
93
 
94
		try {
95
			$result = $runner->doRun($suite, $this->arguments);
96
		} catch (PHPUnit_Framework_Exception $e) {
97
			print $e->getMessage() . "\n";
98
		}
99
 
100
		if ($exit) {
101
			if (isset($result) && $result->wasSuccessful()) {
102
				exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
103
			} elseif (!isset($result) || $result->errorCount() > 0) {
104
				exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
105
			}
106
			exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
107
		}
108
	}
109
 
110
/**
111
 * Create a runner for the command.
112
 *
113
 * @param mixed $loader The loader to be used for the test run.
114
 * @return CakeTestRunner
115
 */
116
	public function getRunner($loader) {
117
		return new CakeTestRunner($loader, $this->_params);
118
	}
119
 
120
/**
121
 * Handler for customizing the FixtureManager class/
122
 *
123
 * @param string $class Name of the class that will be the fixture manager
124
 * @return void
125
 */
126
	public function handleFixture($class) {
127
		$this->arguments['fixtureManager'] = $class;
128
	}
129
 
130
/**
131
 * Handles output flag used to change printing on webrunner.
132
 *
133
 * @param string $reporter
134
 * @return void
135
 */
136
	public function handleReporter($reporter) {
137
		$object = null;
138
 
139
		$reporter = ucwords($reporter);
140
		$coreClass = 'Cake' . $reporter . 'Reporter';
141
		App::uses($coreClass, 'TestSuite/Reporter');
142
 
143
		$appClass = $reporter . 'Reporter';
144
		App::uses($appClass, 'TestSuite/Reporter');
145
 
146
		if (!class_exists($appClass)) {
147
			$object = new $coreClass(null, $this->_params);
148
		} else {
149
			$object = new $appClass(null, $this->_params);
150
		}
151
		return $this->arguments['printer'] = $object;
152
	}
153
 
154
}