|
1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
|
2
|
|
|
/** |
|
3
|
|
|
* Tests for the Jetpack phpcs standard. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-codesniffer |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Sniffs\Tests; |
|
9
|
|
|
|
|
10
|
|
|
use PHP_CodeSniffer\Config; |
|
11
|
|
|
use PHP_CodeSniffer\Files\DummyFile; |
|
12
|
|
|
use PHP_CodeSniffer\Reporter; |
|
13
|
|
|
use PHP_CodeSniffer\Ruleset; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use RecursiveDirectoryIterator; |
|
16
|
|
|
use RecursiveIteratorIterator; |
|
17
|
|
|
use RegexIterator; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Tests for the Jetpack phpcs standard. |
|
21
|
|
|
*/ |
|
22
|
|
|
class JetpackStandardTest extends TestCase { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Run phpcs on a file. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $file File to process. Actual data will be read from "$file.tolint". |
|
28
|
|
|
* @param bool $fix Run in fix mode, returning the fixed file. |
|
29
|
|
|
* @return string If `$fix` is false, the phpcs report. If `$fix` is true, |
|
30
|
|
|
* the fixed file. |
|
31
|
|
|
*/ |
|
32
|
|
|
private function run_phpcs( $file, $fix ) { |
|
33
|
|
|
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
34
|
|
|
$contents = file_get_contents( "{$file}.tolint" ); |
|
35
|
|
|
$this->assertInternalType( 'string', $contents ); |
|
36
|
|
|
|
|
37
|
|
|
$config = new Config(); |
|
38
|
|
|
|
|
39
|
|
|
$config->standards = array( __DIR__ . '/../../../Jetpack/ruleset.xml' ); |
|
40
|
|
|
$config->files = array( $file ); |
|
41
|
|
|
$config->encoding = 'utf-8'; |
|
42
|
|
|
$config->reports = array( 'full' => null ); |
|
43
|
|
|
$config->colors = false; |
|
44
|
|
|
$config->reportWidth = PHP_INT_MAX; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
45
|
|
|
$config->showSources = true; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
46
|
|
|
$config->tabWidth = 4; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
47
|
|
|
|
|
48
|
|
|
$ruleset = new Ruleset( $config ); |
|
49
|
|
|
$dummy = new DummyFile( $contents, $ruleset, $config ); |
|
50
|
|
|
$dummy->process(); |
|
51
|
|
|
|
|
52
|
|
|
if ( ! $fix ) { |
|
53
|
|
|
$reporter = new Reporter( $config ); |
|
54
|
|
|
$reporter->cacheFileReport( $dummy ); |
|
55
|
|
|
ob_start(); |
|
56
|
|
|
$reporter->printReport( 'full' ); |
|
57
|
|
|
$result = ob_get_clean(); |
|
58
|
|
|
|
|
59
|
|
|
// Clean up output. |
|
60
|
|
|
$lines = preg_split( '/[\r\n]+/', $result, -1, PREG_SPLIT_NO_EMPTY ); |
|
61
|
|
|
$lines = preg_grep( '/^-*$|^(?:Time:|FILE:|FOUND|PHPCBF) /', $lines, PREG_GREP_INVERT ); |
|
62
|
|
|
return implode( "\n", $lines ) . "\n"; |
|
63
|
|
|
} elseif ( $dummy->getFixableCount() ) { |
|
64
|
|
|
$dummy->fixer->fixFile(); |
|
65
|
|
|
return $dummy->fixer->getContents(); |
|
66
|
|
|
} else { |
|
67
|
|
|
return $contents; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Test the sniffs by running phpcs or phpcbf against a file. |
|
73
|
|
|
* |
|
74
|
|
|
* @dataProvider provide_files |
|
75
|
|
|
* @param string $file Base filename, without the ".tolint", ".report", or ".fixed" extension. |
|
76
|
|
|
* @param bool $fix Run as phpcbf rather than phpcs. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function test_phpcs( $file, $fix ) { |
|
79
|
|
|
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
80
|
|
|
$expect = file_get_contents( $fix ? "$file.fixed" : "$file.report" ); |
|
81
|
|
|
$this->assertInternalType( 'string', $expect ); |
|
82
|
|
|
$this->assertEquals( $expect, $this->run_phpcs( $file, $fix ) ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Provide arguments for `test_phpcs()`. |
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
public function provide_files() { |
|
91
|
|
|
$dir_iterator = new RecursiveDirectoryIterator( __DIR__ . '/files', RecursiveDirectoryIterator::CURRENT_AS_PATHNAME ); |
|
92
|
|
|
$iterator = new RegexIterator( |
|
93
|
|
|
new RecursiveIteratorIterator( $dir_iterator ), |
|
94
|
|
|
'/\.(?:tolint|report|fixed)$/' |
|
95
|
|
|
); |
|
96
|
|
|
$files = iterator_to_array( $iterator ); |
|
97
|
|
|
|
|
98
|
|
|
$ret = array(); |
|
99
|
|
|
foreach ( $files as $file => $dummy ) { |
|
100
|
|
|
$i = strrpos( $file, '.' ); |
|
101
|
|
|
$ext = substr( $file, $i ); |
|
102
|
|
|
$file = substr( $file, 0, $i ); |
|
103
|
|
|
|
|
104
|
|
|
switch ( $ext ) { |
|
105
|
|
|
case '.tolint': |
|
106
|
|
|
if ( ! isset( $files[ "$file.report" ] ) && ! isset( $files[ "$file.fixed" ] ) ) { |
|
107
|
|
|
fprintf( STDERR, "%s: %s.tolint exists, but both %s.report and %s.fixed are missing.\n", __METHOD__, $file, $file, $file ); |
|
108
|
|
|
} |
|
109
|
|
|
break; |
|
110
|
|
|
|
|
111
|
|
|
case '.report': |
|
112
|
|
|
case '.fixed': |
|
113
|
|
|
if ( isset( $files[ "$file.tolint" ] ) ) { |
|
114
|
|
|
$ret[ "$file$ext" ] = array( $file, '.fixed' === $ext ); |
|
115
|
|
|
} else { |
|
116
|
|
|
fprintf( STDERR, "%s: %s exists, but %s.tolint is missing.\n", __METHOD__, $file . $ext, $file ); |
|
117
|
|
|
} |
|
118
|
|
|
break; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $ret; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|