-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContextKeyRuleTest.php
More file actions
121 lines (108 loc) · 3.95 KB
/
Copy pathContextKeyRuleTest.php
File metadata and controls
121 lines (108 loc) · 3.95 KB
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
<?php
declare(strict_types=1);
namespace SfpTest\PHPStan\Psr\Log\Rules;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use Sfp\PHPStan\Psr\Log\Rules\ContextKeyRule;
use function array_pop;
/**
* @extends RuleTestCase<ContextKeyRule>
* @covers \Sfp\PHPStan\Psr\Log\Rules\ContextKeyRule
*/
final class ContextKeyRuleTest extends RuleTestCase
{
/** @var bool */
private $treatPhpDocTypesAsCertain = false;
/** @var null|string */
private $contextKeyOriginalPattern;
protected function getRule(): Rule
{
return new ContextKeyRule($this->treatPhpDocTypesAsCertain, $this->contextKeyOriginalPattern);
}
/**
* @dataProvider provideNonEmptyStringKeyPattern
* @phpstan-param list<array{0:string, 1:int}> $expectedErrors
*/
public function testAlwaysShouldBeCheckedNonEmptyString(bool $treatPhpDocTypesAsCertain, array $expectedErrors): void
{
$this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain;
$this->contextKeyOriginalPattern = null;
$this->analyse([__DIR__ . '/data/contextKey_nonEmptyString.php'], $expectedErrors);
}
/**
* @phpstan-return list<array{treatPhpDocTypesAsCertain: bool, list<array{0:string, 1:int}>}>
*/
public static function provideNonEmptyStringKeyPattern(): array
{
$expectedErrors = [
[
'Parameter $context of logger method Psr\Log\LoggerInterface::info(), key should be non empty string.',
20, // empty string
],
[
'Parameter $context of logger method Psr\Log\LoggerInterface::info(), key should be non empty string.',
21, // integer
],
[
'Parameter $context of logger method Psr\Log\LoggerInterface::info(), key should be non empty string.',
22, // DNumber
],
[
'Parameter $context of logger method Psr\Log\LoggerInterface::info(), key should be non empty string.',
23, // not specified
],
[
'Parameter $context of logger method Psr\Log\LoggerInterface::log(), key should be non empty string.',
24, // log method call
],
[
'Parameter $context of logger method Psr\Log\LoggerInterface::info(), key should be non empty string.',
29, // inline array
],
[
'Parameter $context of logger method Psr\Log\LoggerInterface::info(), key should be non empty string.',
32, // union parameter
],
];
$expectedErrors1 = $expectedErrors;
array_pop($expectedErrors);
return [
[
'treatPhpDocTypesAsCertain' => true,
$expectedErrors1,
],
[
'treatPhpDocTypesAsCertain' => false,
$expectedErrors,
],
];
}
public function testWithPattern(): void
{
$this->contextKeyOriginalPattern = '#\A[A-Za-z0-9-]+\z#';
$this->analyse([__DIR__ . '/data/contextKey_originalPattern.php'], [
[
'Parameter $context of logger method Psr\Log\LoggerInterface::info(), key should be match #\A[A-Za-z0-9-]+\z#.',
14,
],
]);
}
public function testWithBadRegex(): void
{
$this->contextKeyOriginalPattern = '#\A[A-Za-z0-9-]+';
$this->analyse([__DIR__ . '/data/contextKey_originalPattern.php'], [
[
'Your contextKeyOriginalPattern #\A[A-Za-z0-9-]+ seems not valid regex. Failed.',
8,
],
[
'Your contextKeyOriginalPattern #\A[A-Za-z0-9-]+ seems not valid regex. Failed.',
9,
],
[
'Your contextKeyOriginalPattern #\A[A-Za-z0-9-]+ seems not valid regex. Failed.',
14,
],
]);
}
}