Completed
Push — add/changelog-tooling ( b30521...fa9ac3 )
by
unknown
1097:09 queued 1086:59
created

ConfigTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 181
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A set_up() 0 12 1
A writeComposerJson() 0 9 1
A testLoadBeforeSetOutput() 0 7 1
A testLoad() 0 18 2
A provideLoad() 0 29 1
A testBase() 0 11 1
A testChangesDir() 0 21 1
A testTypes() 0 28 1
1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
/**
3
 * Tests for the changelogger config.
4
 *
5
 * @package automattic/jetpack-changelogger
6
 */
7
8
// phpcs:disable WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_putenv, WordPress.WP.AlternativeFunctions, WordPress.NamingConventions.ValidVariableName
9
10
namespace Automattic\Jetpack\Changelogger\Tests;
11
12
use Automattic\Jetpack\Changelogger\Config;
13
use Symfony\Component\Console\Output\BufferedOutput;
14
use Wikimedia\TestingAccessWrapper;
15
16
/**
17
 * Tests for the changelogger config.
18
 *
19
 * @covers \Automattic\Jetpack\Changelogger\Config
20
 */
21
class ConfigTest extends TestCase {
22
	use \Yoast\PHPUnitPolyfills\Polyfills\ExpectException;
23
24
	/**
25
	 * Set up.
26
	 *
27
	 * @before
28
	 */
29
	public function set_up() {
30
		$this->useTempDir();
31
32
		file_put_contents( 'bogus.json', "bogus\n" );
33
		$this->writeComposerJson(
34
			array(
35
				'types'  => (object) array(),
36
				'foobar' => 'baz',
37
			),
38
			'no-types.json'
39
		);
40
	}
41
42
	/**
43
	 * Write a composer.json file.
44
	 *
45
	 * @param array  $config Contents for `.extra.changelogger`.
46
	 * @param string $file Filename.
47
	 */
48
	public function writeComposerJson( array $config, $file = 'composer.json' ) {
49
		file_put_contents(
50
			$file,
51
			json_encode(
52
				array( 'extra' => array( 'changelogger' => $config ) ),
53
				JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
54
			)
55
		);
56
	}
57
58
	/**
59
	 * Test that calling load() before setOutput() throws.
60
	 */
61
	public function testLoadBeforeSetOutput() {
62
		$this->resetConfigCache();
63
64
		$this->expectException( \LogicException::class );
65
		$this->expectExceptionMessage( 'Must call Config::setOutput() before Config::load()' );
66
		TestingAccessWrapper::newFromClass( Config::class )->load();
67
	}
68
69
	/**
70
	 * Test parsing composer.json.
71
	 *
72
	 * @dataProvider provideLoad
73
	 * @param string|false $composer Value for COMPOSER environment variable.
74
	 * @param string       $expectOut Expected console output.
75
	 * @param array        $expectConfig Expected configuration data.
76
	 */
77
	public function testLoad( $composer, $expectOut, $expectConfig ) {
78
		$expectConfig['base'] = getcwd();
79
80
		$this->resetConfigCache();
81
		putenv( false === $composer ? 'COMPOSER' : "COMPOSER=$composer" );
82
		$out = new BufferedOutput();
83
		Config::setOutput( $out );
84
		$w = TestingAccessWrapper::newFromClass( Config::class );
85
		$w->load();
86
		$this->assertSame( $expectOut, $out->fetch() );
87
		$this->assertEquals( $expectConfig, $w->config );
88
89
		// Second load call should do nothing.
90
		putenv( 'COMPOSER=./doesnotexist.json' );
91
		$w->load();
92
		$this->assertSame( '', $out->fetch() );
93
		$this->assertEquals( $expectConfig, $w->config );
94
	}
95
96
	/**
97
	 * Data provider for testLoad.
98
	 */
99
	public function provideLoad() {
100
		$defaultConfig = TestingAccessWrapper::newFromClass( Config::class )->defaultConfig;
101
102
		return array(
103
			'default'                 => array(
104
				false,
105
				'',
106
				$defaultConfig,
107
			),
108
			'Alternate composer.json' => array(
109
				'no-types.json',
110
				'',
111
				array(
112
					'types'  => array(),
113
					'foobar' => 'baz',
114
				) + $defaultConfig,
115
			),
116
			'missing composer.json'   => array(
117
				'missing.json',
118
				"File missing.json (as specified by the COMPOSER environment variable) is not found.\n",
119
				$defaultConfig,
120
			),
121
			'broken composer.json'    => array(
122
				'bogus.json',
123
				"File bogus.json (as specified by the COMPOSER environment variable) could not be parsed.\n",
124
				$defaultConfig,
125
			),
126
		);
127
	}
128
129
	/**
130
	 * Test the base method.
131
	 */
132
	public function testBase() {
133
		$this->resetConfigCache();
134
		$out = new BufferedOutput();
135
		Config::setOutput( $out );
136
		$this->assertSame( getcwd(), Config::base() );
137
138
		$this->resetConfigCache();
139
		putenv( 'COMPOSER=' . __DIR__ . '/../../../composer.json' );
140
		Config::setOutput( $out );
141
		$this->assertSame( dirname( dirname( dirname( __DIR__ ) ) ), Config::base() );
142
	}
143
144
	/**
145
	 * Test the changesDir method.
146
	 */
147
	public function testChangesDir() {
148
		$this->resetConfigCache();
149
		$out = new BufferedOutput();
150
		Config::setOutput( $out );
151
		$this->assertSame( getcwd() . DIRECTORY_SEPARATOR . 'changelog', Config::changesDir() );
152
153
		$this->resetConfigCache();
154
		$this->writeComposerJson( array( 'changes-dir' => 'changes' ) );
155
		Config::setOutput( $out );
156
		$this->assertSame( getcwd() . DIRECTORY_SEPARATOR . 'changes', Config::changesDir() );
157
158
		$this->resetConfigCache();
159
		$this->writeComposerJson( array( 'changes-dir' => '/tmp/changes' ) );
160
		Config::setOutput( $out );
161
		$this->assertSame( '/tmp/changes', Config::changesDir() );
162
163
		$this->resetConfigCache();
164
		$this->writeComposerJson( array( 'changes-dir' => 'c:\\changes' ) );
165
		Config::setOutput( $out );
166
		$this->assertSame( 'c:\\changes', Config::changesDir() );
167
	}
168
169
	/**
170
	 * Test the types method.
171
	 */
172
	public function testTypes() {
173
		$this->resetConfigCache();
174
		$out = new BufferedOutput();
175
		Config::setOutput( $out );
176
		$w = TestingAccessWrapper::newFromClass( Config::class );
177
178
		$this->assertSame( $w->defaultConfig['types'], Config::types() );
179
180
		$w->config = array(
181
			'types' => array(
182
				'FOO' => 'Stuff',
183
				'bAr' => 'More stuff',
184
			),
185
		);
186
187
		// No change because of caching.
188
		$this->assertSame( $w->defaultConfig['types'], Config::types() );
189
190
		// Clear cache, now it changes.
191
		$w->cache = array();
192
		$this->assertSame(
193
			array(
194
				'foo' => 'Stuff',
195
				'bar' => 'More stuff',
196
			),
197
			Config::types()
198
		);
199
	}
200
201
}
202