|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\AmqpLib\Tests; |
| 4 | + |
| 5 | +use Enqueue\AmqpLib\AmqpConnectionFactory; |
| 6 | +use Enqueue\Test\ClassExtensionTrait; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +/** |
| 10 | + * The class contains the factory tests dedicated to configuration. |
| 11 | + */ |
| 12 | +class AmqpConnectionFactoryConfigTest extends TestCase |
| 13 | +{ |
| 14 | + use ClassExtensionTrait; |
| 15 | + |
| 16 | + public function testThrowNeitherArrayStringNorNullGivenAsConfig() |
| 17 | + { |
| 18 | + $this->expectException(\LogicException::class); |
| 19 | + $this->expectExceptionMessage('The config must be either an array of options, a DSN string or null'); |
| 20 | + |
| 21 | + new AmqpConnectionFactory(new \stdClass()); |
| 22 | + } |
| 23 | + |
| 24 | + public function testThrowIfSchemeIsNotAmqp() |
| 25 | + { |
| 26 | + $this->expectException(\LogicException::class); |
| 27 | + $this->expectExceptionMessage('The given DSN scheme "http" is not supported. Could be "amqp" only.'); |
| 28 | + |
| 29 | + new AmqpConnectionFactory('http://example.com'); |
| 30 | + } |
| 31 | + |
| 32 | + public function testThrowIfDsnCouldNotBeParsed() |
| 33 | + { |
| 34 | + $this->expectException(\LogicException::class); |
| 35 | + $this->expectExceptionMessage('Failed to parse DSN "amqp://:@/"'); |
| 36 | + |
| 37 | + new AmqpConnectionFactory('amqp://:@/'); |
| 38 | + } |
| 39 | + |
| 40 | + public function testThrowIfReceiveMenthodIsInvalid() |
| 41 | + { |
| 42 | + $this->expectException(\LogicException::class); |
| 43 | + $this->expectExceptionMessage('Invalid "receive_method" option value "invalidMethod". It could be only "basic_get", "basic_consume"'); |
| 44 | + |
| 45 | + new AmqpConnectionFactory(['receive_method' => 'invalidMethod']); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @dataProvider provideConfigs |
| 50 | + * |
| 51 | + * @param mixed $config |
| 52 | + * @param mixed $expectedConfig |
| 53 | + */ |
| 54 | + public function testShouldParseConfigurationAsExpected($config, $expectedConfig) |
| 55 | + { |
| 56 | + $factory = new AmqpConnectionFactory($config); |
| 57 | + |
| 58 | + $this->assertAttributeEquals($expectedConfig, 'config', $factory); |
| 59 | + } |
| 60 | + |
| 61 | + public static function provideConfigs() |
| 62 | + { |
| 63 | + yield [ |
| 64 | + null, |
| 65 | + [ |
| 66 | + 'host' => 'localhost', |
| 67 | + 'port' => 5672, |
| 68 | + 'vhost' => '/', |
| 69 | + 'user' => 'guest', |
| 70 | + 'pass' => 'guest', |
| 71 | + 'read_timeout' => 3, |
| 72 | + 'write_timeout' => 3, |
| 73 | + 'lazy' => true, |
| 74 | + 'receive_method' => 'basic_get', |
| 75 | + 'stream' => true, |
| 76 | + 'insist' => false, |
| 77 | + 'login_method' => 'AMQPLAIN', |
| 78 | + 'login_response' => null, |
| 79 | + 'locale' => 'en_US', |
| 80 | + 'keepalive' => false, |
| 81 | + 'heartbeat' => 0, |
| 82 | + 'connection_timeout' => 3.0, |
| 83 | + 'read_write_timeout' => 3.0, |
| 84 | + ], |
| 85 | + ]; |
| 86 | + |
| 87 | + // some examples from Appendix A: Examples (https://www.rabbitmq.com/uri-spec.html) |
| 88 | + |
| 89 | + yield [ |
| 90 | + 'amqp://user:pass@host:10000/vhost', |
| 91 | + [ |
| 92 | + 'host' => 'host', |
| 93 | + 'port' => 10000, |
| 94 | + 'vhost' => 'vhost', |
| 95 | + 'user' => 'user', |
| 96 | + 'pass' => 'pass', |
| 97 | + 'read_timeout' => 3, |
| 98 | + 'write_timeout' => 3, |
| 99 | + 'lazy' => true, |
| 100 | + 'receive_method' => 'basic_get', |
| 101 | + 'stream' => true, |
| 102 | + 'insist' => false, |
| 103 | + 'login_method' => 'AMQPLAIN', |
| 104 | + 'login_response' => null, |
| 105 | + 'locale' => 'en_US', |
| 106 | + 'keepalive' => false, |
| 107 | + 'heartbeat' => 0, |
| 108 | + 'connection_timeout' => 3.0, |
| 109 | + 'read_write_timeout' => 3.0, |
| 110 | + ], |
| 111 | + ]; |
| 112 | + |
| 113 | + yield [ |
| 114 | + 'amqp://user%61:%61pass@ho%61st:10000/v%2fhost', |
| 115 | + [ |
| 116 | + 'host' => 'hoast', |
| 117 | + 'port' => 10000, |
| 118 | + 'vhost' => 'v/host', |
| 119 | + 'user' => 'usera', |
| 120 | + 'pass' => 'apass', |
| 121 | + 'read_timeout' => 3, |
| 122 | + 'write_timeout' => 3, |
| 123 | + 'lazy' => true, |
| 124 | + 'receive_method' => 'basic_get', |
| 125 | + 'stream' => true, |
| 126 | + 'insist' => false, |
| 127 | + 'login_method' => 'AMQPLAIN', |
| 128 | + 'login_response' => null, |
| 129 | + 'locale' => 'en_US', |
| 130 | + 'keepalive' => false, |
| 131 | + 'heartbeat' => 0, |
| 132 | + 'connection_timeout' => 3.0, |
| 133 | + 'read_write_timeout' => 3.0, |
| 134 | + ], |
| 135 | + ]; |
| 136 | + |
| 137 | + yield [ |
| 138 | + 'amqp://', |
| 139 | + [ |
| 140 | + 'host' => 'localhost', |
| 141 | + 'port' => 5672, |
| 142 | + 'vhost' => '/', |
| 143 | + 'user' => 'guest', |
| 144 | + 'pass' => 'guest', |
| 145 | + 'read_timeout' => 3, |
| 146 | + 'write_timeout' => 3, |
| 147 | + 'lazy' => true, |
| 148 | + 'receive_method' => 'basic_get', |
| 149 | + 'stream' => true, |
| 150 | + 'insist' => false, |
| 151 | + 'login_method' => 'AMQPLAIN', |
| 152 | + 'login_response' => null, |
| 153 | + 'locale' => 'en_US', |
| 154 | + 'keepalive' => false, |
| 155 | + 'heartbeat' => 0, |
| 156 | + 'connection_timeout' => 3.0, |
| 157 | + 'read_write_timeout' => 3.0, |
| 158 | + ], |
| 159 | + ]; |
| 160 | + |
| 161 | + yield [ |
| 162 | + 'amqp://user:pass@host:10000/vhost?connection_timeout=2&lazy=', |
| 163 | + [ |
| 164 | + 'host' => 'host', |
| 165 | + 'port' => 10000, |
| 166 | + 'vhost' => 'vhost', |
| 167 | + 'user' => 'user', |
| 168 | + 'pass' => 'pass', |
| 169 | + 'read_timeout' => 3, |
| 170 | + 'write_timeout' => 3, |
| 171 | + 'lazy' => '', |
| 172 | + 'receive_method' => 'basic_get', |
| 173 | + 'stream' => true, |
| 174 | + 'insist' => false, |
| 175 | + 'login_method' => 'AMQPLAIN', |
| 176 | + 'login_response' => null, |
| 177 | + 'locale' => 'en_US', |
| 178 | + 'keepalive' => false, |
| 179 | + 'heartbeat' => 0, |
| 180 | + 'connection_timeout' => '2', |
| 181 | + 'read_write_timeout' => 3.0, |
| 182 | + ], |
| 183 | + ]; |
| 184 | + |
| 185 | + yield [ |
| 186 | + [], |
| 187 | + [ |
| 188 | + 'host' => 'localhost', |
| 189 | + 'port' => 5672, |
| 190 | + 'vhost' => '/', |
| 191 | + 'user' => 'guest', |
| 192 | + 'pass' => 'guest', |
| 193 | + 'read_timeout' => 3, |
| 194 | + 'write_timeout' => 3, |
| 195 | + 'lazy' => true, |
| 196 | + 'receive_method' => 'basic_get', |
| 197 | + 'stream' => true, |
| 198 | + 'insist' => false, |
| 199 | + 'login_method' => 'AMQPLAIN', |
| 200 | + 'login_response' => null, |
| 201 | + 'locale' => 'en_US', |
| 202 | + 'keepalive' => false, |
| 203 | + 'heartbeat' => 0, |
| 204 | + 'connection_timeout' => 3.0, |
| 205 | + 'read_write_timeout' => 3.0, |
| 206 | + ], |
| 207 | + ]; |
| 208 | + |
| 209 | + yield [ |
| 210 | + ['lazy' => false, 'host' => 'host'], |
| 211 | + [ |
| 212 | + 'host' => 'host', |
| 213 | + 'port' => 5672, |
| 214 | + 'vhost' => '/', |
| 215 | + 'user' => 'guest', |
| 216 | + 'pass' => 'guest', |
| 217 | + 'read_timeout' => 3, |
| 218 | + 'write_timeout' => 3, |
| 219 | + 'lazy' => false, |
| 220 | + 'receive_method' => 'basic_get', |
| 221 | + 'stream' => true, |
| 222 | + 'insist' => false, |
| 223 | + 'login_method' => 'AMQPLAIN', |
| 224 | + 'login_response' => null, |
| 225 | + 'locale' => 'en_US', |
| 226 | + 'keepalive' => false, |
| 227 | + 'heartbeat' => 0, |
| 228 | + 'connection_timeout' => 3.0, |
| 229 | + 'read_write_timeout' => 3.0, |
| 230 | + ], |
| 231 | + ]; |
| 232 | + |
| 233 | + yield [ |
| 234 | + ['connection_timeout' => 123, 'read_write_timeout' => 321], |
| 235 | + [ |
| 236 | + 'host' => 'localhost', |
| 237 | + 'port' => 5672, |
| 238 | + 'vhost' => '/', |
| 239 | + 'user' => 'guest', |
| 240 | + 'pass' => 'guest', |
| 241 | + 'read_timeout' => 3, |
| 242 | + 'write_timeout' => 3, |
| 243 | + 'lazy' => true, |
| 244 | + 'receive_method' => 'basic_get', |
| 245 | + 'stream' => true, |
| 246 | + 'insist' => false, |
| 247 | + 'login_method' => 'AMQPLAIN', |
| 248 | + 'login_response' => null, |
| 249 | + 'locale' => 'en_US', |
| 250 | + 'keepalive' => false, |
| 251 | + 'heartbeat' => 0, |
| 252 | + 'connection_timeout' => 123, |
| 253 | + 'read_write_timeout' => 321, |
| 254 | + ], |
| 255 | + ]; |
| 256 | + |
| 257 | + yield [ |
| 258 | + 'amqp://user:pass@host:10000/vhost?connection_timeout=123&read_write_timeout=321', |
| 259 | + [ |
| 260 | + 'host' => 'host', |
| 261 | + 'port' => 10000, |
| 262 | + 'vhost' => 'vhost', |
| 263 | + 'user' => 'user', |
| 264 | + 'pass' => 'pass', |
| 265 | + 'read_timeout' => 3, |
| 266 | + 'write_timeout' => 3, |
| 267 | + 'lazy' => true, |
| 268 | + 'receive_method' => 'basic_get', |
| 269 | + 'stream' => true, |
| 270 | + 'insist' => false, |
| 271 | + 'login_method' => 'AMQPLAIN', |
| 272 | + 'login_response' => null, |
| 273 | + 'locale' => 'en_US', |
| 274 | + 'keepalive' => false, |
| 275 | + 'heartbeat' => 0, |
| 276 | + 'connection_timeout' => '123', |
| 277 | + 'read_write_timeout' => '321', |
| 278 | + ], |
| 279 | + ]; |
| 280 | + } |
| 281 | +} |
0 commit comments