Completed
Push — add/changelog-tooling ( 3f93f3...c37726 )
by
unknown
250:15 queued 241:25
created

ValidateCommandTest::provideExecute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 96

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 96
rs 8.0872
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\Console;
11
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Tests for the changelogger validate command.
16
 *
17
 * @covers \Automattic\Jetpack\Changelogger\Console\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
	}
41
42
	/**
43
	 * Test the command.
44
	 *
45
	 * @dataProvider provideExecute
46
	 * @param string[] $args Command line arguments.
47
	 * @param array    $options Options for CommandTester.
48
	 * @param int      $expectExitCode Expected exit code.
49
	 * @param string   $expectOutput Expected output.
50
	 */
51
	public function testExecute( array $args, array $options, $expectExitCode, $expectOutput ) {
52
		$tester = $this->getTester( 'validate' );
53
		$code   = $tester->execute( $args, $options );
54
		$output = str_replace( getcwd() . '/', '/base/path/', rtrim( $tester->getDisplay() ) );
55
		if ( preg_match( '/^XXX \d+$/', $expectOutput ) ) {
56
			$tmp = file_get_contents( __FILE__ );
57
			$tmp = preg_replace( "/^$expectOutput$/m", $output, $tmp );
58
			file_put_contents( __FILE__, $tmp );
59
		}
60
		$this->assertSame( $expectOutput, $output );
61
		$this->assertSame( $expectExitCode, $code );
62
	}
63
64
	/**
65
	 * Data provider for testExecute.
66
	 */
67
	public function provideExecute() {
68
		return array(
69
			'Normal run'                => array(
70
				array(),
71
				array(),
72
				1,
73
				<<<'EOF'
74
<warning>/base/path/changelog/duplicate-headers:3: Duplicate header "Type", previously seen on line 2.
75
/base/path/changelog/no-entry-not-patch:5: Changelog entry may only be empty when Significance is "patch".
76
<warning>/base/path/changelog/unknown-header:3: Unrecognized header "Bogus".
77
/base/path/changelog/wrong-header-values:1: Significance must be "patch", "minor", or "major".
78
/base/path/changelog/wrong-header-values:2: Type must be must be "security", "added", "changed", "deprecated", "removed", or "fixed".
79
/base/path/changelog/wrong-headers: File does not contain a Significance header.
80
/base/path/changelog/wrong-headers: File does not contain a Type header.
81
<warning>/base/path/changelog/wrong-headers:1: Unrecognized header "Significants".
82
<warning>/base/path/changelog/wrong-headers:2: Unrecognized header "Typo".
83
EOF
84
				,
85
			),
86
			'Verbose run'               => array(
87
				array( '-v' ),
88
				array( 'verbosity' => OutputInterface::VERBOSITY_VERBOSE ),
89
				1,
90
				<<<'EOF'
91
Checking /base/path/changelog/duplicate-headers...
92
<warning>/base/path/changelog/duplicate-headers:3: Duplicate header "Type", previously seen on line 2.
93
Checking /base/path/changelog/good...
94
Checking /base/path/changelog/no-entry-is-patch...
95
Checking /base/path/changelog/no-entry-not-patch...
96
/base/path/changelog/no-entry-not-patch:5: Changelog entry may only be empty when Significance is "patch".
97
Checking /base/path/changelog/unknown-header...
98
<warning>/base/path/changelog/unknown-header:3: Unrecognized header "Bogus".
99
Checking /base/path/changelog/wrong-header-values...
100
/base/path/changelog/wrong-header-values:1: Significance must be "patch", "minor", or "major".
101
/base/path/changelog/wrong-header-values:2: Type must be must be "security", "added", "changed", "deprecated", "removed", or "fixed".
102
Checking /base/path/changelog/wrong-headers...
103
/base/path/changelog/wrong-headers: File does not contain a Significance header.
104
/base/path/changelog/wrong-headers: File does not contain a Type header.
105
<warning>/base/path/changelog/wrong-headers:1: Unrecognized header "Significants".
106
<warning>/base/path/changelog/wrong-headers:2: Unrecognized header "Typo".
107
Found 5 error(s) and 4 warning(s)
108
EOF
109
				,
110
			),
111
			'Specific file'             => array(
112
				array( 'files' => array( 'changelog/good' ) ),
113
				array(),
114
				0,
115
				'',
116
			),
117
			'Only warnings'             => array(
118
				array( 'files' => array( 'changelog/unknown-header' ) ),
119
				array(),
120
				1,
121
				'<warning>changelog/unknown-header:3: Unrecognized header "Bogus".',
122
			),
123
			'Only warnings, non-strict' => array(
124
				array(
125
					'--no-strict' => true,
126
					'files'       => array( 'changelog/unknown-header' ),
127
				),
128
				array(),
129
				0,
130
				'<warning>changelog/unknown-header:3: Unrecognized header "Bogus".',
131
			),
132
			'Multiple specific files'   => array(
133
				array( 'files' => array( 'changelog/.', 'changelog/..', 'changelog/.gitkeep' ) ),
134
				array(),
135
				1,
136
				<<<'EOF'
137
changelog/.: Expected a file, got dir.
138
changelog/..: Expected a file, got dir.
139
changelog/.gitkeep: File does not contain a Significance header.
140
changelog/.gitkeep: File does not contain a Type header.
141
EOF
142
				,
143
			),
144
			'GH Actions output'         => array(
145
				array( '--gh-action' => true ),
146
				array(),
147
				1,
148
				<<<'EOF'
149
::warning file=/base/path/changelog/duplicate-headers,line=3::Duplicate header "Type", previously seen on line 2.
150
::error file=/base/path/changelog/no-entry-not-patch,line=5::Changelog entry may only be empty when Significance is "patch".
151
::warning file=/base/path/changelog/unknown-header,line=3::Unrecognized header "Bogus".
152
::error file=/base/path/changelog/wrong-header-values,line=1::Significance must be "patch", "minor", or "major".
153
::error file=/base/path/changelog/wrong-header-values,line=2::Type must be must be "security", "added", "changed", "deprecated", "removed", or "fixed".
154
::error file=/base/path/changelog/wrong-headers::File does not contain a Significance header.
155
::error file=/base/path/changelog/wrong-headers::File does not contain a Type header.
156
::warning file=/base/path/changelog/wrong-headers,line=1::Unrecognized header "Significants".
157
::warning file=/base/path/changelog/wrong-headers,line=2::Unrecognized header "Typo".
158
EOF
159
				,
160
			),
161
		);
162
	}
163
164
}
165