|
17 | 17 | use Symfony\Component\DependencyInjection\Exception\LogicException;
|
18 | 18 | use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
|
19 | 19 | use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
|
| 20 | +use Symfony\Component\RateLimiter\CompoundRateLimiterFactory; |
| 21 | +use Symfony\Component\RateLimiter\RateLimiterFactoryInterface; |
20 | 22 | use Symfony\Component\Workflow\Exception\InvalidDefinitionException;
|
21 | 23 |
|
22 | 24 | class PhpFrameworkExtensionTest extends FrameworkExtensionTestCase
|
@@ -290,4 +292,77 @@ public function testRateLimiterIsTagged()
|
290 | 292 | $this->assertSame('first', $container->getDefinition('limiter.first')->getTag('rate_limiter')[0]['name']);
|
291 | 293 | $this->assertSame('second', $container->getDefinition('limiter.second')->getTag('rate_limiter')[0]['name']);
|
292 | 294 | }
|
| 295 | + |
| 296 | + public function testRateLimiterCompoundPolicy() |
| 297 | + { |
| 298 | + if (!class_exists(CompoundRateLimiterFactory::class)) { |
| 299 | + $this->markTestSkipped('CompoundRateLimiterFactory is not available.'); |
| 300 | + } |
| 301 | + |
| 302 | + $container = $this->createContainerFromClosure(function (ContainerBuilder $container) { |
| 303 | + $container->loadFromExtension('framework', [ |
| 304 | + 'annotations' => false, |
| 305 | + 'http_method_override' => false, |
| 306 | + 'handle_all_throwables' => true, |
| 307 | + 'php_errors' => ['log' => true], |
| 308 | + 'lock' => true, |
| 309 | + 'rate_limiter' => [ |
| 310 | + 'first' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour'], |
| 311 | + 'second' => ['policy' => 'sliding_window', 'limit' => 10, 'interval' => '1 hour'], |
| 312 | + 'compound' => ['policy' => 'compound', 'limiters' => ['first', 'second']], |
| 313 | + ], |
| 314 | + ]); |
| 315 | + }); |
| 316 | + |
| 317 | + $definition = $container->getDefinition('limiter.compound'); |
| 318 | + $this->assertSame(CompoundRateLimiterFactory::class, $definition->getClass()); |
| 319 | + $this->assertEquals( |
| 320 | + [ |
| 321 | + 'limiter.first', |
| 322 | + 'limiter.second', |
| 323 | + ], |
| 324 | + $definition->getArgument(0)->getValues() |
| 325 | + ); |
| 326 | + $this->assertSame('limiter.compound', (string) $container->getAlias(RateLimiterFactoryInterface::class.' $compoundLimiter')); |
| 327 | + } |
| 328 | + |
| 329 | + public function testRateLimiterCompoundPolicyNoLimiters() |
| 330 | + { |
| 331 | + if (!class_exists(CompoundRateLimiterFactory::class)) { |
| 332 | + $this->markTestSkipped('CompoundRateLimiterFactory is not available.'); |
| 333 | + } |
| 334 | + |
| 335 | + $this->expectException(\LogicException::class); |
| 336 | + $this->createContainerFromClosure(function ($container) { |
| 337 | + $container->loadFromExtension('framework', [ |
| 338 | + 'annotations' => false, |
| 339 | + 'http_method_override' => false, |
| 340 | + 'handle_all_throwables' => true, |
| 341 | + 'php_errors' => ['log' => true], |
| 342 | + 'rate_limiter' => [ |
| 343 | + 'compound' => ['policy' => 'compound'], |
| 344 | + ], |
| 345 | + ]); |
| 346 | + }); |
| 347 | + } |
| 348 | + |
| 349 | + public function testRateLimiterCompoundPolicyInvalidLimiters() |
| 350 | + { |
| 351 | + if (!class_exists(CompoundRateLimiterFactory::class)) { |
| 352 | + $this->markTestSkipped('CompoundRateLimiterFactory is not available.'); |
| 353 | + } |
| 354 | + |
| 355 | + $this->expectException(\LogicException::class); |
| 356 | + $this->createContainerFromClosure(function ($container) { |
| 357 | + $container->loadFromExtension('framework', [ |
| 358 | + 'annotations' => false, |
| 359 | + 'http_method_override' => false, |
| 360 | + 'handle_all_throwables' => true, |
| 361 | + 'php_errors' => ['log' => true], |
| 362 | + 'rate_limiter' => [ |
| 363 | + 'compound' => ['policy' => 'compound', 'limiters' => ['invalid1', 'invalid2']], |
| 364 | + ], |
| 365 | + ]); |
| 366 | + }); |
| 367 | + } |
293 | 368 | }
|
0 commit comments