Skip to content

Commit 0d31521

Browse files
committed
ErrorSuppressionTest::testSuppressSomeErrors(): refactor to data provider
* Maintains the exact same existing tests, now using a data provider. The data provider uses keys which allows for much more descriptive output when using the PHPUnit `--testdox` option, as well as for easier debugging if/when a test would fail. * Orders the tests in logical groups in the data provider. * Switches out `assertEquals()` (loose type comparison) for `assertSame()` (strict type comparison). * Caches the `$config` and `$ruleset` for the test via static local variables to prevent the test run from becoming slower due to the change to the data provider.
1 parent 7364463 commit 0d31521

File tree

1 file changed

+73
-76
lines changed

1 file changed

+73
-76
lines changed

tests/Core/ErrorSuppressionTest.php

Lines changed: 73 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -154,97 +154,94 @@ public function dataSuppressError()
154154
/**
155155
* Test suppressing 1 out of 2 errors.
156156
*
157+
* @param string $before Annotation to place before the code.
158+
* @param string $between Annotation to place between the code.
159+
* @param int $expectedErrors Optional. Number of errors expected.
160+
* Defaults to 1.
161+
*
162+
* @dataProvider dataSuppressSomeErrors
163+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
164+
*
157165
* @return void
158166
*/
159-
public function testSuppressSomeErrors()
167+
public function testSuppressSomeErrors($before, $between, $expectedErrors=1)
160168
{
161-
$config = new Config();
162-
$config->standards = ['Generic'];
163-
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];
164-
165-
$ruleset = new Ruleset($config);
166-
167-
// Process without suppression.
168-
$content = '<?php '.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;';
169-
$file = new DummyFile($content, $ruleset, $config);
170-
$file->process();
171-
172-
$errors = $file->getErrors();
173-
$numErrors = $file->getErrorCount();
174-
$this->assertEquals(2, $numErrors);
175-
$this->assertCount(2, $errors);
176-
177-
// Process with suppression.
178-
$content = '<?php '.PHP_EOL.'// phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'// phpcs:enable'.PHP_EOL.'$var = TRUE;';
179-
$file = new DummyFile($content, $ruleset, $config);
180-
$file->process();
181-
182-
$errors = $file->getErrors();
183-
$numErrors = $file->getErrorCount();
184-
$this->assertEquals(1, $numErrors);
185-
$this->assertCount(1, $errors);
186-
187-
// Process with @ suppression.
188-
$content = '<?php '.PHP_EOL.'// @phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'// @phpcs:enable'.PHP_EOL.'$var = TRUE;';
189-
$file = new DummyFile($content, $ruleset, $config);
190-
$file->process();
191-
192-
$errors = $file->getErrors();
193-
$numErrors = $file->getErrorCount();
194-
$this->assertEquals(1, $numErrors);
195-
$this->assertCount(1, $errors);
169+
static $config, $ruleset;
196170

197-
// Process with suppression (hash comment).
198-
$content = '<?php '.PHP_EOL.'# phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'# phpcs:enable'.PHP_EOL.'$var = TRUE;';
199-
$file = new DummyFile($content, $ruleset, $config);
200-
$file->process();
171+
if (isset($config, $ruleset) === false) {
172+
$config = new Config();
173+
$config->standards = ['Generic'];
174+
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];
201175

202-
$errors = $file->getErrors();
203-
$numErrors = $file->getErrorCount();
204-
$this->assertEquals(1, $numErrors);
205-
$this->assertCount(1, $errors);
176+
$ruleset = new Ruleset($config);
177+
}
206178

207-
// Process with @ suppression (hash comment).
208-
$content = '<?php '.PHP_EOL.'# @phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'# @phpcs:enable'.PHP_EOL.'$var = TRUE;';
179+
$content = <<<EOD
180+
<?php
181+
$before
182+
\$var = FALSE;
183+
$between
184+
\$var = TRUE;
185+
EOD;
209186
$file = new DummyFile($content, $ruleset, $config);
210187
$file->process();
211188

212-
$errors = $file->getErrors();
213-
$numErrors = $file->getErrorCount();
214-
$this->assertEquals(1, $numErrors);
215-
$this->assertCount(1, $errors);
216-
217-
// Process with suppression (deprecated syntax).
218-
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreStart'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'// @codingStandardsIgnoreEnd'.PHP_EOL.'$var = TRUE;';
219-
$file = new DummyFile($content, $ruleset, $config);
220-
$file->process();
189+
$this->assertSame($expectedErrors, $file->getErrorCount());
190+
$this->assertCount($expectedErrors, $file->getErrors());
221191

222-
$errors = $file->getErrors();
223-
$numErrors = $file->getErrorCount();
224-
$this->assertEquals(1, $numErrors);
225-
$this->assertCount(1, $errors);
192+
}//end testSuppressSomeErrors()
226193

227-
// Process with a PHPDoc block suppression.
228-
$content = '<?php '.PHP_EOL.'/** phpcs:disable */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'/** phpcs:enable */'.PHP_EOL.'$var = TRUE;';
229-
$file = new DummyFile($content, $ruleset, $config);
230-
$file->process();
231194

232-
$errors = $file->getErrors();
233-
$numErrors = $file->getErrorCount();
234-
$this->assertEquals(1, $numErrors);
235-
$this->assertCount(1, $errors);
195+
/**
196+
* Data provider.
197+
*
198+
* @see testSuppressSomeErrors()
199+
*
200+
* @return array
201+
*/
202+
public function dataSuppressSomeErrors()
203+
{
204+
return [
205+
'no suppression' => [
206+
'before' => '',
207+
'between' => '',
208+
'expectedErrors' => 2,
209+
],
236210

237-
// Process with a PHPDoc block suppression (deprecated syntax).
238-
$content = '<?php '.PHP_EOL.'/** @codingStandardsIgnoreStart */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'/** @codingStandardsIgnoreEnd */'.PHP_EOL.'$var = TRUE;';
239-
$file = new DummyFile($content, $ruleset, $config);
240-
$file->process();
211+
// With suppression.
212+
'disable/enable: slash comment' => [
213+
'before' => '// phpcs:disable',
214+
'between' => '// phpcs:enable',
215+
],
216+
'disable/enable: slash comment, with @' => [
217+
'before' => '// @phpcs:disable',
218+
'between' => '// @phpcs:enable',
219+
],
220+
'disable/enable: hash comment' => [
221+
'before' => '# phpcs:disable',
222+
'between' => '# phpcs:enable',
223+
],
224+
'disable/enable: hash comment, with @' => [
225+
'before' => '# @phpcs:disable',
226+
'between' => '# @phpcs:enable',
227+
],
228+
'disable/enable: single line docblock comment' => [
229+
'before' => '/** phpcs:disable */',
230+
'between' => '/** phpcs:enable */',
231+
],
241232

242-
$errors = $file->getErrors();
243-
$numErrors = $file->getErrorCount();
244-
$this->assertEquals(1, $numErrors);
245-
$this->assertCount(1, $errors);
233+
// Deprecated syntax.
234+
'old style: slash comment' => [
235+
'before' => '// @codingStandardsIgnoreStart',
236+
'between' => '// @codingStandardsIgnoreEnd',
237+
],
238+
'old style: single line docblock comment' => [
239+
'before' => '/** @codingStandardsIgnoreStart */',
240+
'between' => '/** @codingStandardsIgnoreEnd */',
241+
],
242+
];
246243

247-
}//end testSuppressSomeErrors()
244+
}//end dataSuppressSomeErrors()
248245

249246

250247
/**

0 commit comments

Comments
 (0)