Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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.0.0
44
 */
45
 
46
require_once 'PHPUnit/Util/Filter.php';
47
 
48
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
49
 
50
/**
51
 * Test helpers.
52
 *
53
 * @category   Testing
54
 * @package    PHPUnit
55
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
56
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
57
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
58
 * @version    Release: 3.4.15
59
 * @link       http://www.phpunit.de/
60
 * @since      Class available since Release 3.0.0
61
 */
62
class PHPUnit_Util_Test
63
{
64
    const REGEX_DATA_PROVIDER      = '/@dataProvider\s+([a-zA-Z0-9._:-\\\\x7f-\xff]+)/';
65
    const REGEX_EXPECTED_EXCEPTION = '(@expectedException\s+([:.\w\\\\x7f-\xff]+)(?:[\t ]+(\S*))?(?:[\t ]+(\S*))?\s*$)m';
66
 
67
    private static $annotationCache = array();
68
 
69
    protected static $templateMethods = array(
70
      'setUp', 'assertPreConditions', 'assertPostConditions', 'tearDown'
71
    );
72
 
73
    /**
74
     * @param  PHPUnit_Framework_Test $test
75
     * @param  boolean                $asString
76
     * @return mixed
77
     */
78
    public static function describe(PHPUnit_Framework_Test $test, $asString = TRUE)
79
    {
80
        if ($asString) {
81
            if ($test instanceof PHPUnit_Framework_SelfDescribing) {
82
                return $test->toString();
83
            } else {
84
                return get_class($test);
85
            }
86
        } else {
87
            if ($test instanceof PHPUnit_Framework_TestCase) {
88
                return array(
89
                  get_class($test), $test->getName()
90
                );
91
            }
92
 
93
            else if ($test instanceof PHPUnit_Framework_SelfDescribing) {
94
                return array('', $test->toString());
95
            }
96
 
97
            else {
98
                return array('', get_class($test));
99
            }
100
        }
101
    }
102
 
103
    /**
104
     * @param  PHPUnit_Framework_Test       $test
105
     * @param  PHPUnit_Framework_TestResult $result
106
     * @return mixed
107
     */
108
    public static function lookupResult(PHPUnit_Framework_Test $test, PHPUnit_Framework_TestResult $result)
109
    {
110
        $testName = self::describe($test);
111
 
112
        foreach ($result->errors() as $error) {
113
            if ($testName == self::describe($error->failedTest())) {
114
                return $error;
115
            }
116
        }
117
 
118
        foreach ($result->failures() as $failure) {
119
            if ($testName == self::describe($failure->failedTest())) {
120
                return $failure;
121
            }
122
        }
123
 
124
        foreach ($result->notImplemented() as $notImplemented) {
125
            if ($testName == self::describe($notImplemented->failedTest())) {
126
                return $notImplemented;
127
            }
128
        }
129
 
130
        foreach ($result->skipped() as $skipped) {
131
            if ($testName == self::describe($skipped->failedTest())) {
132
                return $skipped;
133
            }
134
        }
135
 
136
        return PHPUnit_Runner_BaseTestRunner::STATUS_PASSED;
137
    }
138
 
139
    /**
140
     * Returns the files and lines a test method wants to cover.
141
     *
142
     * @param  string $className
143
     * @param  string $methodName
144
     * @return array
145
     * @since  Method available since Release 3.2.0
146
     */
147
    public static function getLinesToBeCovered($className, $methodName)
148
    {
149
        $codeToCoverList = array();
150
        $result          = array();
151
 
152
        if (($pos = strpos($methodName, ' ')) !== FALSE) {
153
            $methodName = substr($methodName, 0, $pos);
154
        }
155
 
156
        $class = new ReflectionClass($className);
157
 
158
        if (!$class->hasMethod($methodName)) {
159
            return $result;
160
        }
161
 
162
        $method     = new ReflectionMethod($className, $methodName);
163
        $docComment = $class->getDocComment() . $method->getDocComment();
164
 
165
        foreach (self::$templateMethods as $templateMethod) {
166
            if ($class->hasMethod($templateMethod)) {
167
                $reflector   = $class->getMethod($templateMethod);
168
                $docComment .= $reflector->getDocComment();
169
                unset($reflector);
170
            }
171
        }
172
 
173
        $annotations = self::parseAnnotations($docComment);
174
 
175
        if (isset($annotations['covers'])) {
176
            foreach ($annotations['covers'] as $coveredElement) {
177
                $codeToCoverList = array_merge(
178
                    $codeToCoverList,
179
                    self::resolveCoversToReflectionObjects($coveredElement)
180
                );
181
            }
182
 
183
            foreach ($codeToCoverList as $codeToCover) {
184
                $fileName  = $codeToCover->getFileName();
185
                $startLine = $codeToCover->getStartLine();
186
                $endLine   = $codeToCover->getEndLine();
187
 
188
                if (!isset($result[$fileName])) {
189
                    $result[$fileName] = array();
190
                }
191
 
192
                $result[$fileName] = array_unique(
193
                  array_merge(
194
                    $result[$fileName], range($startLine, $endLine)
195
                  )
196
                );
197
            }
198
        }
199
 
200
        return $result;
201
    }
202
 
203
    /**
204
     * Returns the expected exception for a test.
205
     *
206
     * @param  string $docComment
207
     * @return array
208
     * @since  Method available since Release 3.3.6
209
     */
210
    public static function getExpectedException($docComment)
211
    {
212
        if (preg_match(self::REGEX_EXPECTED_EXCEPTION, $docComment, $matches)) {
213
            $class   = $matches[1];
214
            $code    = 0;
215
            $message = '';
216
 
217
            if (isset($matches[2])) {
218
                $message = trim($matches[2]);
219
            }
220
 
221
            if (isset($matches[3])) {
222
                $code = (int)$matches[3];
223
            }
224
 
225
            return array(
226
              'class' => $class, 'code' => $code, 'message' => $message
227
            );
228
        }
229
 
230
        return FALSE;
231
    }
232
 
233
    /**
234
     * Returns the provided data for a method.
235
     *
236
     * @param  string $className
237
     * @param  string $methodName
238
     * @param  string $docComment
239
     * @return mixed  array|Iterator when a data provider is specified and exists
240
     *                false          when a data provider is specified and does not exist
241
     *                null           when no data provider is specified
242
     * @since  Method available since Release 3.2.0
243
     */
244
    public static function getProvidedData($className, $methodName)
245
    {
246
        $reflector  = new ReflectionMethod($className, $methodName);
247
        $docComment = $reflector->getDocComment();
248
        $data       = NULL;
249
 
250
        if (preg_match(self::REGEX_DATA_PROVIDER, $docComment, $matches)) {
251
            $dataProviderMethodNameNamespace = explode('\\', $matches[1]);
252
            $leaf                            = explode('::', array_pop($dataProviderMethodNameNamespace));
253
            $dataProviderMethodName          = array_pop($leaf);
254
 
255
            if (!empty($dataProviderMethodNameNamespace)) {
256
                $dataProviderMethodNameNamespace = join('\\', $dataProviderMethodNameNamespace) . '\\';
257
            } else {
258
                $dataProviderMethodNameNamespace = '';
259
            }
260
 
261
            if (!empty($leaf)) {
262
                $dataProviderClassName = $dataProviderMethodNameNamespace . array_pop($leaf);
263
            } else {
264
                $dataProviderClassName = $className;
265
            }
266
 
267
            $dataProviderClass  = new ReflectionClass($dataProviderClassName);
268
            $dataProviderMethod = $dataProviderClass->getMethod(
269
              $dataProviderMethodName
270
            );
271
 
272
            if ($dataProviderMethod->isStatic()) {
273
                $object = NULL;
274
            } else {
275
                $object = $dataProviderClass->newInstance();
276
            }
277
 
278
            if ($dataProviderMethod->getNumberOfParameters() == 0) {
279
                $data = $dataProviderMethod->invoke($object);
280
            } else {
281
                $data = $dataProviderMethod->invoke($object, $methodName);
282
            }
283
        }
284
 
285
        if ($data !== NULL) {
286
            foreach ($data as $key => $value) {
287
                if (!is_array($value)) {
288
                    throw new InvalidArgumentException(
289
                      sprintf(
290
                        'Data set %s is invalid.',
291
                        is_int($key) ? '#' . $key : '"' . $key . '"'
292
                      )
293
                    );
294
                }
295
            }
296
        }
297
 
298
        return $data;
299
    }
300
 
301
    /**
302
     * @param  string $coveredElement
303
     * @return array
304
     */
305
    private static function resolveCoversToReflectionObjects($coveredElement)
306
    {
307
        $codeToCoverList = array();
308
 
309
        if (strpos($coveredElement, '::') !== FALSE) {
310
            list($className, $methodName) = explode('::', $coveredElement);
311
 
312
            if ($methodName[0] == '<') {
313
                $classes = array($className);
314
 
315
                foreach ($classes as $className)
316
                {
317
                    if (!class_exists($className) &&
318
                        !interface_exists($className)) {
319
                        throw new PHPUnit_Framework_Exception(
320
                          sprintf(
321
                            'Trying to @cover not existing class or ' .
322
                            'interface "%s".',
323
                            $className
324
                          )
325
                        );
326
                    }
327
 
328
                    $class   = new ReflectionClass($className);
329
                    $methods = $class->getMethods();
330
                    $inverse = isset($methodName[1]) && $methodName[1] == '!';
331
 
332
                    if (strpos($methodName, 'protected')) {
333
                        $visibility = 'isProtected';
334
                    }
335
 
336
                    else if (strpos($methodName, 'private')) {
337
                        $visibility = 'isPrivate';
338
                    }
339
 
340
                    else if (strpos($methodName, 'public')) {
341
                        $visibility = 'isPublic';
342
                    }
343
 
344
                    foreach ($methods as $method) {
345
                        if ($inverse && !$method->$visibility()) {
346
                            $codeToCoverList[] = $method;
347
                        }
348
 
349
                        else if (!$inverse && $method->$visibility()) {
350
                            $codeToCoverList[] = $method;
351
                        }
352
                    }
353
                }
354
            } else {
355
                $classes = array($className);
356
 
357
                foreach ($classes as $className) {
358
                    if (!((class_exists($className) ||
359
                           interface_exists($className)) &&
360
                          method_exists($className, $methodName))) {
361
                        throw new PHPUnit_Framework_Exception(
362
                          sprintf(
363
                            'Trying to @cover not existing method "%s::%s".',
364
                            $className,
365
                            $methodName
366
                          )
367
                        );
368
                    }
369
 
370
                    $codeToCoverList[] = new ReflectionMethod(
371
                      $className, $methodName
372
                    );
373
                }
374
            }
375
        } else {
376
            $extended = FALSE;
377
 
378
            if (strpos($coveredElement, '<extended>') !== FALSE) {
379
                $coveredElement = str_replace(
380
                  '<extended>', '', $coveredElement
381
                );
382
 
383
                $extended = TRUE;
384
            }
385
 
386
            $classes = array($coveredElement);
387
 
388
            if ($extended) {
389
                $classes = array_merge(
390
                  $classes,
391
                  class_implements($coveredElement),
392
                  class_parents($coveredElement)
393
                );
394
            }
395
 
396
            foreach ($classes as $className) {
397
                if (!class_exists($className) &&
398
                    !interface_exists($className)) {
399
                    throw new PHPUnit_Framework_Exception(
400
                      sprintf(
401
                        'Trying to @cover not existing class or ' .
402
                        'interface "%s".',
403
                        $className
404
                      )
405
                    );
406
                }
407
 
408
                $codeToCoverList[] = new ReflectionClass($className);
409
            }
410
        }
411
 
412
        return $codeToCoverList;
413
    }
414
 
415
    /**
416
     * @param  string $className
417
     * @param  string $methodName
418
     * @return array
419
     * @throws ReflectionException
420
     * @since  Method available since Release 3.4.0
421
     */
422
    public static function parseTestMethodAnnotations($className, $methodName = '')
423
    {
424
        if (!isset(self::$annotationCache[$className])) {
425
            $class = new ReflectionClass($className);
426
            self::$annotationCache[$className] = self::parseAnnotations($class->getDocComment());
427
        }
428
 
429
        if (!empty($methodName) && !isset(self::$annotationCache[$className . '::' . $methodName])) {
430
            $method = new ReflectionMethod($className, $methodName);
431
            self::$annotationCache[$className . '::' . $methodName] = self::parseAnnotations($method->getDocComment());
432
        }
433
 
434
        return array(
435
          'class'  => self::$annotationCache[$className],
436
          'method' => !empty($methodName) ? self::$annotationCache[$className . '::' . $methodName] : array()
437
        );
438
    }
439
 
440
    /**
441
     * @param  string $docblock
442
     * @return array
443
     * @since  Method available since Release 3.4.0
444
     */
445
    private static function parseAnnotations($docblock)
446
    {
447
        $annotations = array();
448
 
449
        if (preg_match_all('/@(?P<name>[A-Za-z_-]+)(?:[ \t]+(?P<value>.*?))?[ \t]*\r?$/m', $docblock, $matches)) {
450
            $numMatches = count($matches[0]);
451
 
452
            for ($i = 0; $i < $numMatches; ++$i) {
453
                $annotations[$matches['name'][$i]][] = $matches['value'][$i];
454
            }
455
        }
456
 
457
        return $annotations;
458
    }
459
 
460
    /**
461
     * Returns the backup settings for a test.
462
     *
463
     * @param  string $className
464
     * @param  string $methodName
465
     * @return array
466
     * @since  Method available since Release 3.4.0
467
     */
468
    public static function getBackupSettings($className, $methodName)
469
    {
470
        return array(
471
          'backupGlobals' => self::getBooleanAnnotationSetting(
472
            $className, $methodName, 'backupGlobals'
473
          ),
474
          'backupStaticAttributes' => self::getBooleanAnnotationSetting(
475
            $className, $methodName, 'backupStaticAttributes'
476
          )
477
        );
478
    }
479
 
480
    /**
481
     * Returns the dependencies for a test class or method.
482
     *
483
     * @param  string $className
484
     * @param  string $methodName
485
     * @return array
486
     * @since  Method available since Release 3.4.0
487
     */
488
    public static function getDependencies($className, $methodName)
489
    {
490
        $annotations = self::parseTestMethodAnnotations(
491
          $className, $methodName
492
        );
493
 
494
        $dependencies = array();
495
 
496
        if (isset($annotations['class']['depends'])) {
497
            $dependencies = $annotations['class']['depends'];
498
        }
499
 
500
        if (isset($annotations['method']['depends'])) {
501
            $dependencies = array_merge(
502
              $dependencies, $annotations['method']['depends']
503
            );
504
        }
505
 
506
        return array_unique($dependencies);
507
    }
508
 
509
    /**
510
     * Returns the error handler settings for a test.
511
     *
512
     * @param  string $className
513
     * @param  string $methodName
514
     * @return boolean
515
     * @since  Method available since Release 3.4.0
516
     */
517
    public static function getErrorHandlerSettings($className, $methodName)
518
    {
519
        return self::getBooleanAnnotationSetting(
520
          $className, $methodName, 'errorHandler'
521
        );
522
    }
523
 
524
    /**
525
     * Returns the groups for a test class or method.
526
     *
527
     * @param  string $className
528
     * @param  string $methodName
529
     * @return array
530
     * @since  Method available since Release 3.2.0
531
     */
532
    public static function getGroups($className, $methodName = '')
533
    {
534
        $annotations = self::parseTestMethodAnnotations(
535
          $className, $methodName
536
        );
537
 
538
        $groups = array();
539
 
540
        if (isset($annotations['class']['group'])) {
541
            $groups = $annotations['class']['group'];
542
        }
543
 
544
        if (isset($annotations['method']['group'])) {
545
            $groups = array_merge($groups, $annotations['method']['group']);
546
        }
547
 
548
        return array_unique($groups);
549
    }
550
 
551
    /**
552
     * Returns the tickets for a test class or method.
553
     *
554
     * @param  string $className
555
     * @param  string $methodName
556
     * @return array
557
     * @since  Method available since Release 3.4.0
558
     */
559
    public static function getTickets($className, $methodName)
560
    {
561
        $annotations = self::parseTestMethodAnnotations(
562
          $className, $methodName
563
        );
564
 
565
        $tickets = array();
566
 
567
        if (isset($annotations['class']['ticket'])) {
568
            $tickets = $annotations['class']['ticket'];
569
        }
570
 
571
        if (isset($annotations['method']['ticket'])) {
572
            $tickets = array_merge($tickets, $annotations['method']['ticket']);
573
        }
574
 
575
        return array_unique($tickets);
576
    }
577
 
578
    /**
579
     * Returns the output buffering settings for a test.
580
     *
581
     * @param  string $className
582
     * @param  string $methodName
583
     * @return boolean
584
     * @since  Method available since Release 3.4.0
585
     */
586
    public static function getOutputBufferingSettings($className, $methodName)
587
    {
588
        return self::getBooleanAnnotationSetting(
589
          $className, $methodName, 'outputBuffering'
590
        );
591
    }
592
 
593
    /**
594
     * Returns the process isolation settings for a test.
595
     *
596
     * @param  string $className
597
     * @param  string $methodName
598
     * @return boolean
599
     * @since  Method available since Release 3.4.1
600
     */
601
    public static function getProcessIsolationSettings($className, $methodName)
602
    {
603
        $annotations = self::parseTestMethodAnnotations(
604
          $className, $methodName
605
        );
606
 
607
        if (isset($annotations['class']['runTestsInSeparateProcesses']) ||
608
            isset($annotations['method']['runInSeparateProcess'])) {
609
            return TRUE;
610
        } else {
611
            return FALSE;
612
        }
613
    }
614
 
615
    /**
616
     * Returns the preserve global state settings for a test.
617
     *
618
     * @param  string $className
619
     * @param  string $methodName
620
     * @return boolean
621
     * @since  Method available since Release 3.4.0
622
     */
623
    public static function getPreserveGlobalStateSettings($className, $methodName)
624
    {
625
        return self::getBooleanAnnotationSetting(
626
          $className, $methodName, 'preserveGlobalState'
627
        );
628
    }
629
 
630
    /**
631
     * @param  string $className
632
     * @param  string $methodName
633
     * @param  string $settingName
634
     * @return boolean
635
     * @since  Method available since Release 3.4.0
636
     */
637
    private static function getBooleanAnnotationSetting($className, $methodName, $settingName)
638
    {
639
        $annotations = self::parseTestMethodAnnotations(
640
          $className, $methodName
641
        );
642
 
643
        $result = NULL;
644
 
645
        if (isset($annotations['class'][$settingName])) {
646
            if ($annotations['class'][$settingName][0] == 'enabled') {
647
                $result = TRUE;
648
            }
649
 
650
            else if ($annotations['class'][$settingName][0] == 'disabled') {
651
                $result = FALSE;
652
            }
653
        }
654
 
655
        if (isset($annotations['method'][$settingName])) {
656
            if ($annotations['method'][$settingName][0] == 'enabled') {
657
                $result = TRUE;
658
            }
659
 
660
            else if ($annotations['method'][$settingName][0] == 'disabled') {
661
                $result = FALSE;
662
            }
663
        }
664
 
665
        return $result;
666
    }
667
}
668
?>