Completed
Push — add/changelog-tooling ( 7f5585...86359e )
by
unknown
58:45 queued 48:46
created

ValidateCommandTest::provideExecute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 138

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 138
rs 8
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
/**
3
 * Tests for the changelogger validate command.
4
 *
5
 * @package automattic/jetpack-changelogger
6
 */
7
8
// phpcs:disable WordPress.WP.AlternativeFunctions, WordPress.NamingConventions.ValidVariableName
9
10
namespace Automattic\Jetpack\Changelogger\Tests;
11
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Tests for the changelogger validate command.
16
 *
17
 * @covers \Automattic\Jetpack\Changelogger\ValidateCommand
18
 */
19
class ValidateCommandTest extends CommandTestCase {
20
	use \Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames;
21
22
	/**
23
	 * Set up.
24
	 *
25
	 * @before
26
	 */
27
	public function set_up() {
28
		parent::set_up();
29
		$this->useTempDir();
30
31
		mkdir( 'changelog' );
32
		file_put_contents( 'changelog/.gitkeep', '' );
33
		file_put_contents( 'changelog/good', "Significance: minor\nType: added\nComment: This is a comment\n\nEntry.\n" );
34
		file_put_contents( 'changelog/no-entry-is-patch', "Significance: patch\nType: added\nComment: This is a comment\n\n" );
35
		file_put_contents( 'changelog/unknown-header', "Significance: minor\nType: added\nBogus: foo\n\nEntry.\n" );
36
		file_put_contents( 'changelog/no-entry-not-patch', "Significance: minor\nType: added\nComment: This is a comment\n\n" );
37
		file_put_contents( 'changelog/wrong-headers', "Significants: patch\nTypo: added\nComment: This is a comment\n\nEntry." );
38
		file_put_contents( 'changelog/wrong-header-values', "Significance: bogus\nType: bogus" );
39
		file_put_contents( 'changelog/duplicate-headers', "Significance: patch\nType: fixed\nType: added\n\nOk?" );
40
		file_put_contents( 'changelog/custom-type', "Significance: patch\nType: foo\n\nOk?" );
41
		file_put_contents( 'changelog/no-type', "Significance: patch\n\nOk?" );
42
	}
43
44
	/**
45
	 * Test the command.
46
	 *
47
	 * @dataProvider provideExecute
48
	 * @param string[] $args Command line arguments.
49
	 * @param array    $options Options for the test and CommandTester.
50
	 * @param int      $expectExitCode Expected exit code.
51
	 * @param string   $expectOutput Expected output.
52
	 */
53
	public function testExecute( array $args, array $options, $expectExitCode, $expectOutput ) {
54 View Code Duplication
		if ( isset( $options['composer.json'] ) ) {
55
			file_put_contents( 'composer.json', json_encode( $options['composer.json'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) );
56
			unset( $options['composer.json'] );
57
		}
58
59
		$tester = $this->getTester( 'validate' );
60
		$code   = $tester->execute( $args, $options );
61
		$output = str_replace( getcwd() . '/', '/base/path/', rtrim( $tester->getDisplay() ) );
62
		if ( preg_match( '/^XXX \d+$/', $expectOutput ) ) {
63
			$tmp = file_get_contents( __FILE__ );
64
			$tmp = preg_replace( "/^$expectOutput$/m", $output, $tmp );
65
			file_put_contents( __FILE__, $tmp );
66
		}
67
		$this->assertSame( $expectOutput, $output );
68
		$this->assertSame( $expectExitCode, $code );
69
	}
70
71
	/**
72
	 * Data provider for testExecute.
73
	 */
74
	public function provideExecute() {
75
		$composerWithTypes   = array(
76
			'extra' => array(
77
				'changelogger' => array(
78
					'types' => array(
79
						'foo' => 'Foo',
80
						'bar' => 'Bar',
81
					),
82
				),
83
			),
84
		);
85
		$composerWithNoTypes = array(
86
			'extra' => array(
87
				'changelogger' => array(
88
					'types' => (object) array(),
89
				),
90
			),
91
		);
92
93
		return array(
94
			'Normal run'                => array(
95
				array(),
96
				array(),
97
				1,
98
				<<<'EOF'
99
/base/path/changelog/custom-type:2: Type must be "security", "added", "changed", "deprecated", "removed", or "fixed".
100
<warning>/base/path/changelog/duplicate-headers:3: Duplicate header "Type", previously seen on line 2.
101
/base/path/changelog/no-entry-not-patch:5: Changelog entry may only be empty when Significance is "patch".
102
/base/path/changelog/no-type: File does not contain a Type header.
103
<warning>/base/path/changelog/unknown-header:3: Unrecognized header "Bogus".
104
/base/path/changelog/wrong-header-values:1: Significance must be "patch", "minor", or "major".
105
/base/path/changelog/wrong-header-values:2: Type must be "security", "added", "changed", "deprecated", "removed", or "fixed".
106
/base/path/changelog/wrong-headers: File does not contain a Significance header.
107
/base/path/changelog/wrong-headers: File does not contain a Type header.
108
<warning>/base/path/changelog/wrong-headers:1: Unrecognized header "Significants".
109
<warning>/base/path/changelog/wrong-headers:2: Unrecognized header "Typo".
110
EOF
111
				,
112
			),
113
			'Verbose run'               => array(
114
				array( '-v' ),
115
				array( 'verbosity' => OutputInterface::VERBOSITY_VERBOSE ),
116
				1,
117
				<<<'EOF'
118
Checking /base/path/changelog/custom-type...
119
/base/path/changelog/custom-type:2: Type must be "security", "added", "changed", "deprecated", "removed", or "fixed".
120
Checking /base/path/changelog/duplicate-headers...
121
<warning>/base/path/changelog/duplicate-headers:3: Duplicate header "Type", previously seen on line 2.
122
Checking /base/path/changelog/good...
123
Checking /base/path/changelog/no-entry-is-patch...
124
Checking /base/path/changelog/no-entry-not-patch...
125
/base/path/changelog/no-entry-not-patch:5: Changelog entry may only be empty when Significance is "patch".
126
Checking /base/path/changelog/no-type...
127
/base/path/changelog/no-type: File does not contain a Type header.
128
Checking /base/path/changelog/unknown-header...
129
<warning>/base/path/changelog/unknown-header:3: Unrecognized header "Bogus".
130
Checking /base/path/changelog/wrong-header-values...
131
/base/path/changelog/wrong-header-values:1: Significance must be "patch", "minor", or "major".
132
/base/path/changelog/wrong-header-values:2: Type must be "security", "added", "changed", "deprecated", "removed", or "fixed".
133
Checking /base/path/changelog/wrong-headers...
134
/base/path/changelog/wrong-headers: File does not contain a Significance header.
135
/base/path/changelog/wrong-headers: File does not contain a Type header.
136
<warning>/base/path/changelog/wrong-headers:1: Unrecognized header "Significants".
137
<warning>/base/path/changelog/wrong-headers:2: Unrecognized header "Typo".
138
Found 7 error(s) and 4 warning(s)
139
EOF
140
				,
141
			),
142
			'Specific file'             => array(
143
				array( 'files' => array( 'changelog/good' ) ),
144
				array(),
145
				0,
146
				'',
147
			),
148
			'Only warnings'             => array(
149
				array( 'files' => array( 'changelog/unknown-header' ) ),
150
				array(),
151
				1,
152
				'<warning>changelog/unknown-header:3: Unrecognized header "Bogus".',
153
			),
154
			'Only warnings, non-strict' => array(
155
				array(
156
					'--no-strict' => true,
157
					'files'       => array( 'changelog/unknown-header' ),
158
				),
159
				array(),
160
				0,
161
				'<warning>changelog/unknown-header:3: Unrecognized header "Bogus".',
162
			),
163
			'Multiple specific files'   => array(
164
				array( 'files' => array( 'changelog/.', 'changelog/..', 'changelog/.gitkeep' ) ),
165
				array(),
166
				1,
167
				<<<'EOF'
168
changelog/.: Expected a file, got dir.
169
changelog/..: Expected a file, got dir.
170
changelog/.gitkeep: File does not contain a Significance header.
171
changelog/.gitkeep: File does not contain a Type header.
172
EOF
173
				,
174
			),
175
			'Custom types'              => array(
176
				array( 'files' => array( 'changelog/good', 'changelog/custom-type', 'changelog/no-type' ) ),
177
				array( 'composer.json' => $composerWithTypes ),
178
				1,
179
				<<<'EOF'
180
changelog/good:2: Type must be "foo" or "bar".
181
changelog/no-type: File does not contain a Type header.
182
EOF
183
				,
184
			),
185
			'No types'                  => array(
186
				array( 'files' => array( 'changelog/good', 'changelog/custom-type', 'changelog/no-type' ) ),
187
				array( 'composer.json' => $composerWithNoTypes ),
188
				0,
189
				'',
190
			),
191
			'GH Actions output'         => array(
192
				array( '--gh-action' => true ),
193
				array(),
194
				1,
195
				<<<'EOF'
196
::error file=/base/path/changelog/custom-type,line=2::Type must be "security", "added", "changed", "deprecated", "removed", or "fixed".
197
::warning file=/base/path/changelog/duplicate-headers,line=3::Duplicate header "Type", previously seen on line 2.
198
::error file=/base/path/changelog/no-entry-not-patch,line=5::Changelog entry may only be empty when Significance is "patch".
199
::error file=/base/path/changelog/no-type::File does not contain a Type header.
200
::warning file=/base/path/changelog/unknown-header,line=3::Unrecognized header "Bogus".
201
::error file=/base/path/changelog/wrong-header-values,line=1::Significance must be "patch", "minor", or "major".
202
::error file=/base/path/changelog/wrong-header-values,line=2::Type must be "security", "added", "changed", "deprecated", "removed", or "fixed".
203
::error file=/base/path/changelog/wrong-headers::File does not contain a Significance header.
204
::error file=/base/path/changelog/wrong-headers::File does not contain a Type header.
205
::warning file=/base/path/changelog/wrong-headers,line=1::Unrecognized header "Significants".
206
::warning file=/base/path/changelog/wrong-headers,line=2::Unrecognized header "Typo".
207
EOF
208
				,
209
			),
210
		);
211
	}
212
213
}
214