5
* Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* * Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in
17
* the documentation and/or other materials provided with the
20
* * Neither the name of Sebastian Bergmann nor the names of his
21
* contributors may be used to endorse or promote products derived
22
* from this software without specific prior written permission.
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
* POSSIBILITY OF SUCH DAMAGE.
39
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
41
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
42
* @link http://www.phpunit.de/
43
* @since File available since Release 3.3.0
46
require_once 'PHPUnit/Util/Filter.php';
47
require_once 'PHPUnit/Util/File.php';
48
require_once 'PHPUnit/Util/Template.php';
49
require_once 'PHPUnit/Util/Skeleton.php';
51
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
54
* Generator for class skeletons from test classes.
58
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
59
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
60
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
61
* @version Release: 3.4.14
62
* @link http://www.phpunit.de/
63
* @since Class available since Release 3.3.0
65
class PHPUnit_Util_Skeleton_Class extends PHPUnit_Util_Skeleton
70
* @param string $inClassName
71
* @param string $inSourceFile
72
* @param string $outClassName
73
* @param string $outSourceFile
74
* @throws RuntimeException
76
public function __construct($inClassName, $inSourceFile = '', $outClassName = '', $outSourceFile = '')
78
if (empty($inSourceFile)) {
79
$inSourceFile = $inClassName . '.php';
82
if (!is_file($inSourceFile)) {
83
throw new PHPUnit_Framework_Exception(
85
'"%s" could not be opened.',
92
if (empty($outClassName)) {
93
$outClassName = substr($inClassName, 0, strlen($inClassName) - 4);
96
if (empty($outSourceFile)) {
97
$outSourceFile = dirname($inSourceFile) . DIRECTORY_SEPARATOR .
98
$outClassName . '.php';
102
$inClassName, $inSourceFile, $outClassName, $outSourceFile
107
* Generates the class' source.
111
public function generate()
115
foreach ($this->findTestedMethods() as $method) {
116
$methodTemplate = new PHPUnit_Util_Template(
118
'%s%sTemplate%sMethod.tpl',
126
$methodTemplate->setVar(
128
'methodName' => $method,
132
$methods .= $methodTemplate->render();
135
$classTemplate = new PHPUnit_Util_Template(
137
'%s%sTemplate%sClass.tpl',
145
$classTemplate->setVar(
147
'className' => $this->outClassName['fullyQualifiedClassName'],
148
'methods' => $methods,
149
'date' => date('Y-m-d'),
150
'time' => date('H:i:s')
154
return $classTemplate->render();
158
* Returns the methods of the class under test
159
* that are called from the test methods.
163
protected function findTestedMethods()
165
$setUpVariables = array();
166
$testedMethods = array();
167
$classes = PHPUnit_Util_File::getClassesInFile(
170
$testMethods = $classes[$this->inClassName['fullyQualifiedClassName']]['methods'];
173
foreach ($testMethods as $name => $testMethod) {
174
if (strtolower($name) == 'setup') {
175
$setUpVariables = $this->findVariablesThatReferenceClass(
176
$testMethod['tokens']
183
foreach ($testMethods as $name => $testMethod) {
184
$argVariables = array();
186
if (strtolower($name) == 'setup') {
190
$start = strpos($testMethod['signature'], '(') + 1;
191
$end = strlen($testMethod['signature']) - $start - 1;
192
$args = substr($testMethod['signature'], $start, $end);
194
foreach (explode(',', $args) as $arg) {
195
list($type, $var) = explode(' ', $arg);
197
if ($type == $this->outClassName['fullyQualifiedClassName']) {
198
$argVariables[] = $var;
202
$variables = array_unique(
206
$this->findVariablesThatReferenceClass($testMethod['tokens'])
210
foreach ($testMethod['tokens'] as $i => $token) {
212
if (is_array($token) && $token[0] == T_DOUBLE_COLON &&
213
is_array($testMethod['tokens'][$i-1]) &&
214
$testMethod['tokens'][$i-1][0] == T_STRING &&
215
$testMethod['tokens'][$i-1][1] == $this->outClassName['fullyQualifiedClassName'] &&
216
is_array($testMethod['tokens'][$i+1]) &&
217
$testMethod['tokens'][$i+1][0] == T_STRING &&
218
$testMethod['tokens'][$i+2] == '(') {
219
$testedMethods[] = $testMethod['tokens'][$i+1][1];
222
// $this->object->method()
223
else if (is_array($token) && $token[0] == T_OBJECT_OPERATOR &&
224
in_array($this->findVariableName($testMethod['tokens'], $i), $variables) &&
225
is_array($testMethod['tokens'][$i+2]) &&
226
$testMethod['tokens'][$i+2][0] == T_OBJECT_OPERATOR &&
227
is_array($testMethod['tokens'][$i+3]) &&
228
$testMethod['tokens'][$i+3][0] == T_STRING &&
229
$testMethod['tokens'][$i+4] == '(') {
230
$testedMethods[] = $testMethod['tokens'][$i+3][1];
234
else if (is_array($token) && $token[0] == T_OBJECT_OPERATOR &&
235
in_array($this->findVariableName($testMethod['tokens'], $i), $variables) &&
236
is_array($testMethod['tokens'][$i+1]) &&
237
$testMethod['tokens'][$i+1][0] == T_STRING &&
238
$testMethod['tokens'][$i+2] == '(') {
239
$testedMethods[] = $testMethod['tokens'][$i+1][1];
244
$testedMethods = array_unique($testedMethods);
245
sort($testedMethods);
247
return $testedMethods;
251
* Returns the variables used in test methods
252
* that reference the class under test.
254
* @param array $tokens
257
protected function findVariablesThatReferenceClass(array $tokens)
260
$variables = array();
262
foreach ($tokens as $i => $token) {
263
if (is_string($token)) {
264
if (trim($token) == ';') {
271
list ($token, $value) = $token;
281
if ($value == $this->outClassName['fullyQualifiedClassName']) {
282
$variables[] = $this->findVariableName(
298
* Finds the variable name of the object for the method call
299
* that is currently being processed.
301
* @param array $tokens
302
* @param integer $start
305
protected function findVariableName(array $tokens, $start)
307
for ($i = $start - 1; $i >= 0; $i--) {
308
if (is_array($tokens[$i]) && $tokens[$i][0] == T_VARIABLE) {
309
$variable = $tokens[$i][1];
311
if (is_array($tokens[$i+1]) &&
312
$tokens[$i+1][0] == T_OBJECT_OPERATOR &&
313
$tokens[$i+2] != '(' &&
314
$tokens[$i+3] != '(') {
315
$variable .= '->' . $tokens[$i+2][1];
b'\\ No newline at end of file'