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