Completed
Push — add/changelog-tooling ( 257a85 )
by
unknown
149:15 queued 138:48
created

ConfigTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testLoadBeforeSetOutput() 0 7 1
A testLoad() 0 16 2
A provideLoad() 0 37 1
A testBase() 0 12 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.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
	 * Test that calling load() before setOutput() throws.
26
	 */
27
	public function testLoadBeforeSetOutput() {
28
		$this->resetConfigCache();
29
30
		$this->expectException( \LogicException::class );
31
		$this->expectExceptionMessage( 'Must call Config::setOutput() before Config::load()' );
32
		TestingAccessWrapper::newFromClass( Config::class )->load();
33
	}
34
35
	/**
36
	 * Test parsing composer.json.
37
	 *
38
	 * @dataProvider provideLoad
39
	 * @param string|false $composer Value for COMPOSER environment variable.
40
	 * @param string       $expectOut Expected console output.
41
	 * @param array        $expectConfig Expected configuration data.
42
	 */
43
	public function testLoad( $composer, $expectOut, $expectConfig ) {
44
		$this->resetConfigCache();
45
		putenv( false === $composer ? 'COMPOSER' : "COMPOSER=$composer" );
46
		$out = new BufferedOutput();
47
		Config::setOutput( $out );
48
		$w = TestingAccessWrapper::newFromClass( Config::class );
49
		$w->load();
50
		$this->assertSame( $expectOut, $out->fetch() );
51
		$this->assertEquals( $expectConfig, $w->config );
52
53
		// Second load call should do nothing.
54
		putenv( 'COMPOSER=' . __DIR__ . '../fixtures/doesnotexist.json' );
55
		$w->load();
56
		$this->assertSame( '', $out->fetch() );
57
		$this->assertEquals( $expectConfig, $w->config );
58
	}
59
60
	/**
61
	 * Data provider for testLoad.
62
	 */
63
	public function provideLoad() {
64
		$defaultConfig = TestingAccessWrapper::newFromClass( Config::class )->defaultConfig;
65
		$fixtures      = dirname( __DIR__ ) . '/fixtures';
66
67
		return array(
68
			'default'                 => array(
69
				false,
70
				'',
71
				array(
72
					'base' => getcwd(),
73
				) + $defaultConfig,
74
			),
75
			'Alternate composer.json' => array(
76
				"$fixtures/no-types.json",
77
				'',
78
				array(
79
					'types'  => array(),
80
					'foobar' => 'baz',
81
					'base'   => $fixtures,
82
				) + $defaultConfig,
83
			),
84
			'missing composer.json'   => array(
85
				"$fixtures/missing.json",
86
				"File $fixtures/missing.json (as specified by the COMPOSER environment variable) is not found.\n",
87
				array(
88
					'base' => getcwd(),
89
				) + $defaultConfig,
90
			),
91
			'broken composer.json'    => array(
92
				"$fixtures/bogus.json",
93
				"File $fixtures/bogus.json (as specified by the COMPOSER environment variable) could not be parsed.\n",
94
				array(
95
					'base' => getcwd(),
96
				) + $defaultConfig,
97
			),
98
		);
99
	}
100
101
	/**
102
	 * Test the base method.
103
	 */
104
	public function testBase() {
105
		$this->resetConfigCache();
106
		$out = new BufferedOutput();
107
		Config::setOutput( $out );
108
		$this->assertSame( getcwd(), Config::base() );
109
110
		$this->resetConfigCache();
111
		putenv( 'COMPOSER=' . dirname( __DIR__ ) . '/fixtures/no-types.json' );
112
		Config::setOutput( $out );
113
		$this->assertSame( dirname( __DIR__ ) . '/fixtures', Config::base() );
114
115
	}
116
117
	/**
118
	 * Test the types method.
119
	 */
120
	public function testTypes() {
121
		$this->resetConfigCache();
122
		$out = new BufferedOutput();
123
		Config::setOutput( $out );
124
		$w = TestingAccessWrapper::newFromClass( Config::class );
125
126
		$this->assertSame( $w->defaultConfig['types'], Config::types() );
127
128
		$w->config = array(
129
			'types' => array(
130
				'FOO' => 'Stuff',
131
				'bAr' => 'More stuff',
132
			),
133
		);
134
135
		// No change because of caching.
136
		$this->assertSame( $w->defaultConfig['types'], Config::types() );
137
138
		// Clear cache, now it changes.
139
		$w->cache = array();
140
		$this->assertSame(
141
			array(
142
				'foo' => 'Stuff',
143
				'bar' => 'More stuff',
144
			),
145
			Config::types()
146
		);
147
	}
148
149
}
150