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
160 lines (130 loc) · 4.88 KB
/
ConfigurationTest.php
File metadata and controls
160 lines (130 loc) · 4.88 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
<?php
namespace Feature;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
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(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->assertSame($customLogger, $logger);
}
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(Logger::class, $logger);
}
public function testConfigurationWithCustomLoggerThrowsExceptionWhenLogLevelIsAlsoProvided(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Setting log_level is not allowed when a custom logger is provided.');
$customLogger = new Logger('custom-logger-with-level');
$customLogger->pushHandler(new StreamHandler('php://stdout', Logger::ERROR));
$configWithBoth = array_merge($this->baseConfig, [
'logger' => $customLogger,
'log_level' => Logger::DEBUG,
]);
new Configuration($configWithBoth);
}
public function testConfigurationWithInvalidLoggerThrowsException(): void
{
$this->expectException(ConfigError::class);
$this->expectExceptionMessage('Logger must implement Psr\Log\LoggerInterface');
// Try to pass a non-logger object (should throw exception)
$configWithInvalidLogger = array_merge($this->baseConfig, [
'logger' => 'not-a-logger-instance',
]);
new Configuration($configWithInvalidLogger);
}
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());
}
}