Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 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 The loader instance to use.
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 The command arguments
59
 * @param bool $exit The exit mode.
60
 * @return void
61
 */
62
	public function run(array $argv, $exit = true) {
63
		$this->handleArguments($argv);
64
 
65
		$runner = $this->getRunner($this->arguments['loader']);
66
 
67
		if (is_object($this->arguments['test']) &&
68
			$this->arguments['test'] instanceof PHPUnit_Framework_Test) {
69
			$suite = $this->arguments['test'];
70
		} else {
71
			$suite = $runner->getTest(
72
				$this->arguments['test'],
73
				$this->arguments['testFile']
74
			);
75
		}
76
 
77
		if ($this->arguments['listGroups']) {
78
			PHPUnit_TextUI_TestRunner::printVersionString();
79
 
80
			print "Available test group(s):\n";
81
 
82
			$groups = $suite->getGroups();
83
			sort($groups);
84
 
85
			foreach ($groups as $group) {
86
				print " - $group\n";
87
			}
88
 
89
			exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
90
		}
91
 
92
		unset($this->arguments['test']);
93
		unset($this->arguments['testFile']);
94
 
95
		try {
96
			$result = $runner->doRun($suite, $this->arguments);
97
		} catch (PHPUnit_Framework_Exception $e) {
98
			print $e->getMessage() . "\n";
99
		}
100
 
101
		if ($exit) {
102
			if (isset($result) && $result->wasSuccessful()) {
103
				exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
104
			} elseif (!isset($result) || $result->errorCount() > 0) {
105
				exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
106
			}
107
			exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
108
		}
109
	}
110
 
111
/**
112
 * Create a runner for the command.
113
 *
114
 * @param mixed $loader The loader to be used for the test run.
115
 * @return CakeTestRunner
116
 */
117
	public function getRunner($loader) {
118
		return new CakeTestRunner($loader, $this->_params);
119
	}
120
 
121
/**
122
 * Handler for customizing the FixtureManager class/
123
 *
124
 * @param string $class Name of the class that will be the fixture manager
125
 * @return void
126
 */
127
	public function handleFixture($class) {
128
		$this->arguments['fixtureManager'] = $class;
129
	}
130
 
131
/**
132
 * Handles output flag used to change printing on webrunner.
133
 *
134
 * @param string $reporter The reporter class to use.
135
 * @return void
136
 */
137
	public function handleReporter($reporter) {
138
		$object = null;
139
 
140
		$reporter = ucwords($reporter);
141
		$coreClass = 'Cake' . $reporter . 'Reporter';
142
		App::uses($coreClass, 'TestSuite/Reporter');
143
 
144
		$appClass = $reporter . 'Reporter';
145
		App::uses($appClass, 'TestSuite/Reporter');
146
 
147
		if (!class_exists($appClass)) {
148
			$object = new $coreClass(null, $this->_params);
149
		} else {
150
			$object = new $appClass(null, $this->_params);
151
		}
152
		return $this->arguments['printer'] = $object;
153
	}
154
 
155
}