|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sineflow\ElasticsearchBundle\Tests\Functional\Manager; |
| 4 | + |
| 5 | +use Psr\Http\Client\ClientInterface; |
| 6 | +use Sineflow\ElasticsearchBundle\Manager\ConnectionManager; |
| 7 | +use Sineflow\ElasticsearchBundle\Tests\AbstractContainerAwareTestCase; |
| 8 | + |
| 9 | +class ConnectionManagerTest extends AbstractContainerAwareTestCase |
| 10 | +{ |
| 11 | + public function testConnectionManagerWithoutHttpClientService(): void |
| 12 | + { |
| 13 | + $connectionManager = $this->getContainer()->get('sfes.connection.default'); |
| 14 | + |
| 15 | + $this->assertInstanceOf(ConnectionManager::class, $connectionManager); |
| 16 | + |
| 17 | + // Use reflection to verify HTTP client is null (will use auto-discovery) |
| 18 | + $reflection = new \ReflectionClass($connectionManager); |
| 19 | + $httpClientProperty = $reflection->getProperty('httpClient'); |
| 20 | + $httpClientProperty->setAccessible(true); |
| 21 | + |
| 22 | + $this->assertNull($httpClientProperty->getValue($connectionManager), 'Default connection should not have a custom HTTP client injected'); |
| 23 | + } |
| 24 | + |
| 25 | + public function testConnectionManagerHasHttpClientOptions(): void |
| 26 | + { |
| 27 | + $connectionManager = $this->getContainer()->get('sfes.connection.default'); |
| 28 | + |
| 29 | + $this->assertInstanceOf(ConnectionManager::class, $connectionManager); |
| 30 | + |
| 31 | + // Use reflection to access connection settings |
| 32 | + $reflection = new \ReflectionClass($connectionManager); |
| 33 | + $settingsProperty = $reflection->getProperty('connectionSettings'); |
| 34 | + $settingsProperty->setAccessible(true); |
| 35 | + |
| 36 | + $settings = $settingsProperty->getValue($connectionManager); |
| 37 | + |
| 38 | + $this->assertIsArray($settings); |
| 39 | + $this->assertArrayHasKey('http_client_options', $settings, 'Connection settings should contain http_client_options key'); |
| 40 | + $this->assertIsArray($settings['http_client_options'], 'http_client_options should be an array'); |
| 41 | + } |
| 42 | + |
| 43 | + public function testConnectionManagerWithCustomHttpClientService(): void |
| 44 | + { |
| 45 | + // Create a mock HTTP client |
| 46 | + $mockHttpClient = $this->createMock(ClientInterface::class); |
| 47 | + |
| 48 | + // Register it as a service |
| 49 | + $container = $this->getContainer(); |
| 50 | + $container->set('app.test_http_client', $mockHttpClient); |
| 51 | + |
| 52 | + // Create a ConnectionManager with the custom HTTP client |
| 53 | + $connectionManager = new ConnectionManager( |
| 54 | + 'test_custom_client', |
| 55 | + [ |
| 56 | + 'hosts' => ['localhost:9200'], |
| 57 | + 'profiling' => false, |
| 58 | + 'logging' => false, |
| 59 | + 'bulk_batch_size' => 100, |
| 60 | + 'http_client_options' => [ |
| 61 | + 'timeout' => 45, |
| 62 | + 'max_duration' => 600, |
| 63 | + ], |
| 64 | + 'http_client_service' => 'app.test_http_client', |
| 65 | + ], |
| 66 | + true, // debug mode |
| 67 | + $mockHttpClient |
| 68 | + ); |
| 69 | + |
| 70 | + // Use reflection to verify the HTTP client is set |
| 71 | + $reflection = new \ReflectionClass($connectionManager); |
| 72 | + $httpClientProperty = $reflection->getProperty('httpClient'); |
| 73 | + $httpClientProperty->setAccessible(true); |
| 74 | + |
| 75 | + $injectedClient = $httpClientProperty->getValue($connectionManager); |
| 76 | + $this->assertSame($mockHttpClient, $injectedClient, 'Custom HTTP client should be injected into ConnectionManager'); |
| 77 | + } |
| 78 | + |
| 79 | + public function testConnectionManagerPreservesHttpClientOptions(): void |
| 80 | + { |
| 81 | + $customOptions = [ |
| 82 | + 'timeout' => 30, |
| 83 | + 'connect_timeout' => 10, |
| 84 | + 'max_duration' => 3600, |
| 85 | + 'extra' => [ |
| 86 | + 'curl' => [ |
| 87 | + CURLOPT_SSL_VERIFYPEER => false, |
| 88 | + ], |
| 89 | + ], |
| 90 | + ]; |
| 91 | + |
| 92 | + $connectionManager = new ConnectionManager( |
| 93 | + 'test_options', |
| 94 | + [ |
| 95 | + 'hosts' => ['localhost:9200'], |
| 96 | + 'profiling' => false, |
| 97 | + 'logging' => false, |
| 98 | + 'bulk_batch_size' => 100, |
| 99 | + 'http_client_options' => $customOptions, |
| 100 | + 'http_client_service' => null, |
| 101 | + ], |
| 102 | + true, |
| 103 | + null |
| 104 | + ); |
| 105 | + |
| 106 | + // Use reflection to access connection settings |
| 107 | + $reflection = new \ReflectionClass($connectionManager); |
| 108 | + $settingsProperty = $reflection->getProperty('connectionSettings'); |
| 109 | + $settingsProperty->setAccessible(true); |
| 110 | + |
| 111 | + $settings = $settingsProperty->getValue($connectionManager); |
| 112 | + |
| 113 | + $this->assertArrayHasKey('http_client_options', $settings); |
| 114 | + $this->assertSame($customOptions, $settings['http_client_options'], 'HTTP client options should be preserved exactly as configured'); |
| 115 | + |
| 116 | + // Verify nested options are preserved |
| 117 | + $this->assertIsArray($settings['http_client_options']['extra']); |
| 118 | + $this->assertIsArray($settings['http_client_options']['extra']['curl']); |
| 119 | + $this->assertFalse($settings['http_client_options']['extra']['curl'][CURLOPT_SSL_VERIFYPEER]); |
| 120 | + } |
| 121 | + |
| 122 | + public function testConnectionManagerWithBothHttpClientServiceAndOptions(): void |
| 123 | + { |
| 124 | + $mockHttpClient = $this->createMock(ClientInterface::class); |
| 125 | + |
| 126 | + $customOptions = [ |
| 127 | + 'timeout' => 60, |
| 128 | + 'max_duration' => 120, |
| 129 | + ]; |
| 130 | + |
| 131 | + $connectionManager = new ConnectionManager( |
| 132 | + 'test_both', |
| 133 | + [ |
| 134 | + 'hosts' => ['localhost:9200'], |
| 135 | + 'profiling' => false, |
| 136 | + 'logging' => false, |
| 137 | + 'bulk_batch_size' => 100, |
| 138 | + 'http_client_options' => $customOptions, |
| 139 | + 'http_client_service' => 'app.test_http_client', |
| 140 | + ], |
| 141 | + true, |
| 142 | + $mockHttpClient |
| 143 | + ); |
| 144 | + |
| 145 | + // Verify HTTP client is injected |
| 146 | + $reflection = new \ReflectionClass($connectionManager); |
| 147 | + $httpClientProperty = $reflection->getProperty('httpClient'); |
| 148 | + $httpClientProperty->setAccessible(true); |
| 149 | + |
| 150 | + $injectedClient = $httpClientProperty->getValue($connectionManager); |
| 151 | + $this->assertSame($mockHttpClient, $injectedClient); |
| 152 | + |
| 153 | + // Verify HTTP client options are preserved |
| 154 | + $settingsProperty = $reflection->getProperty('connectionSettings'); |
| 155 | + $settingsProperty->setAccessible(true); |
| 156 | + |
| 157 | + $settings = $settingsProperty->getValue($connectionManager); |
| 158 | + $this->assertSame($customOptions, $settings['http_client_options']); |
| 159 | + } |
| 160 | +} |
0 commit comments