| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Tests for PHP_CodeSniffer error suppression tags.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
11 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
12 |
* @version CVS: $Id: ErrorSuppressionTest.php 287720 2009-08-26 00:37:52Z squiz $
|
|
|
13 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
require_once 'PHPUnit/Framework/TestCase.php';
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Tests for PHP_CodeSniffer error suppression tags.
|
|
|
20 |
*
|
|
|
21 |
* @category PHP
|
|
|
22 |
* @package PHP_CodeSniffer
|
|
|
23 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
24 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
25 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
26 |
* @version Release: 1.2.1
|
|
|
27 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
28 |
*/
|
|
|
29 |
class Core_ErrorSuppressionTest extends PHPUnit_Framework_TestCase
|
|
|
30 |
{
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Test suppressing a single error.
|
|
|
35 |
*
|
|
|
36 |
* @return void
|
|
|
37 |
*/
|
|
|
38 |
public function testSuppressError()
|
|
|
39 |
{
|
|
|
40 |
$phpcs = new PHP_CodeSniffer();
|
|
|
41 |
$phpcs->setTokenListeners('Generic', array('Generic_Sniffs_PHP_LowerCaseConstantSniff'));
|
|
|
42 |
$phpcs->populateTokenListeners();
|
|
|
43 |
|
|
|
44 |
// Process without suppression.
|
|
|
45 |
$content = '<?php '.PHP_EOL.'$var = FALSE;';
|
|
|
46 |
$phpcs->processFile('noSuppressionTest.php', $content);
|
|
|
47 |
|
|
|
48 |
$files = $phpcs->getFiles();
|
|
|
49 |
$file = $files[0];
|
|
|
50 |
|
|
|
51 |
$errors = $file->getErrors();
|
|
|
52 |
$numErrors = $file->getErrorCount();
|
|
|
53 |
$this->assertEquals(1, $numErrors);
|
|
|
54 |
$this->assertEquals(1, count($errors));
|
|
|
55 |
|
|
|
56 |
// Process with suppression.
|
|
|
57 |
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreStart'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'// @codingStandardsIgnoreEnd';
|
|
|
58 |
$phpcs->processFile('suppressionTest.php', $content);
|
|
|
59 |
|
|
|
60 |
$files = $phpcs->getFiles();
|
|
|
61 |
$file = $files[1];
|
|
|
62 |
|
|
|
63 |
$errors = $file->getErrors();
|
|
|
64 |
$numErrors = $file->getErrorCount();
|
|
|
65 |
$this->assertEquals(0, $numErrors);
|
|
|
66 |
$this->assertEquals(0, count($errors));
|
|
|
67 |
|
|
|
68 |
}//end testSuppressError()
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Test suppressing 1 out of 2 errors.
|
|
|
73 |
*
|
|
|
74 |
* @return void
|
|
|
75 |
*/
|
|
|
76 |
public function testSuppressSomeErrors()
|
|
|
77 |
{
|
|
|
78 |
$phpcs = new PHP_CodeSniffer();
|
|
|
79 |
$phpcs->setTokenListeners('Generic', array('Generic_Sniffs_PHP_LowerCaseConstantSniff'));
|
|
|
80 |
$phpcs->populateTokenListeners();
|
|
|
81 |
|
|
|
82 |
// Process without suppression.
|
|
|
83 |
$content = '<?php '.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;';
|
|
|
84 |
$phpcs->processFile('noSuppressionTest.php', $content);
|
|
|
85 |
|
|
|
86 |
$files = $phpcs->getFiles();
|
|
|
87 |
$file = $files[0];
|
|
|
88 |
|
|
|
89 |
$errors = $file->getErrors();
|
|
|
90 |
$numErrors = $file->getErrorCount();
|
|
|
91 |
$this->assertEquals(2, $numErrors);
|
|
|
92 |
$this->assertEquals(2, count($errors));
|
|
|
93 |
|
|
|
94 |
// Process with suppression.
|
|
|
95 |
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreStart'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'// @codingStandardsIgnoreEnd'.PHP_EOL.'$var = TRUE;';
|
|
|
96 |
$phpcs->processFile('suppressionTest.php', $content);
|
|
|
97 |
|
|
|
98 |
$files = $phpcs->getFiles();
|
|
|
99 |
$file = $files[1];
|
|
|
100 |
|
|
|
101 |
$errors = $file->getErrors();
|
|
|
102 |
$numErrors = $file->getErrorCount();
|
|
|
103 |
$this->assertEquals(1, $numErrors);
|
|
|
104 |
$this->assertEquals(1, count($errors));
|
|
|
105 |
|
|
|
106 |
}//end testSuppressSomeErrors()
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Test suppressing a single warning.
|
|
|
111 |
*
|
|
|
112 |
* @return void
|
|
|
113 |
*/
|
|
|
114 |
public function testSuppressWarning()
|
|
|
115 |
{
|
|
|
116 |
$phpcs = new PHP_CodeSniffer();
|
|
|
117 |
$phpcs->setTokenListeners('Generic', array('Generic_Sniffs_Commenting_TodoSniff'));
|
|
|
118 |
$phpcs->populateTokenListeners();
|
|
|
119 |
|
|
|
120 |
// Process without suppression.
|
|
|
121 |
$content = '<?php '.PHP_EOL.'//TODO: write some code';
|
|
|
122 |
$phpcs->processFile('noSuppressionTest.php', $content);
|
|
|
123 |
|
|
|
124 |
$files = $phpcs->getFiles();
|
|
|
125 |
$file = $files[0];
|
|
|
126 |
|
|
|
127 |
$warnings = $file->getWarnings();
|
|
|
128 |
$numWarnings = $file->getWarningCount();
|
|
|
129 |
$this->assertEquals(1, $numWarnings);
|
|
|
130 |
$this->assertEquals(1, count($warnings));
|
|
|
131 |
|
|
|
132 |
// Process with suppression.
|
|
|
133 |
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreStart'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// @codingStandardsIgnoreEnd';
|
|
|
134 |
$phpcs->processFile('suppressionTest.php', $content);
|
|
|
135 |
|
|
|
136 |
$files = $phpcs->getFiles();
|
|
|
137 |
$file = $files[1];
|
|
|
138 |
|
|
|
139 |
$warnings = $file->getWarnings();
|
|
|
140 |
$numWarnings = $file->getWarningCount();
|
|
|
141 |
$this->assertEquals(0, $numWarnings);
|
|
|
142 |
$this->assertEquals(0, count($warnings));
|
|
|
143 |
|
|
|
144 |
}//end testSuppressWarning()
|
|
|
145 |
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Test suppressing a whole file.
|
|
|
149 |
*
|
|
|
150 |
* @return void
|
|
|
151 |
*/
|
|
|
152 |
public function testSuppressFile()
|
|
|
153 |
{
|
|
|
154 |
$phpcs = new PHP_CodeSniffer();
|
|
|
155 |
$phpcs->setTokenListeners('Squiz', array('Squiz_Sniffs_Commenting_FileCommentSniff'));
|
|
|
156 |
$phpcs->populateTokenListeners();
|
|
|
157 |
|
|
|
158 |
// Process without suppression.
|
|
|
159 |
$content = '<?php '.PHP_EOL.'$var = FALSE;';
|
|
|
160 |
$phpcs->processFile('noSuppressionTest.php', $content);
|
|
|
161 |
|
|
|
162 |
$files = $phpcs->getFiles();
|
|
|
163 |
$file = $files[0];
|
|
|
164 |
|
|
|
165 |
$errors = $file->getErrors();
|
|
|
166 |
$numErrors = $file->getErrorCount();
|
|
|
167 |
$this->assertEquals(1, $numErrors);
|
|
|
168 |
$this->assertEquals(1, count($errors));
|
|
|
169 |
$this->assertEquals(1, count($files));
|
|
|
170 |
|
|
|
171 |
// Process with suppression.
|
|
|
172 |
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreFile'.PHP_EOL.'$var = FALSE;';
|
|
|
173 |
$phpcs->processFile('suppressionTest.php', $content);
|
|
|
174 |
|
|
|
175 |
// The file shouldn't even be added to the $files array.
|
|
|
176 |
$files = $phpcs->getFiles();
|
|
|
177 |
$this->assertEquals(1, count($files));
|
|
|
178 |
|
|
|
179 |
}//end testSupressError()
|
|
|
180 |
|
|
|
181 |
|
|
|
182 |
}//end class
|
|
|
183 |
|
|
|
184 |
?>
|