~ubuntu-branches/ubuntu/natty/phpunit/natty

« back to all changes in this revision

Viewing changes to PHPUnit-3.4.14/PHPUnit/Util/Skeleton/Class.php

  • Committer: Package Import Robot
  • Author(s): Ivan Borzenkov
  • Date: 2010-12-11 18:19:39 UTC
  • mfrom: (0.11.1) (1.5.3) (12.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20101211181939-8650nbu08hf2z9v1
Tags: 3.5.5-2
fix doc-base-file-references-missing-file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * PHPUnit
4
 
 *
5
 
 * Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6
 
 * All rights reserved.
7
 
 *
8
 
 * Redistribution and use in source and binary forms, with or without
9
 
 * modification, are permitted provided that the following conditions
10
 
 * are met:
11
 
 *
12
 
 *   * Redistributions of source code must retain the above copyright
13
 
 *     notice, this list of conditions and the following disclaimer.
14
 
 *
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
18
 
 *     distribution.
19
 
 *
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.
23
 
 *
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.
36
 
 *
37
 
 * @category   Testing
38
 
 * @package    PHPUnit
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
44
 
 */
45
 
 
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';
50
 
 
51
 
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
52
 
 
53
 
/**
54
 
 * Generator for class skeletons from test classes.
55
 
 *
56
 
 * @category   Testing
57
 
 * @package    PHPUnit
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
64
 
 */
65
 
class PHPUnit_Util_Skeleton_Class extends PHPUnit_Util_Skeleton
66
 
{
67
 
    /**
68
 
     * Constructor.
69
 
     *
70
 
     * @param string $inClassName
71
 
     * @param string $inSourceFile
72
 
     * @param string $outClassName
73
 
     * @param string $outSourceFile
74
 
     * @throws RuntimeException
75
 
     */
76
 
    public function __construct($inClassName, $inSourceFile = '', $outClassName = '', $outSourceFile = '')
77
 
    {
78
 
        if (empty($inSourceFile)) {
79
 
            $inSourceFile = $inClassName . '.php';
80
 
        }
81
 
 
82
 
        if (!is_file($inSourceFile)) {
83
 
            throw new PHPUnit_Framework_Exception(
84
 
              sprintf(
85
 
                '"%s" could not be opened.',
86
 
 
87
 
                $inSourceFile
88
 
              )
89
 
            );
90
 
        }
91
 
 
92
 
        if (empty($outClassName)) {
93
 
            $outClassName = substr($inClassName, 0, strlen($inClassName) - 4);
94
 
        }
95
 
 
96
 
        if (empty($outSourceFile)) {
97
 
            $outSourceFile = dirname($inSourceFile) . DIRECTORY_SEPARATOR .
98
 
                             $outClassName . '.php';
99
 
        }
100
 
 
101
 
        parent::__construct(
102
 
          $inClassName, $inSourceFile, $outClassName, $outSourceFile
103
 
        );
104
 
    }
105
 
 
106
 
    /**
107
 
     * Generates the class' source.
108
 
     *
109
 
     * @return mixed
110
 
     */
111
 
    public function generate()
112
 
    {
113
 
        $methods = '';
114
 
 
115
 
        foreach ($this->findTestedMethods() as $method) {
116
 
            $methodTemplate = new PHPUnit_Util_Template(
117
 
              sprintf(
118
 
                '%s%sTemplate%sMethod.tpl',
119
 
 
120
 
                dirname(__FILE__),
121
 
                DIRECTORY_SEPARATOR,
122
 
                DIRECTORY_SEPARATOR
123
 
              )
124
 
            );
125
 
 
126
 
            $methodTemplate->setVar(
127
 
              array(
128
 
                'methodName' => $method,
129
 
              )
130
 
            );
131
 
 
132
 
            $methods .= $methodTemplate->render();
133
 
        }
134
 
 
135
 
        $classTemplate = new PHPUnit_Util_Template(
136
 
          sprintf(
137
 
            '%s%sTemplate%sClass.tpl',
138
 
 
139
 
            dirname(__FILE__),
140
 
            DIRECTORY_SEPARATOR,
141
 
            DIRECTORY_SEPARATOR
142
 
          )
143
 
        );
144
 
 
145
 
        $classTemplate->setVar(
146
 
          array(
147
 
            'className' => $this->outClassName['fullyQualifiedClassName'],
148
 
            'methods'   => $methods,
149
 
            'date'      => date('Y-m-d'),
150
 
            'time'      => date('H:i:s')
151
 
          )
152
 
        );
153
 
 
154
 
        return $classTemplate->render();
155
 
    }
156
 
 
157
 
    /**
158
 
     * Returns the methods of the class under test
159
 
     * that are called from the test methods.
160
 
     *
161
 
     * @return array
162
 
     */
163
 
    protected function findTestedMethods()
164
 
    {
165
 
        $setUpVariables = array();
166
 
        $testedMethods  = array();
167
 
        $classes        = PHPUnit_Util_File::getClassesInFile(
168
 
                            $this->inSourceFile
169
 
                          );
170
 
        $testMethods    = $classes[$this->inClassName['fullyQualifiedClassName']]['methods'];
171
 
        unset($classes);
172
 
 
173
 
        foreach ($testMethods as $name => $testMethod) {
174
 
            if (strtolower($name) == 'setup') {
175
 
                $setUpVariables = $this->findVariablesThatReferenceClass(
176
 
                  $testMethod['tokens']
177
 
                );
178
 
 
179
 
                break;
180
 
            }
181
 
        }
182
 
 
183
 
        foreach ($testMethods as $name => $testMethod) {
184
 
            $argVariables = array();
185
 
 
186
 
            if (strtolower($name) == 'setup') {
187
 
                continue;
188
 
            }
189
 
 
190
 
            $start = strpos($testMethod['signature'], '(') + 1;
191
 
            $end   = strlen($testMethod['signature']) - $start - 1;
192
 
            $args  = substr($testMethod['signature'], $start, $end);
193
 
 
194
 
            foreach (explode(',', $args) as $arg) {
195
 
                list($type, $var) = explode(' ', $arg);
196
 
 
197
 
                if ($type == $this->outClassName['fullyQualifiedClassName']) {
198
 
                    $argVariables[] = $var;
199
 
                }
200
 
            }
201
 
 
202
 
            $variables = array_unique(
203
 
              array_merge(
204
 
                $setUpVariables,
205
 
                $argVariables,
206
 
                $this->findVariablesThatReferenceClass($testMethod['tokens'])
207
 
              )
208
 
            );
209
 
 
210
 
            foreach ($testMethod['tokens'] as $i => $token) {
211
 
                // Class::method()
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];
220
 
                }
221
 
 
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];
231
 
                }
232
 
 
233
 
                // $object->method()
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];
240
 
                }
241
 
            }
242
 
        }
243
 
 
244
 
        $testedMethods = array_unique($testedMethods);
245
 
        sort($testedMethods);
246
 
 
247
 
        return $testedMethods;
248
 
    }
249
 
 
250
 
    /**
251
 
     * Returns the variables used in test methods
252
 
     * that reference the class under test.
253
 
     *
254
 
     * @param  array $tokens
255
 
     * @return array
256
 
     */
257
 
    protected function findVariablesThatReferenceClass(array $tokens)
258
 
    {
259
 
        $inNew     = FALSE;
260
 
        $variables = array();
261
 
 
262
 
        foreach ($tokens as $i => $token) {
263
 
            if (is_string($token)) {
264
 
                if (trim($token) == ';') {
265
 
                    $inNew = FALSE;
266
 
                }
267
 
 
268
 
                continue;
269
 
            }
270
 
 
271
 
            list ($token, $value) = $token;
272
 
 
273
 
            switch ($token) {
274
 
                case T_NEW: {
275
 
                    $inNew = TRUE;
276
 
                }
277
 
                break;
278
 
 
279
 
                case T_STRING: {
280
 
                    if ($inNew) {
281
 
                        if ($value == $this->outClassName['fullyQualifiedClassName']) {
282
 
                            $variables[] = $this->findVariableName(
283
 
                              $tokens, $i
284
 
                            );
285
 
                        }
286
 
                    }
287
 
 
288
 
                    $inNew = FALSE;
289
 
                }
290
 
                break;
291
 
            }
292
 
        }
293
 
 
294
 
        return $variables;
295
 
    }
296
 
 
297
 
    /**
298
 
     * Finds the variable name of the object for the method call
299
 
     * that is currently being processed.
300
 
     *
301
 
     * @param  array   $tokens
302
 
     * @param  integer $start
303
 
     * @return mixed
304
 
     */
305
 
    protected function findVariableName(array $tokens, $start)
306
 
    {
307
 
        for ($i = $start - 1; $i >= 0; $i--) {
308
 
            if (is_array($tokens[$i]) && $tokens[$i][0] == T_VARIABLE) {
309
 
                $variable = $tokens[$i][1];
310
 
 
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];
316
 
                }
317
 
 
318
 
                return $variable;
319
 
            }
320
 
        }
321
 
 
322
 
        return FALSE;
323
 
    }
324
 
}
325
 
?>
 
 
b'\\ No newline at end of file'