forked from typesense/typesense-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationTest.php
More file actions
195 lines (156 loc) · 6.1 KB
/
ConfigurationTest.php
File metadata and controls
195 lines (156 loc) · 6.1 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace Feature;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Typesense\Exceptions\ConfigError;
use Typesense\Lib\Configuration;
class ConfigurationTest extends TestCase
{
private array $baseConfig;
protected function setUp(): void
{
$this->baseConfig = [
'api_key' => 'test_api_key',
'nodes' => [
[
'host' => 'localhost',
'port' => '8108',
'protocol' => 'http',
],
],
];
}
public function testConfigurationWithDefaultLogger(): void
{
$config = new Configuration($this->baseConfig);
$logger = $config->getLogger();
$this->assertInstanceOf(LoggerInterface::class, $logger);
$this->assertInstanceOf(Logger::class, $logger);
}
public function testConfigurationWithCustomLogger(): void
{
// Create a custom logger
$customLogger = new Logger('custom-test-logger');
$customLogger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
// Add custom logger to config
$configWithCustomLogger = array_merge($this->baseConfig, [
'logger' => $customLogger,
]);
$config = new Configuration($configWithCustomLogger);
$logger = $config->getLogger();
// Assert that the logger is the same instance we passed
$this->assertInstanceOf(LoggerInterface::class, $logger);
$this->assertSame($customLogger, $logger);
$this->assertEquals('custom-test-logger', $logger->getName());
}
public function testConfigurationWithCustomLogLevel(): void
{
// Add custom log level to config
$configWithLogLevel = array_merge($this->baseConfig, [
'log_level' => Logger::DEBUG,
]);
$config = new Configuration($configWithLogLevel);
$logger = $config->getLogger();
$this->assertInstanceOf(LoggerInterface::class, $logger);
$this->assertInstanceOf(Logger::class, $logger);
}
public function testConfigurationWithCustomLoggerOverridesLogLevel(): void
{
// Create a custom logger
$customLogger = new Logger('custom-logger-with-level');
$customLogger->pushHandler(new StreamHandler('php://stdout', Logger::ERROR));
// Add both custom logger and log level to config (logger should win)
$configWithBoth = array_merge($this->baseConfig, [
'logger' => $customLogger,
'log_level' => Logger::DEBUG, // This should be ignored
]);
$config = new Configuration($configWithBoth);
$logger = $config->getLogger();
// Assert that the custom logger is used, not a new one with log_level
$this->assertSame($customLogger, $logger);
$this->assertEquals('custom-logger-with-level', $logger->getName());
}
public function testConfigurationWithMockLogger(): void
{
// Create a mock logger for testing
$mockLogger = $this->createMock(LoggerInterface::class);
// Add mock logger to config
$configWithMockLogger = array_merge($this->baseConfig, [
'logger' => $mockLogger,
]);
$config = new Configuration($configWithMockLogger);
$logger = $config->getLogger();
// Assert that the logger is the same instance we passed
$this->assertInstanceOf(LoggerInterface::class, $logger);
$this->assertSame($mockLogger, $logger);
}
public function testConfigurationWithInvalidLoggerIgnored(): void
{
// Try to pass a non-logger object (should fall back to default)
$configWithInvalidLogger = array_merge($this->baseConfig, [
'logger' => 'not-a-logger-instance',
]);
$config = new Configuration($configWithInvalidLogger);
$logger = $config->getLogger();
// Should fall back to default logger
$this->assertInstanceOf(LoggerInterface::class, $logger);
$this->assertInstanceOf(Logger::class, $logger);
$this->assertEquals('typesense', $logger->getName());
}
public function testConfigurationThrowsErrorWhenNodesAreMissing(): void
{
$this->expectException(ConfigError::class);
$this->expectExceptionMessage('`nodes` is not defined.');
new Configuration([
'api_key' => 'test_api_key',
]);
}
public function testConfigurationThrowsErrorWhenApiKeyIsMissing(): void
{
$this->expectException(ConfigError::class);
$this->expectExceptionMessage('`api_key` is not defined.');
new Configuration([
'nodes' => [
[
'host' => 'localhost',
'port' => '8108',
'protocol' => 'http',
],
],
]);
}
public function testConfigurationWithAllOptions(): void
{
$customLogger = new Logger('full-config-logger');
$customLogger->pushHandler(new StreamHandler('php://stdout', Logger::INFO));
$fullConfig = [
'api_key' => 'test_api_key',
'nodes' => [
[
'host' => 'localhost',
'port' => '8108',
'protocol' => 'http',
'path' => '/api',
],
],
'nearest_node' => [
'host' => 'nearest.example.com',
'port' => '443',
'protocol' => 'https',
],
'logger' => $customLogger,
'num_retries' => 5,
'retry_interval_seconds' => 2.0,
'healthcheck_interval_seconds' => 30,
'randomize_nodes' => false,
];
$config = new Configuration($fullConfig);
$this->assertSame($customLogger, $config->getLogger());
$this->assertEquals(5, $config->getNumRetries());
$this->assertEquals(2.0, $config->getRetryIntervalSeconds());
$this->assertEquals(30, $config->getHealthCheckIntervalSeconds());
$this->assertNotNull($config->getNearestNode());
}
}