Fix bug in style changes for the Use cases on the live site.
[yaffs-website] / vendor / phpunit / phpunit / src / Util / TestDox / NamePrettifier.php
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  * @since Class available since Release 2.1.0
15  */
16 class PHPUnit_Util_TestDox_NamePrettifier
17 {
18     /**
19      * @var string
20      */
21     protected $prefix = 'Test';
22
23     /**
24      * @var string
25      */
26     protected $suffix = 'Test';
27
28     /**
29      * @var array
30      */
31     protected $strings = array();
32
33     /**
34      * Prettifies the name of a test class.
35      *
36      * @param string $name
37      *
38      * @return string
39      */
40     public function prettifyTestClass($name)
41     {
42         $title = $name;
43
44         if ($this->suffix !== null &&
45             $this->suffix == substr($name, -1 * strlen($this->suffix))) {
46             $title = substr($title, 0, strripos($title, $this->suffix));
47         }
48
49         if ($this->prefix !== null &&
50             $this->prefix == substr($name, 0, strlen($this->prefix))) {
51             $title = substr($title, strlen($this->prefix));
52         }
53
54         if (substr($title, 0, 1) == '\\') {
55             $title = substr($title, 1);
56         }
57
58         return $title;
59     }
60
61     /**
62      * Prettifies the name of a test method.
63      *
64      * @param string $name
65      *
66      * @return string
67      */
68     public function prettifyTestMethod($name)
69     {
70         $buffer = '';
71
72         if (!is_string($name) || strlen($name) == 0) {
73             return $buffer;
74         }
75
76         $string = preg_replace('#\d+$#', '', $name, -1, $count);
77
78         if (in_array($string, $this->strings)) {
79             $name = $string;
80         } elseif ($count == 0) {
81             $this->strings[] = $string;
82         }
83
84         if (substr($name, 0, 4) == 'test') {
85             $name = substr($name, 4);
86         }
87
88         $name[0] = strtoupper($name[0]);
89
90         if (strpos($name, '_') !== false) {
91             return trim(str_replace('_', ' ', $name));
92         }
93
94         $max        = strlen($name);
95         $wasNumeric = false;
96
97         for ($i = 0; $i < $max; $i++) {
98             if ($i > 0 &&
99                 ord($name[$i]) >= 65 &&
100                 ord($name[$i]) <= 90) {
101                 $buffer .= ' ' . strtolower($name[$i]);
102             } else {
103                 $isNumeric = is_numeric($name[$i]);
104
105                 if (!$wasNumeric && $isNumeric) {
106                     $buffer    .= ' ';
107                     $wasNumeric = true;
108                 }
109
110                 if ($wasNumeric && !$isNumeric) {
111                     $wasNumeric = false;
112                 }
113
114                 $buffer .= $name[$i];
115             }
116         }
117
118         return $buffer;
119     }
120
121     /**
122      * Sets the prefix of test names.
123      *
124      * @param string $prefix
125      */
126     public function setPrefix($prefix)
127     {
128         $this->prefix = $prefix;
129     }
130
131     /**
132      * Sets the suffix of test names.
133      *
134      * @param string $suffix
135      */
136     public function setSuffix($suffix)
137     {
138         $this->suffix = $suffix;
139     }
140 }