Skip to content

Commit 32312bd

Browse files
committed
Refactored custom sniffer tests
1 parent 04c5476 commit 32312bd

6 files changed

Lines changed: 41 additions & 46 deletions

File tree

tests/Fixtures/SnifferTestRunner.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class SnifferTestRunner
2424
private ?Config $config;
2525
private array $properties;
2626

27-
public function __construct(string $sniffClass)
27+
public function __construct(string $sniffClass, array $options = [])
2828
{
2929
$runner = SnifferTokens::runner(__DIR__ . '/tests.phpcs.xml');
3030

@@ -36,6 +36,7 @@ public function __construct(string $sniffClass)
3636
$code = Util\Common::getSniffCode($sniffClass);
3737
$this->ruleset->ruleset[$code]['properties'] = [];
3838
$this->properties = &$this->ruleset->ruleset[$code]['properties'];
39+
$this->setProperties($options);
3940
}
4041

4142
public function sniff(string $filename): Files\File
@@ -48,7 +49,7 @@ public function sniff(string $filename): Files\File
4849
return $testFile;
4950
}
5051

51-
public function setProperties(array $properties): void
52+
private function setProperties(array $properties): void
5253
{
5354
$this->properties = array_map(fn ($value) => ['scope' => 'sniff', 'value' => $value], $properties);
5455
}

tests/Sniffer/Sniffs/Arrays/AmbiguousAssociativitySniffTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ class AmbiguousAssociativitySniffTest extends SnifferTest
1919
{
2020
public function test_AssocArray_WithNonAssocValues_GivesWarning()
2121
{
22-
$this->assertWarningLines('./tests/Fixtures/code-samples/Sniffs/InvalidArrays.php', [5, 20, 22, 27]);
22+
$this->assertWarningLines([5, 20, 22, 27], 'InvalidArrays.php');
2323
}
2424

25-
protected function sniffer(): string
25+
protected function sniffClass(): string
2626
{
2727
return AmbiguousAssociativitySniff::class;
2828
}

tests/Sniffer/Sniffs/NamingConventions/ValidVariableNameSniffTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717

1818
class ValidVariableNameSniffTest extends SnifferTest
1919
{
20-
public function test_Number_InLocalVariableName_GivesWarning()
20+
public function test_LocalVariableName_WithNumber_GivesWarning()
2121
{
22-
$lines = [16, 17, 34, 36, 56, 68];
23-
$this->assertWarningLines('./tests/Fixtures/code-samples/Sniffs/InvalidVariableNames.php', $lines);
22+
$warningLines = [16, 17, 34, 36, 56, 68];
23+
$this->assertWarningLines($warningLines, 'InvalidVariableNames.php');
2424
}
2525

26-
public function test_InvalidCamelCaseOrNumber_InClassVariable_GivesError(): void
26+
public function test_ClassVariableName_WithNoCamelCaseOrNumber_GivesError()
2727
{
28-
$lines = [13, 14, 15, 24, 25, 26, 27, 30, 31, 32, 34, 36, 43, 44, 53, 59, 60, 61, 69, 85];
29-
$this->assertErrorLines('./tests/Fixtures/code-samples/Sniffs/InvalidVariableNames.php', $lines);
28+
$errorLines = [13, 14, 15, 24, 25, 26, 27, 30, 31, 32, 34, 36, 43, 44, 53, 59, 60, 61, 69, 85];
29+
$this->assertErrorLines($errorLines, 'InvalidVariableNames.php');
3030
}
3131

32-
protected function sniffer(): string
32+
protected function sniffClass(): string
3333
{
3434
return ValidVariableNameSniff::class;
3535
}

tests/Sniffer/Sniffs/PhpDoc/CallableDefinitionSniffTest.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,25 @@
1717

1818
class CallableDefinitionSniffTest extends SnifferTest
1919
{
20-
/** @dataProvider properties */
21-
public function test_CallableParamDoc_WithoutDefinition_GivesWarning(array $properties, array $expectedWarningLines)
20+
/** @dataProvider warningsForProperties */
21+
public function test_CallableParamDoc_WithoutDefinition_GivesWarning(array $warningLines, array $properties)
2222
{
23-
$this->setProperties($properties);
24-
$this->assertWarningLines('./tests/Fixtures/code-samples/Sniffs/PhpDocCallableDefinitions.php', $expectedWarningLines);
23+
$this->assertWarningLines($warningLines, 'PhpDocCallableDefinitions.php', $properties);
2524
}
2625

27-
public static function properties(): iterable
26+
public static function warningsForProperties(): iterable
2827
{
2928
return [
30-
[['syntax' => 'both', 'includeClosure' => false], range(15, 18)],
31-
[['syntax' => 'both', 'includeClosure' => true], range(15, 22)],
32-
[['syntax' => 'short', 'includeClosure' => false], array_merge(range(15, 18), [27, 28, 31])],
33-
[['syntax' => 'long', 'includeClosure' => false], array_merge(range(15, 18), [23, 24], [33])],
34-
[['syntax' => 'short', 'includeClosure' => true], array_merge(range(15, 22), range(27, 31))],
35-
[['syntax' => 'long', 'includeClosure' => true], array_merge(range(15, 26), [32, 33, 34])]
29+
'short+long closure(-)' => [range(15, 18), ['syntax' => 'both', 'includeClosure' => false]],
30+
'short+long closure(+)' => [range(15, 22), ['syntax' => 'both', 'includeClosure' => true]],
31+
'short closure (-)' => [array_merge(range(15, 18), [27, 28, 31]), ['syntax' => 'short', 'includeClosure' => false]],
32+
'long closure (-)' => [array_merge(range(15, 18), [23, 24], [33]), ['syntax' => 'long', 'includeClosure' => false]],
33+
'short closure (+)' => [array_merge(range(15, 22), range(27, 31)), ['syntax' => 'short', 'includeClosure' => true]],
34+
'long closure (+)' => [array_merge(range(15, 26), [32, 33, 34]), ['syntax' => 'long', 'includeClosure' => true]]
3635
];
3736
}
3837

39-
protected function sniffer(): string
38+
protected function sniffClass(): string
4039
{
4140
return CallableDefinitionSniff::class;
4241
}

tests/Sniffer/Sniffs/PhpDoc/RequiredForPublicApiSniffTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@
1818
class RequiredForPublicApiSniffTest extends SnifferTest
1919
{
2020
/** @dataProvider classFileWarnings */
21-
public function test_Interface_Warnings(string $filename, array $warningLines)
21+
public function test_Interface_Warnings(array $warningLines, string $filename)
2222
{
23-
$this->assertWarningLines('./tests/Fixtures/code-samples/Sniffs/' . $filename, $warningLines);
23+
$this->assertWarningLines($warningLines, $filename);
2424
}
2525

2626
public static function classFileWarnings(): iterable
2727
{
2828
return [
29-
'interface' => ['PhpDocRequiredForInterfaceApi.php', [12]],
30-
'class' => ['PhpDocRequiredForClassApi.php', [14]],
31-
'parent' => ['PhpDocRequiredForParentApi.php', [8]],
32-
'invalid' => ['PhpDocRequiredForInvalidClass.php', [8]]
29+
'interface' => [[12], 'PhpDocRequiredForInterfaceApi.php'],
30+
'class' => [[14], 'PhpDocRequiredForClassApi.php'],
31+
'parent' => [[8], 'PhpDocRequiredForParentApi.php'],
32+
'invalid' => [[8], 'PhpDocRequiredForInvalidClass.php']
3333
];
3434
}
3535

36-
protected function sniffer(): string
36+
protected function sniffClass(): string
3737
{
3838
return RequiredForPublicApiSniff::class;
3939
}

tests/SnifferTest.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,29 @@
1212
namespace Polymorphine\Dev\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use PHP_CodeSniffer\Files\File;
1516
use Polymorphine\Dev\Tests\Fixtures\SnifferTestRunner;
1617

1718

1819
abstract class SnifferTest extends TestCase
1920
{
20-
private SnifferTestRunner $runner;
21-
22-
protected function setUp(): void
21+
public function assertWarningLines(array $expectedWarningLines, string $filename, array $options = []): void
2322
{
24-
$this->runner = new SnifferTestRunner($this->sniffer());
23+
$file = $this->sniffedFile($filename, $options);
24+
$this->assertEquals($expectedWarningLines, array_keys($file->getWarnings()));
2525
}
2626

27-
public function setProperties(array $properties): void
27+
public function assertErrorLines(array $expectedErrorLines, string $filename, array $options = []): void
2828
{
29-
$this->runner->setProperties($properties);
29+
$file = $this->sniffedFile($filename, $options);
30+
$this->assertEquals($expectedErrorLines, array_keys($file->getErrors()));
3031
}
3132

32-
public function assertWarningLines(string $filename, array $expectedWarningLines): void
33-
{
34-
$fileWarnings = $this->runner->sniff($filename)->getWarnings();
35-
$this->assertEquals($expectedWarningLines, array_keys($fileWarnings));
36-
}
33+
abstract protected function sniffClass(): string;
3734

38-
public function assertErrorLines(string $filename, array $expectedErrorLines): void
35+
private function sniffedFile(string $filename, array $options = []): File
3936
{
40-
$fileErrors = $this->runner->sniff($filename)->getErrors();
41-
$this->assertEquals($expectedErrorLines, array_keys($fileErrors));
37+
$runner = new SnifferTestRunner($this->sniffClass(), $options);
38+
return $runner->sniff('./tests/Fixtures/code-samples/Sniffs/' . $filename);
4239
}
43-
44-
abstract protected function sniffer(): string;
4540
}

0 commit comments

Comments
 (0)