]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Util/Skeleton/Test.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Util / Skeleton / Test.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, 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-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
43  * @link       http://www.phpunit.de/
44  * @since      File available since Release 3.3.0
45  */
46
47 require_once 'PHPUnit/Util/Filter.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 test class skeletons from classes.
55  *
56  * @category   Testing
57  * @package    PHPUnit
58  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
59  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
60  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
61  * @version    Release: 3.3.17
62  * @link       http://www.phpunit.de/
63  * @since      Class available since Release 3.3.0
64  */
65 class PHPUnit_Util_Skeleton_Test extends PHPUnit_Util_Skeleton
66 {
67     /**
68      * @var    array
69      */
70     protected $methodNameCounter = array();
71
72     /**
73      * Constructor.
74      *
75      * @param  string  $inClassName
76      * @param  string  $inSourceFile
77      * @throws RuntimeException
78      */
79     public function __construct($inClassName, $inSourceFile = '')
80     {
81         $this->inClassName  = $inClassName;
82         $this->outClassName = $inClassName . 'Test';
83
84         if (class_exists($inClassName)) {
85             $this->inSourceFile = '<internal>';
86         }
87
88         else if (empty($inSourceFile) && is_file($inClassName . '.php')) {
89             $this->inSourceFile = $inClassName . '.php';
90         }
91
92         else if (empty($inSourceFile) ||
93                  is_file(str_replace('_', '/', $inClassName) . '.php')) {
94             $this->inSourceFile  = str_replace('_', '/', $inClassName) . '.php';
95             $this->outSourceFile = str_replace('_', '/', $inClassName) . 'Test.php';
96         }
97
98         else if (empty($inSourceFile)) {
99             throw new RuntimeException(
100               sprintf(
101                 'Neither "%s.php" nor "%s.php" could be opened.',
102                 $inClassName,
103                 str_replace('_', '/', $inClassName)
104               )
105             );
106         }
107
108         else if (!is_file($inSourceFile)) {
109             throw new RuntimeException(
110               sprintf(
111                 '"%s" could not be opened.',
112
113                 $inSourceFile
114               )
115             );
116         } else {
117             $this->inSourceFile = $inSourceFile;
118         }
119
120         if ($this->inSourceFile != '<internal>') {
121             include_once $this->inSourceFile;
122         }
123
124         if (!class_exists($inClassName)) {
125             throw new RuntimeException(
126               sprintf(
127                 'Could not find class "%s" in "%s".',
128
129                 $inClassName,
130                 realpath($this->inSourceFile)
131               )
132             );
133         }
134
135         $this->outSourceFile = dirname($this->inSourceFile) . DIRECTORY_SEPARATOR . $this->inClassName . 'Test.php';
136     }
137
138     /**
139      * Generates the test class' source.
140      *
141      * @param  boolean $verbose
142      * @return mixed
143      */
144     public function generate($verbose = FALSE)
145     {
146         $class             = new ReflectionClass($this->inClassName);
147         $methods           = '';
148         $incompleteMethods = '';
149
150         foreach ($class->getMethods() as $method) {
151             if (!$method->isConstructor() &&
152                 !$method->isAbstract() &&
153                  $method->isPublic() &&
154                  $method->getDeclaringClass()->getName() == $this->inClassName) {
155                 $assertAnnotationFound = FALSE;
156
157                 if (preg_match_all('/@assert(.*)$/Um', $method->getDocComment(), $annotations)) {
158                     foreach ($annotations[1] as $annotation) {
159                         if (preg_match('/\((.*)\)\s+([^\s]*)\s+(.*)/', $annotation, $matches)) {
160                             switch ($matches[2]) {
161                                 case '==': {
162                                     $assertion = 'Equals';
163                                 }
164                                 break;
165
166                                 case '!=': {
167                                     $assertion = 'NotEquals';
168                                 }
169                                 break;
170
171                                 case '===': {
172                                     $assertion = 'Same';
173                                 }
174                                 break;
175
176                                 case '!==': {
177                                     $assertion = 'NotSame';
178                                 }
179                                 break;
180
181                                 case '>': {
182                                     $assertion = 'GreaterThan';
183                                 }
184                                 break;
185
186                                 case '>=': {
187                                     $assertion = 'GreaterThanOrEqual';
188                                 }
189                                 break;
190
191                                 case '<': {
192                                     $assertion = 'LessThan';
193                                 }
194                                 break;
195
196                                 case '<=': {
197                                     $assertion = 'LessThanOrEqual';
198                                 }
199                                 break;
200
201                                 case 'throws': {
202                                     $assertion = 'exception';
203                                 }
204                                 break;
205
206                                 default: {
207                                     throw new RuntimeException;
208                                 }
209                             }
210
211                             if ($assertion == 'exception') {
212                                 $template = 'TestMethodException';
213                             }
214
215                             else if ($assertion == 'Equals' && strtolower($matches[3]) == 'true') {
216                                 $assertion = 'True';
217                                 $template  = 'TestMethodBool';
218                             }
219
220                             else if ($assertion == 'NotEquals' && strtolower($matches[3]) == 'true') {
221                                 $assertion = 'False';
222                                 $template  = 'TestMethodBool';
223                             }
224
225                             else if ($assertion == 'Equals' && strtolower($matches[3]) == 'false') {
226                                 $assertion = 'False';
227                                 $template  = 'TestMethodBool';
228                             }
229
230                             else if ($assertion == 'NotEquals' && strtolower($matches[3]) == 'false') {
231                                 $assertion = 'True';
232                                 $template  = 'TestMethodBool';
233                             }
234
235                             else {
236                                 $template = 'TestMethod';
237                             }
238
239                             if ($method->isStatic()) {
240                                 $template .= 'Static';
241                             }
242
243                             $methodTemplate = new PHPUnit_Util_Template(
244                               sprintf(
245                                 '%s%sTemplate%s%s.tpl',
246
247                                 dirname(__FILE__),
248                                 DIRECTORY_SEPARATOR,
249                                 DIRECTORY_SEPARATOR,
250                                 $template
251                               )
252                             );
253
254                             $origMethodName = $method->getName();
255                             $methodName     = ucfirst($origMethodName);
256
257                             if (isset($this->methodNameCounter[$methodName])) {
258                                 $this->methodNameCounter[$methodName]++;
259                             } else {
260                                 $this->methodNameCounter[$methodName] = 1;
261                             }
262
263                             if ($this->methodNameCounter[$methodName] > 1) {
264                                 $methodName .= $this->methodNameCounter[$methodName];
265                             }
266
267                             $methodTemplate->setVar(
268                               array(
269                                 'annotation'     => trim($annotation),
270                                 'arguments'      => $matches[1],
271                                 'assertion'      => isset($assertion) ? $assertion : '',
272                                 'expected'       => $matches[3],
273                                 'origMethodName' => $origMethodName,
274                                 'className'      => $this->inClassName,
275                                 'methodName'     => $methodName
276                               )
277                             );
278
279                             $methods .= $methodTemplate->render();
280
281                             $assertAnnotationFound = TRUE;
282                         }
283                     }
284                 }
285
286                 if (!$assertAnnotationFound) {
287                     $methodTemplate = new PHPUnit_Util_Template(
288                       sprintf(
289                         '%s%sTemplate%sIncompleteTestMethod.tpl',
290
291                         dirname(__FILE__),
292                         DIRECTORY_SEPARATOR,
293                         DIRECTORY_SEPARATOR
294                       )
295                     );
296
297                     $methodTemplate->setVar(
298                       array(
299                         'methodName' => ucfirst($method->getName())
300                       )
301                     );
302
303                     $incompleteMethods .= $methodTemplate->render();
304                 }
305             }
306         }
307
308         $classTemplate = new PHPUnit_Util_Template(
309           sprintf(
310             '%s%sTemplate%sTestClass.tpl',
311
312             dirname(__FILE__),
313             DIRECTORY_SEPARATOR,
314             DIRECTORY_SEPARATOR
315           )
316         );
317
318         if ($this->inSourceFile != '<internal>') {
319             $requireClassFile = sprintf(
320               "\n\nrequire_once '%s';",
321
322               $this->inSourceFile
323             );
324         } else {
325             $requireClassFile = '';
326         }
327
328         $classTemplate->setVar(
329           array(
330             'className'        => $this->inClassName,
331             'requireClassFile' => $requireClassFile,
332             'methods'          => $methods . $incompleteMethods,
333             'date'             => date('Y-m-d'),
334             'time'             => date('H:i:s')
335           )
336         );
337
338         if (!$verbose) {
339             return $classTemplate->render();
340         } else {
341             return array(
342               'code'       => $classTemplate->render(),
343               'incomplete' => empty($methods)
344             );
345         }
346     }
347 }
348 ?>