5
* Copyright (c) 2002-2010, Sebastian Bergmann <sebastian@phpunit.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.
38
* @subpackage Util_Skeleton
39
* @author Sebastian Bergmann <sebastian@phpunit.de>
40
* @copyright 2002-2010 Sebastian Bergmann <sebastian@phpunit.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 'Text/Template.php';
49
* Generator for class skeletons from test classes.
52
* @subpackage Util_Skeleton
53
* @author Sebastian Bergmann <sebastian@phpunit.de>
54
* @copyright 2002-2010 Sebastian Bergmann <sebastian@phpunit.de>
55
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
56
* @version Release: 3.5.5
57
* @link http://www.phpunit.de/
58
* @since Class available since Release 3.3.0
60
class PHPUnit_Util_Skeleton_Class extends PHPUnit_Util_Skeleton
65
* @param string $inClassName
66
* @param string $inSourceFile
67
* @param string $outClassName
68
* @param string $outSourceFile
69
* @throws RuntimeException
71
public function __construct($inClassName, $inSourceFile = '', $outClassName = '', $outSourceFile = '')
73
if (empty($inSourceFile)) {
74
$inSourceFile = $inClassName . '.php';
77
if (!is_file($inSourceFile)) {
78
throw new PHPUnit_Framework_Exception(
80
'"%s" could not be opened.',
87
if (empty($outClassName)) {
88
$outClassName = substr($inClassName, 0, strlen($inClassName) - 4);
91
if (empty($outSourceFile)) {
92
$outSourceFile = dirname($inSourceFile) . DIRECTORY_SEPARATOR .
93
$outClassName . '.php';
97
$inClassName, $inSourceFile, $outClassName, $outSourceFile
102
* Generates the class' source.
106
public function generate()
110
foreach ($this->findTestedMethods() as $method) {
111
$methodTemplate = new Text_Template(
113
'%s%sTemplate%sMethod.tpl',
121
$methodTemplate->setVar(
123
'methodName' => $method,
127
$methods .= $methodTemplate->render();
130
$classTemplate = new Text_Template(
132
'%s%sTemplate%sClass.tpl',
140
$classTemplate->setVar(
142
'className' => $this->outClassName['fullyQualifiedClassName'],
143
'methods' => $methods,
144
'date' => date('Y-m-d'),
145
'time' => date('H:i:s')
149
return $classTemplate->render();
153
* Returns the methods of the class under test
154
* that are called from the test methods.
158
protected function findTestedMethods()
160
$setUpVariables = array();
161
$testedMethods = array();
162
$classes = PHPUnit_Util_File::getClassesInFile(
165
$testMethods = $classes[$this->inClassName['fullyQualifiedClassName']]['methods'];
168
foreach ($testMethods as $name => $testMethod) {
169
if (strtolower($name) == 'setup') {
170
$setUpVariables = $this->findVariablesThatReferenceClass(
171
$testMethod['tokens']
178
foreach ($testMethods as $name => $testMethod) {
179
$argVariables = array();
181
if (strtolower($name) == 'setup') {
185
$start = strpos($testMethod['signature'], '(') + 1;
186
$end = strlen($testMethod['signature']) - $start - 1;
187
$args = substr($testMethod['signature'], $start, $end);
189
foreach (explode(',', $args) as $arg) {
190
$arg = explode(' ', trim($arg));
192
if (count($arg) == 2) {
200
if ($type == $this->outClassName['fullyQualifiedClassName']) {
201
$argVariables[] = $var;
205
$variables = array_unique(
209
$this->findVariablesThatReferenceClass($testMethod['tokens'])
213
foreach ($testMethod['tokens'] as $i => $token) {
215
if (is_array($token) && $token[0] == T_DOUBLE_COLON &&
216
is_array($testMethod['tokens'][$i-1]) &&
217
$testMethod['tokens'][$i-1][0] == T_STRING &&
218
$testMethod['tokens'][$i-1][1] == $this->outClassName['fullyQualifiedClassName'] &&
219
is_array($testMethod['tokens'][$i+1]) &&
220
$testMethod['tokens'][$i+1][0] == T_STRING &&
221
$testMethod['tokens'][$i+2] == '(') {
222
$testedMethods[] = $testMethod['tokens'][$i+1][1];
225
// $this->object->method()
226
else if (is_array($token) && $token[0] == T_OBJECT_OPERATOR &&
227
in_array($this->findVariableName($testMethod['tokens'], $i), $variables) &&
228
is_array($testMethod['tokens'][$i+2]) &&
229
$testMethod['tokens'][$i+2][0] == T_OBJECT_OPERATOR &&
230
is_array($testMethod['tokens'][$i+3]) &&
231
$testMethod['tokens'][$i+3][0] == T_STRING &&
232
$testMethod['tokens'][$i+4] == '(') {
233
$testedMethods[] = $testMethod['tokens'][$i+3][1];
237
else if (is_array($token) && $token[0] == T_OBJECT_OPERATOR &&
238
in_array($this->findVariableName($testMethod['tokens'], $i), $variables) &&
239
is_array($testMethod['tokens'][$i+1]) &&
240
$testMethod['tokens'][$i+1][0] == T_STRING &&
241
$testMethod['tokens'][$i+2] == '(') {
242
$testedMethods[] = $testMethod['tokens'][$i+1][1];
247
$testedMethods = array_unique($testedMethods);
248
sort($testedMethods);
250
return $testedMethods;
254
* Returns the variables used in test methods
255
* that reference the class under test.
257
* @param array $tokens
260
protected function findVariablesThatReferenceClass(array $tokens)
263
$variables = array();
265
foreach ($tokens as $i => $token) {
266
if (is_string($token)) {
267
if (trim($token) == ';') {
274
list ($token, $value) = $token;
284
if ($value == $this->outClassName['fullyQualifiedClassName']) {
285
$variables[] = $this->findVariableName(
301
* Finds the variable name of the object for the method call
302
* that is currently being processed.
304
* @param array $tokens
305
* @param integer $start
308
protected function findVariableName(array $tokens, $start)
310
for ($i = $start - 1; $i >= 0; $i--) {
311
if (is_array($tokens[$i]) && $tokens[$i][0] == T_VARIABLE) {
312
$variable = $tokens[$i][1];
314
if (is_array($tokens[$i+1]) &&
315
$tokens[$i+1][0] == T_OBJECT_OPERATOR &&
316
$tokens[$i+2] != '(' &&
317
$tokens[$i+3] != '(') {
318
$variable .= '->' . $tokens[$i+2][1];