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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
<?php
use PHPUnit\Framework\TestCase;
final class ConfigTest extends TestCase
{
protected function setUp(): void
{
// Get a fresh instance and reload from env
Config::get_instance()->reload();
}
protected function tearDown(): void
{
// Clean up any test-set config values
Config::get_instance()->reload();
}
// ── Singleton behavior ───────────────────────────────────────────────────
public function test_singleton_returns_same_instance(): void
{
$config1 = Config::get_instance();
$config2 = Config::get_instance();
$this->assertSame($config1, $config2);
}
// ── cast_to() ────────────────────────────────────────────────────────────
public function test_cast_to_string(): void
{
$this->assertEquals('hello', Config::cast_to('hello', Config::T_STRING));
}
public function test_cast_to_int(): void
{
$this->assertEquals(42, Config::cast_to('42', Config::T_INT));
}
public function test_cast_to_int_from_float_string(): void
{
$this->assertEquals(3, Config::cast_to('3.14', Config::T_INT));
}
public function test_cast_to_bool_true(): void
{
$this->assertTrue(Config::cast_to('1', Config::T_BOOL));
$this->assertTrue(Config::cast_to('true', Config::T_BOOL));
}
public function test_cast_to_bool_false(): void
{
$this->assertFalse(Config::cast_to('0', Config::T_BOOL));
$this->assertFalse(Config::cast_to('', Config::T_BOOL));
$this->assertFalse(Config::cast_to('false', Config::T_BOOL));
}
// ── get() with defaults ──────────────────────────────────────────────────
public function test_get_db_type_default(): void
{
Config::set(Config::DB_TYPE, 'sqlite');
$this->assertEquals('sqlite', Config::get(Config::DB_TYPE));
}
public function test_get_db_host_default(): void
{
Config::set(Config::DB_HOST, 'db');
$this->assertEquals('db', Config::get(Config::DB_HOST));
}
public function test_get_db_port_default(): void
{
Config::set(Config::DB_PORT, '5432');
$this->assertEquals('5432', Config::get(Config::DB_PORT));
}
public function test_get_scratch_db_default(): void
{
Config::set(Config::SCRATCH_DB, 'db/scratch.db');
$this->assertEquals('db/scratch.db', Config::get(Config::SCRATCH_DB));
}
public function test_get_session_name_default(): void
{
Config::set(Config::SESSION_NAME, 'epube_sid');
$this->assertEquals('epube_sid', Config::get(Config::SESSION_NAME));
}
// ── set() overrides ──────────────────────────────────────────────────────
public function test_set_db_type(): void
{
Config::set(Config::DB_TYPE, 'pgsql');
$this->assertEquals('pgsql', Config::get(Config::DB_TYPE));
}
public function test_set_db_host(): void
{
Config::set(Config::DB_HOST, 'myhost');
$this->assertEquals('myhost', Config::get(Config::DB_HOST));
}
public function test_set_db_port(): void
{
Config::set(Config::DB_PORT, '55432');
$this->assertEquals('55432', Config::get(Config::DB_PORT));
}
public function test_set_session_name(): void
{
Config::set(Config::SESSION_NAME, 'custom_sid');
$this->assertEquals('custom_sid', Config::get(Config::SESSION_NAME));
}
public function test_set_session_lifetime(): void
{
Config::set(Config::SESSION_LIFETIME, 3600);
$this->assertEquals(3600, Config::get(Config::SESSION_LIFETIME));
}
// ── set() returns bool ───────────────────────────────────────────────────
public function test_set_returns_true_for_known_key(): void
{
$this->assertTrue(Config::set(Config::DB_TYPE, 'pgsql'));
}
public function test_set_returns_false_for_unknown_key(): void
{
$this->assertFalse(@Config::set('NONEXISTENT_KEY', 'value'));
}
// ── reload() resets to env ───────────────────────────────────────────────
public function test_reload_resets_to_env(): void
{
Config::set(Config::DB_TYPE, 'pgsql');
putenv('EPUBE_DB_TYPE=sqlite');
Config::get_instance()->reload();
$this->assertEquals('sqlite', Config::get(Config::DB_TYPE));
}
// ── get_self_url() ───────────────────────────────────────────────────────
public function test_get_self_url_http(): void
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/backend.php?op=list';
$url = Config::get_self_url();
$this->assertStringContainsString('http://', $url);
$this->assertEquals('http://example.com', $url);
}
public function test_get_self_url_https(): void
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/backend.php';
$_SERVER['HTTPS'] = 'on';
$url = Config::get_self_url();
$this->assertStringContainsString('https://', $url);
$this->assertEquals('https://example.com', $url);
}
public function test_get_self_url_with_x_forwarded_proto(): void
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/backend.php';
unset($_SERVER['HTTPS']);
$_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https';
$url = Config::get_self_url();
$this->assertStringContainsString('https://', $url);
}
public function test_get_self_url_trailing_slash_removed(): void
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/backend.php/';
$url = Config::get_self_url();
$this->assertStringNotContainsString('///', $url);
}
}
|