~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to vendor/phpunit/phpunit/src/Util/TestDox/NamePrettifier.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * This file is part of PHPUnit.
 
4
 *
 
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 
6
 *
 
7
 * For the full copyright and license information, please view the LICENSE
 
8
 * file that was distributed with this source code.
 
9
 */
 
10
 
 
11
/**
 
12
 * Prettifies class and method names for use in TestDox documentation.
 
13
 *
 
14
 * @package    PHPUnit
 
15
 * @subpackage Util_TestDox
 
16
 * @author     Sebastian Bergmann <sebastian@phpunit.de>
 
17
 * @copyright  Sebastian Bergmann <sebastian@phpunit.de>
 
18
 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
 
19
 * @link       http://www.phpunit.de/
 
20
 * @since      Class available since Release 2.1.0
 
21
 */
 
22
class PHPUnit_Util_TestDox_NamePrettifier
 
23
{
 
24
    /**
 
25
     * @var    string
 
26
     */
 
27
    protected $prefix = 'Test';
 
28
 
 
29
    /**
 
30
     * @var    string
 
31
     */
 
32
    protected $suffix = 'Test';
 
33
 
 
34
    /**
 
35
     * @var    array
 
36
     */
 
37
    protected $strings = array();
 
38
 
 
39
    /**
 
40
     * Prettifies the name of a test class.
 
41
     *
 
42
     * @param  string $name
 
43
     * @return string
 
44
     */
 
45
    public function prettifyTestClass($name)
 
46
    {
 
47
        $title = $name;
 
48
 
 
49
        if ($this->suffix !== null &&
 
50
            $this->suffix == substr($name, -1 * strlen($this->suffix))) {
 
51
            $title = substr($title, 0, strripos($title, $this->suffix));
 
52
        }
 
53
 
 
54
        if ($this->prefix !== null &&
 
55
            $this->prefix == substr($name, 0, strlen($this->prefix))) {
 
56
            $title = substr($title, strlen($this->prefix));
 
57
        }
 
58
 
 
59
        if (substr($title, 0, 1) == '\\') {
 
60
            $title = substr($title, 1);
 
61
        }
 
62
 
 
63
        return $title;
 
64
    }
 
65
 
 
66
    /**
 
67
     * Prettifies the name of a test method.
 
68
     *
 
69
     * @param  string $name
 
70
     * @return string
 
71
     */
 
72
    public function prettifyTestMethod($name)
 
73
    {
 
74
        $buffer = '';
 
75
 
 
76
        if (!is_string($name) || strlen($name) == 0) {
 
77
            return $buffer;
 
78
        }
 
79
 
 
80
        $string = preg_replace('#\d+$#', '', $name, -1, $count);
 
81
 
 
82
        if (in_array($string, $this->strings)) {
 
83
            $name = $string;
 
84
        } elseif ($count == 0) {
 
85
            $this->strings[] = $string;
 
86
        }
 
87
 
 
88
        if (strpos($name, '_') !== false) {
 
89
            return str_replace('_', ' ', $name);
 
90
        }
 
91
 
 
92
        $max = strlen($name);
 
93
 
 
94
        if (substr($name, 0, 4) == 'test') {
 
95
            $offset = 4;
 
96
        } else {
 
97
            $offset  = 0;
 
98
            $name[0] = strtoupper($name[0]);
 
99
        }
 
100
 
 
101
        $wasNumeric = false;
 
102
 
 
103
        for ($i = $offset; $i < $max; $i++) {
 
104
            if ($i > $offset &&
 
105
                ord($name[$i]) >= 65 &&
 
106
                ord($name[$i]) <= 90) {
 
107
                $buffer .= ' ' . strtolower($name[$i]);
 
108
            } else {
 
109
                $isNumeric = is_numeric($name[$i]);
 
110
 
 
111
                if (!$wasNumeric && $isNumeric) {
 
112
                    $buffer    .= ' ';
 
113
                    $wasNumeric = true;
 
114
                }
 
115
 
 
116
                if ($wasNumeric && !$isNumeric) {
 
117
                    $wasNumeric = false;
 
118
                }
 
119
 
 
120
                $buffer .= $name[$i];
 
121
            }
 
122
        }
 
123
 
 
124
        return $buffer;
 
125
    }
 
126
 
 
127
    /**
 
128
     * Sets the prefix of test names.
 
129
     *
 
130
     * @param string $prefix
 
131
     */
 
132
    public function setPrefix($prefix)
 
133
    {
 
134
        $this->prefix = $prefix;
 
135
    }
 
136
 
 
137
    /**
 
138
     * Sets the suffix of test names.
 
139
     *
 
140
     * @param string $suffix
 
141
     */
 
142
    public function setSuffix($suffix)
 
143
    {
 
144
        $this->suffix = $suffix;
 
145
    }
 
146
}