Skip to content

Commit 58e8734

Browse files
committed
ErrorSuppressionTest::testSuppressWarning(): 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 0d31521 commit 58e8734

File tree

1 file changed

+64
-56
lines changed

1 file changed

+64
-56
lines changed

tests/Core/ErrorSuppressionTest.php

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -247,77 +247,85 @@ public function dataSuppressSomeErrors()
247247
/**
248248
* Test suppressing a single warning.
249249
*
250+
* @param string $before Annotation to place before the code.
251+
* @param string $after Annotation to place after the code.
252+
* @param int $expectedWarnings Optional. Number of warnings expected.
253+
* Defaults to 0.
254+
*
255+
* @dataProvider dataSuppressWarning
256+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
257+
*
250258
* @return void
251259
*/
252-
public function testSuppressWarning()
260+
public function testSuppressWarning($before, $after, $expectedWarnings=0)
253261
{
254-
$config = new Config();
255-
$config->standards = ['Generic'];
256-
$config->sniffs = ['Generic.Commenting.Todo'];
257-
258-
$ruleset = new Ruleset($config);
259-
260-
// Process without suppression.
261-
$content = '<?php '.PHP_EOL.'//TODO: write some code';
262-
$file = new DummyFile($content, $ruleset, $config);
263-
$file->process();
264-
265-
$warnings = $file->getWarnings();
266-
$numWarnings = $file->getWarningCount();
267-
$this->assertEquals(1, $numWarnings);
268-
$this->assertCount(1, $warnings);
262+
static $config, $ruleset;
269263

270-
// Process with suppression.
271-
$content = '<?php '.PHP_EOL.'// phpcs:disable'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:enable';
272-
$file = new DummyFile($content, $ruleset, $config);
273-
$file->process();
264+
if (isset($config, $ruleset) === false) {
265+
$config = new Config();
266+
$config->standards = ['Generic'];
267+
$config->sniffs = ['Generic.Commenting.Todo'];
274268

275-
$warnings = $file->getWarnings();
276-
$numWarnings = $file->getWarningCount();
277-
$this->assertEquals(0, $numWarnings);
278-
$this->assertCount(0, $warnings);
269+
$ruleset = new Ruleset($config);
270+
}
279271

280-
// Process with @ suppression.
281-
$content = '<?php '.PHP_EOL.'// @phpcs:disable'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// @phpcs:enable';
272+
$content = <<<EOD
273+
<?php
274+
$before
275+
//TODO: write some code.
276+
$after
277+
EOD;
282278
$file = new DummyFile($content, $ruleset, $config);
283279
$file->process();
284280

285-
$warnings = $file->getWarnings();
286-
$numWarnings = $file->getWarningCount();
287-
$this->assertEquals(0, $numWarnings);
288-
$this->assertCount(0, $warnings);
281+
$this->assertSame($expectedWarnings, $file->getWarningCount());
282+
$this->assertCount($expectedWarnings, $file->getWarnings());
289283

290-
// Process with suppression (deprecated syntax).
291-
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreStart'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// @codingStandardsIgnoreEnd';
292-
$file = new DummyFile($content, $ruleset, $config);
293-
$file->process();
294-
295-
$warnings = $file->getWarnings();
296-
$numWarnings = $file->getWarningCount();
297-
$this->assertEquals(0, $numWarnings);
298-
$this->assertCount(0, $warnings);
284+
}//end testSuppressWarning()
299285

300-
// Process with a docblock suppression.
301-
$content = '<?php '.PHP_EOL.'/** phpcs:disable */'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'/** phpcs:enable */';
302-
$file = new DummyFile($content, $ruleset, $config);
303-
$file->process();
304286

305-
$warnings = $file->getWarnings();
306-
$numWarnings = $file->getWarningCount();
307-
$this->assertEquals(0, $numWarnings);
308-
$this->assertCount(0, $warnings);
287+
/**
288+
* Data provider.
289+
*
290+
* @see testSuppressWarning()
291+
*
292+
* @return array
293+
*/
294+
public function dataSuppressWarning()
295+
{
296+
return [
297+
'no suppression' => [
298+
'before' => '',
299+
'after' => '',
300+
'expectedWarnings' => 1,
301+
],
309302

310-
// Process with a docblock suppression (deprecated syntax).
311-
$content = '<?php '.PHP_EOL.'/** @codingStandardsIgnoreStart */'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'/** @codingStandardsIgnoreEnd */';
312-
$file = new DummyFile($content, $ruleset, $config);
313-
$file->process();
303+
// With suppression.
304+
'disable/enable: slash comment' => [
305+
'before' => '// phpcs:disable',
306+
'after' => '// phpcs:enable',
307+
],
308+
'disable/enable: slash comment, with @' => [
309+
'before' => '// @phpcs:disable',
310+
'after' => '// @phpcs:enable',
311+
],
312+
'disable/enable: single line docblock comment' => [
313+
'before' => '/** phpcs:disable */',
314+
'after' => '/** phpcs:enable */',
315+
],
314316

315-
$warnings = $file->getWarnings();
316-
$numWarnings = $file->getWarningCount();
317-
$this->assertEquals(0, $numWarnings);
318-
$this->assertCount(0, $warnings);
317+
// Deprecated syntax.
318+
'old style: slash comment' => [
319+
'before' => '// @codingStandardsIgnoreStart',
320+
'after' => '// @codingStandardsIgnoreEnd',
321+
],
322+
'old style: single line docblock comment' => [
323+
'before' => '/** @codingStandardsIgnoreStart */',
324+
'after' => '/** @codingStandardsIgnoreEnd */',
325+
],
326+
];
319327

320-
}//end testSuppressWarning()
328+
}//end dataSuppressWarning()
321329

322330

323331
/**

0 commit comments

Comments
 (0)