-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathConfigTest.php
126 lines (112 loc) · 5.23 KB
/
ConfigTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Platformsh\Cli\Tests;
use Platformsh\Cli\Service\Config;
class ConfigTest extends \PHPUnit_Framework_TestCase
{
private $configFile;
public function __construct($name = null, array $data = array(), $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->configFile = __DIR__ . '/data/mock-cli-config.yaml';
}
/**
* Test loading config from file.
*/
public function testLoadMainConfig()
{
$config = new Config([], $this->configFile);
$this->assertTrue($config->has('application.name'));
$this->assertFalse($config->has('nonexistent'));
$this->assertEquals('Mock CLI', $config->get('application.name'));
$this->assertEquals(123, $config->getWithDefault('nonexistent', 123));
}
public function testGetHomeDirectory()
{
$homeDir = (new Config(['HOME' => '.'], $this->configFile))->getHomeDirectory();
$this->assertNotEmpty($homeDir, 'Home directory returned');
$this->assertNotEquals('.', $homeDir, 'Home directory not relative');
$homeDir = (new Config(['MOCK_CLI_HOME' => __DIR__ . '/data', 'HOME' => __DIR__], $this->configFile))->getHomeDirectory();
$this->assertEquals(__DIR__ . '/data', $homeDir, 'Home directory overridden');
$homeDir = (new Config(['MOCK_CLI_HOME' => '', 'HOME' => __DIR__], $this->configFile))->getHomeDirectory();
$this->assertEquals(__DIR__, $homeDir, 'Empty value treated as nonexistent');
}
/**
* Test that selected environment variables can override initial config.
*/
public function testEnvironmentOverrides()
{
$config = new Config([], $this->configFile);
putenv('MOCK_CLI_DISABLE_CACHE=0');
$config = new Config([
'MOCK_CLI_APPLICATION_NAME' => 'Overridden application name',
'MOCK_CLI_DEBUG' => 1,
], $this->configFile);
$this->assertFalse((bool) $config->get('api.disable_cache'));
$this->assertTrue((bool) $config->get('api.debug'));
$this->assertEquals('Overridden application name', $config->get('application.name'));
}
/**
* Test that selected user config can override initial config.
*/
public function testUserConfigOverrides()
{
$config = new Config([], $this->configFile);
$this->assertFalse($config->has('experimental.test'));
$home = getenv('HOME');
putenv('HOME=' . __DIR__ . '/data');
$config = new Config([], $this->configFile);
putenv('HOME=' . $home);
$this->assertTrue($config->has('experimental.test'));
$this->assertTrue($config->get('experimental.test'));
}
/**
* Test misc. dynamic defaults.
*/
public function testDynamicDefaults()
{
$config = new Config([], $this->configFile);
$this->assertEquals('mock-cli', $config->get('application.slug'));
$this->assertEquals('mock-cli-tmp', $config->get('application.tmp_sub_dir'));
$this->assertEquals('mock-cli', $config->get('api.oauth2_client_id'));
$this->assertEquals('console.example.com', $config->get('detection.console_domain'));
$this->assertEquals('.mock/applications.yaml', $config->get('service.applications_config_file'));
$this->assertEquals('X-Mock-Cluster', $config->get('detection.cluster_header'));
}
/**
* Test dynamic defaults for URLs.
*/
public function testDynamicUrlDefaults()
{
$config = new Config(['MOCK_CLI_AUTH_URL' => 'https://auth.example.com'], $this->configFile);
$this->assertEquals('https://auth.example.com/oauth2/token', $config->get('api.oauth2_token_url'));
$this->assertEquals('https://auth.example.com/oauth2/authorize', $config->get('api.oauth2_auth_url'));
$this->assertEquals('https://auth.example.com/oauth2/revoke', $config->get('api.oauth2_revoke_url'));
}
/**
* Test dynamic defaults for local paths.
*/
public function testLocalPathDefaults()
{
$config = new Config([], $this->configFile);
$this->assertEquals('.mock/local', $config->get('local.local_dir'));
$this->assertEquals('.mock/local/project.yaml', $config->get('local.project_config'));
$this->assertEquals('.mock/local/builds', $config->get('local.build_dir'));
$this->assertEquals('.mock/local/build-archives', $config->get('local.archive_dir'));
$this->assertEquals('.mock/local/deps', $config->get('local.dependencies_dir'));
$this->assertEquals('.mock/local/shared', $config->get('local.shared_dir'));
putenv('MOCK_CLI_LOCAL_SHARED_DIR=/tmp/shared');
$config = new Config([], $this->configFile);
$this->assertEquals('/tmp/shared', $config->get('local.shared_dir'));
}
/**
* Test the default for application.writable_user_dir
*/
public function testGetWritableUserDir()
{
$config = new Config([], $this->configFile);
$this->assertEquals('mock-cli-user-config', $config->get('application.user_config_dir'));
$this->assertEquals(null, $config->get('application.writable_user_dir'));
$home = $config->getHomeDirectory();
$this->assertEquals($home . DIRECTORY_SEPARATOR . 'mock-cli-user-config', $config->getWritableUserDir());
}
}