-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathFixtureTests.php
188 lines (163 loc) · 4.87 KB
/
FixtureTests.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
184
185
186
187
188
<?php
/**
* Run tests on fixture files against our custom standards.
*
* This test suite runs our standards against files which have
* known errors or known passing conditions. We run these tests
* against said fixture files as it's closer to real-world conditions
* than isolated unit tests and provides another layer of security.
*/
namespace HM\CodingStandards\Tests;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Files\LocalFile;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
/**
* Class FixtureTests
*
* @group fixtures
*/
class FixtureTests extends TestCase {
/**
* Config instance.
*
* @var \PHP_CodeSniffer\Config
*/
protected $config;
/**
* Ruleset instance.
*
* @var \PHP_CodeSniffer\Ruleset
*/
protected $ruleset;
/**
* Get a lit of files from a directory path.
*
* @param string $directory Directory to recursively look through.
* @return array List of files to run.
*/
public static function get_files_from_dir( string $directory ) {
$files = [];
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $directory )
);
foreach ( $iterator as $path => $file ) {
if ( ! $file->isFile() || $file->getExtension() === 'json' ) {
continue;
}
$files[] = [ $path ];
}
return $files;
}
/**
* Get files from the pass fixtures directory.
*
* @return array List of parameters to provide.
*/
public static function failing_files() {
$directory = __DIR__ . '/fixtures/fail';
return static::get_files_from_dir( $directory );
}
/**
* Get files from the pass fixtures directory.
*
* @return array List of parameters to provide.
*/
public static function passing_files() {
$directory = __DIR__ . '/fixtures/pass';
return static::get_files_from_dir( $directory );
}
/**
* Setup our ruleset.
*/
public function setUp() {
$this->config = new Config();
$this->config->cache = false;
$this->config->standards = [ 'HM' ];
// Keeping the tabWidth set inline with WPCS.
// See: https://github.com/humanmade/coding-standards/pull/88#issuecomment-464076803
$this->config->tabWidth = 4;
// We want to setup our tests to only load our standards in for testing.
$this->config->sniffs = [
'HM.Classes.OnlyClassInFile',
'HM.Debug.ESLint',
'HM.Files.ClassFileName',
'HM.Files.FunctionFileName',
'HM.Files.NamespaceDirectoryName',
'HM.Functions.NamespacedFunctions',
'HM.Layout.Order',
'HM.Namespaces.NoLeadingSlashOnUse',
'HM.Performance.SlowMetaQuery',
'HM.Performance.SlowOrderBy',
'HM.PHP.Isset',
'HM.Security.EscapeOutput',
'HM.Security.NonceVerification',
'HM.Security.ValidatedSanitizedInput',
'HM.Whitespace.MultipleEmptyLines',
];
$this->ruleset = new Ruleset( $this->config );
// Set configuration as needed too.
$this->ruleset->setSniffProperty( 'HM\\Sniffs\\Security\\EscapeOutputSniff', 'customAutoEscapedFunctions', [
'my_custom_func',
'another_func',
] );
$this->ruleset->setSniffProperty( 'HM\\Sniffs\\Security\\NonceVerificationSniff', 'allowQueryVariables', true );
}
/**
* @dataProvider passing_files
*/
public function test_passing_files( $file ) {
$phpcsFile = new LocalFile( $file, $this->ruleset, $this->config );
$phpcsFile->process();
$rel_file = substr( $file, strlen( __DIR__ ) );
$foundErrors = $phpcsFile->getErrors();
$this->assertEquals( [], $foundErrors, sprintf( 'File %s should not contain any errors', $rel_file ) );
$foundWarnings = $phpcsFile->getWarnings();
$this->assertEquals( [], $foundWarnings, sprintf( 'File %s should not contain any warnings', $rel_file ) );
}
/**
* @dataProvider failing_files
*/
public function test_failing_files( $file ) {
$phpcsFile = new LocalFile( $file, $this->ruleset, $this->config );
$phpcsFile->process();
$rel_file = substr( $file, strlen( __DIR__ ) );
$foundErrors = $phpcsFile->getErrors();
$foundWarnings = $phpcsFile->getWarnings();
$expected_file = $file . '.json';
$expected = json_decode( file_get_contents( $expected_file ), true );
$this->assertEquals(
JSON_ERROR_NONE,
json_last_error(),
sprintf(
'Expected JSON should be correctly parsed: %s',
json_last_error_msg()
)
);
$found = [];
foreach ( $foundErrors as $line => $columns ) {
foreach ( $columns as $column => $errors ) {
foreach ( $errors as $error ) {
$found[ $line ][] = [
'source' => $error['source'],
'type' => 'error',
];
}
}
}
foreach ( $foundWarnings as $line => $columns ) {
foreach ( $columns as $column => $errors ) {
foreach ( $errors as $error ) {
$found[ $line ][] = [
'source' => $error['source'],
'type' => 'warning',
];
}
}
}
$this->assertEquals( $expected, $found, sprintf( 'File %s should only contain specified errors', $rel_file ) );
// var_dump( $foundErrors );
}
}