]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHP/CodeCoverage/TextUI/Command.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHP / CodeCoverage / TextUI / Command.php
1 <?php
2 /**
3  * PHP_CodeCoverage
4  *
5  * Copyright (c) 2009-2011, 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   PHP
38  * @package    CodeCoverage
39  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40  * @copyright  2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42  * @link       http://github.com/sebastianbergmann/php-code-coverage
43  * @since      File available since Release 1.0.0
44  */
45
46 require_once 'PHP/CodeCoverage.php';
47
48 require_once 'ezc/Base/base.php';
49 spl_autoload_register(array('ezcBase', 'autoload'));
50
51 /**
52  * TextUI frontend for PHP_CodeCoverage.
53  *
54  * @category   PHP
55  * @package    CodeCoverage
56  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
57  * @copyright  2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
58  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
59  * @version    Release: 1.0.4
60  * @link       http://github.com/sebastianbergmann/php-code-coverage
61  * @since      Class available since Release 1.0.0
62  */
63 class PHP_CodeCoverage_TextUI_Command
64 {
65     /**
66      * Main method.
67      */
68     public static function main()
69     {
70         $input = new ezcConsoleInput;
71
72         $input->registerOption(
73           new ezcConsoleOption(
74             '',
75             'clover',
76             ezcConsoleInput::TYPE_STRING
77            )
78         );
79
80         $input->registerOption(
81           new ezcConsoleOption(
82             '',
83             'html',
84             ezcConsoleInput::TYPE_STRING
85            )
86         );
87
88         $input->registerOption(
89           new ezcConsoleOption(
90             '',
91             'blacklist',
92             ezcConsoleInput::TYPE_STRING,
93             array(),
94             TRUE
95            )
96         );
97
98         $input->registerOption(
99           new ezcConsoleOption(
100             '',
101             'whitelist',
102             ezcConsoleInput::TYPE_STRING,
103             array(),
104             TRUE
105            )
106         );
107
108         $input->registerOption(
109           new ezcConsoleOption(
110             'h',
111             'help',
112             ezcConsoleInput::TYPE_NONE,
113             NULL,
114             FALSE,
115             '',
116             '',
117             array(),
118             array(),
119             FALSE,
120             FALSE,
121             TRUE
122            )
123         );
124
125         $input->registerOption(
126           new ezcConsoleOption(
127             'v',
128             'version',
129             ezcConsoleInput::TYPE_NONE,
130             NULL,
131             FALSE,
132             '',
133             '',
134             array(),
135             array(),
136             FALSE,
137             FALSE,
138             TRUE
139            )
140         );
141
142         try {
143             $input->process();
144         }
145
146         catch (ezcConsoleOptionException $e) {
147             print $e->getMessage() . "\n";
148             exit(1);
149         }
150
151         if ($input->getOption('help')->value) {
152             self::showHelp();
153             exit(0);
154         }
155
156         else if ($input->getOption('version')->value) {
157             self::printVersionString();
158             exit(0);
159         }
160
161         $arguments = $input->getArguments();
162         $clover    = $input->getOption('clover')->value;
163         $html      = $input->getOption('html')->value;
164         $blacklist = $input->getOption('blacklist')->value;
165         $whitelist = $input->getOption('whitelist')->value;
166
167         if (count($arguments) == 1) {
168             self::printVersionString();
169
170             $coverage = new PHP_CodeCoverage;
171             $filter   = $coverage->filter();
172
173             if (empty($whitelist)) {
174                 $c = new ReflectionClass('ezcBase');
175                 $filter->addDirectoryToBlacklist(dirname($c->getFileName()));
176                 $c = new ReflectionClass('ezcConsoleInput');
177                 $filter->addDirectoryToBlacklist(dirname($c->getFileName()));
178
179                 foreach ($blacklist as $item) {
180                     if (is_dir($item)) {
181                         $filter->addDirectoryToBlacklist($item);
182                     }
183
184                     else if (is_file($item)) {
185                         $filter->addFileToBlacklist($item);
186                     }
187                 }
188             } else {
189                 foreach ($whitelist as $item) {
190                     if (is_dir($item)) {
191                         $filter->addDirectoryToWhitelist($item);
192                     }
193
194                     else if (is_file($item)) {
195                         $filter->addFileToWhitelist($item);
196                     }
197                 }
198             }
199
200             $coverage->start('phpcov');
201
202             require $arguments[0];
203
204             $coverage->stop();
205
206             if ($clover) {
207                 require 'PHP/CodeCoverage/Report/Clover.php';
208
209                 $writer = new PHP_CodeCoverage_Report_Clover;
210                 $writer->process($coverage, $clover);
211             }
212
213             if ($html) {
214                 require 'PHP/CodeCoverage/Report/HTML.php';
215
216                 $writer = new PHP_CodeCoverage_Report_HTML;
217                 $writer->process($coverage, $html);
218             }
219         } else {
220             self::showHelp();
221             exit(1);
222         }
223     }
224
225     /**
226      * Shows an error.
227      *
228      * @param string $message
229      */
230     protected static function showError($message)
231     {
232         self::printVersionString();
233
234         print $message;
235
236         exit(1);
237     }
238
239     /**
240      * Shows the help.
241      */
242     protected static function showHelp()
243     {
244         self::printVersionString();
245
246         print <<<EOT
247 Usage: phpcov [switches] <file>
248
249   --clover <file>         Write code coverage data in Clover XML format.
250   --html <dir>            Generate code coverage report in HTML format.
251
252   --blacklist <dir|file>  Adds <dir|file> to the blacklist.
253   --whitelist <dir|file>  Adds <dir|file> to the whitelist.
254
255   --help                  Prints this usage information.
256   --version               Prints the version and exits.
257
258 EOT;
259     }
260
261     /**
262      * Prints the version string.
263      */
264     protected static function printVersionString()
265     {
266         print "phpcov 1.0.4 by Sebastian Bergmann.\n\n";
267     }
268 }