|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Bedrock\Bundle\RateLimitBundle\Tests\EventListener; |
| 6 | + |
| 7 | +use Bedrock\Bundle\RateLimitBundle\EventListener\ReadRateLimitConfigurationListener; |
| 8 | +use Bedrock\Bundle\RateLimitBundle\Model\RateLimit; |
| 9 | +use Bedrock\Bundle\RateLimitBundle\RateLimitModifier\RateLimitModifierInterface; |
| 10 | +use PHPUnit\Framework\MockObject\MockObject; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Symfony\Component\HttpFoundation\ParameterBag; |
| 13 | +use Symfony\Component\HttpFoundation\Request; |
| 14 | +use Symfony\Component\HttpKernel\Event\ControllerEvent; |
| 15 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 16 | + |
| 17 | +class ReadRateLimitConfigurationListenerTest extends TestCase |
| 18 | +{ |
| 19 | + private ReadRateLimitConfigurationListener $readRateLimitConfigurationListener; |
| 20 | + |
| 21 | + /** @var array<RateLimitModifierInterface|MockObject> */ |
| 22 | + private $rateLimitModifiers; |
| 23 | + |
| 24 | + private int $limitDefaultValue = 1000; |
| 25 | + |
| 26 | + private int $periodDefaultValue = 60; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param array<array<string, int>> $routes |
| 30 | + */ |
| 31 | + public function createReadRateLimitConfigurationListener(array $routes): void |
| 32 | + { |
| 33 | + $this->readRateLimitConfigurationListener = new ReadRateLimitConfigurationListener( |
| 34 | + $this->rateLimitModifiers = [ |
| 35 | + $this->createMock(RateLimitModifierInterface::class), |
| 36 | + $this->createMock(RateLimitModifierInterface::class), |
| 37 | + ], |
| 38 | + $this->limitDefaultValue, |
| 39 | + $this->periodDefaultValue, |
| 40 | + $routes |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + public function testItDoesNotSetRateLimitIfNoConfigurationProvided(): void |
| 45 | + { |
| 46 | + $this->createReadRateLimitConfigurationListener([]); |
| 47 | + $event = $this->createEvent(); |
| 48 | + |
| 49 | + $this->rateLimitModifiers[0]->expects($this->never())->method('support'); |
| 50 | + $this->rateLimitModifiers[1]->expects($this->never())->method('support'); |
| 51 | + |
| 52 | + $this->readRateLimitConfigurationListener->onKernelController($event); |
| 53 | + $this->assertFalse($event->getRequest()->attributes->has('_rate_limit')); |
| 54 | + } |
| 55 | + |
| 56 | + public function testItSetsRateLimitIfConfigurationProvidedWithDefaultValue(): void |
| 57 | + { |
| 58 | + $this->createReadRateLimitConfigurationListener(['some_route_name' => []]); |
| 59 | + $request = $this->createMock(Request::class); |
| 60 | + $request->attributes = new ParameterBag(); |
| 61 | + $event = $this->createEvent($request); |
| 62 | + |
| 63 | + $this->rateLimitModifiers[0]->expects($this->once())->method('support')->willReturn(true); |
| 64 | + $rateLimit = new RateLimit($this->limitDefaultValue, $this->periodDefaultValue); |
| 65 | + $rateLimit->varyHashOn('_route', 'some_route_name'); |
| 66 | + $this->rateLimitModifiers[0]->expects($this->once())->method('modifyRateLimit')->with($request, $rateLimit); |
| 67 | + |
| 68 | + $this->rateLimitModifiers[1]->expects($this->once())->method('support')->willReturn(false); |
| 69 | + $this->rateLimitModifiers[1]->expects($this->never())->method('modifyRateLimit'); |
| 70 | + |
| 71 | + $this->readRateLimitConfigurationListener->onKernelController($event); |
| 72 | + $this->assertTrue($event->getRequest()->attributes->has('_rate_limit')); |
| 73 | + |
| 74 | + $this->assertEquals( |
| 75 | + $rateLimit, |
| 76 | + $event->getRequest()->attributes->get('_rate_limit') |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + public function testItSetsRateLimitIfConfigurationProvidedWithCustomValue(): void |
| 81 | + { |
| 82 | + $this->createReadRateLimitConfigurationListener([ |
| 83 | + 'some_route_name' => [ |
| 84 | + 'limit' => 10, |
| 85 | + 'period' => 5, |
| 86 | + ], |
| 87 | + ]); |
| 88 | + $request = $this->createMock(Request::class); |
| 89 | + $request->attributes = new ParameterBag(); |
| 90 | + $event = $this->createEvent($request); |
| 91 | + |
| 92 | + $this->rateLimitModifiers[0]->expects($this->once())->method('support')->willReturn(true); |
| 93 | + $rateLimit = new RateLimit(10, 5); |
| 94 | + $rateLimit->varyHashOn('_route', 'some_route_name'); |
| 95 | + $this->rateLimitModifiers[0]->expects($this->once())->method('modifyRateLimit')->with($request, $rateLimit); |
| 96 | + |
| 97 | + $this->rateLimitModifiers[1]->expects($this->once())->method('support')->willReturn(false); |
| 98 | + $this->rateLimitModifiers[1]->expects($this->never())->method('modifyRateLimit'); |
| 99 | + |
| 100 | + $this->readRateLimitConfigurationListener->onKernelController($event); |
| 101 | + $this->assertTrue($event->getRequest()->attributes->has('_rate_limit')); |
| 102 | + |
| 103 | + $this->assertEquals( |
| 104 | + $rateLimit, |
| 105 | + $event->getRequest()->attributes->get('_rate_limit') |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + public function testItSetsRateLimitIfConfigurationProvidedWithCustomValueOnMoreThanOneRoute(): void |
| 110 | + { |
| 111 | + $this->createReadRateLimitConfigurationListener([ |
| 112 | + 'some_route_name' => [ |
| 113 | + 'limit' => 100, |
| 114 | + ], |
| 115 | + 'an_other_route' => [ |
| 116 | + 'period' => 15, |
| 117 | + ], |
| 118 | + ]); |
| 119 | + $request = $this->createMock(Request::class); |
| 120 | + $request->attributes = new ParameterBag(); |
| 121 | + $event = $this->createEvent($request); |
| 122 | + |
| 123 | + $this->rateLimitModifiers[0]->expects($this->once())->method('support')->willReturn(true); |
| 124 | + $rateLimit = new RateLimit(100, $this->periodDefaultValue); |
| 125 | + $rateLimit->varyHashOn('_route', 'some_route_name'); |
| 126 | + $this->rateLimitModifiers[0]->expects($this->once())->method('modifyRateLimit')->with($request, $rateLimit); |
| 127 | + |
| 128 | + $this->rateLimitModifiers[1]->expects($this->once())->method('support')->willReturn(false); |
| 129 | + $this->rateLimitModifiers[1]->expects($this->never())->method('modifyRateLimit'); |
| 130 | + |
| 131 | + $this->readRateLimitConfigurationListener->onKernelController($event); |
| 132 | + $this->assertTrue($event->getRequest()->attributes->has('_rate_limit')); |
| 133 | + |
| 134 | + $this->assertEquals( |
| 135 | + $rateLimit, |
| 136 | + $event->getRequest()->attributes->get('_rate_limit') |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + protected function createEvent(Request $request = null, string $serviceId = null): ControllerEvent |
| 141 | + { |
| 142 | + $request = $request ?? new Request(); |
| 143 | + $request->attributes->set('_controller', $serviceId ?? FakeInvokableClass::class); |
| 144 | + $request->attributes->set('_route', 'some_route_name'); |
| 145 | + |
| 146 | + return new ControllerEvent( |
| 147 | + $this->createMock(HttpKernelInterface::class), |
| 148 | + new FakeInvokableClass(), |
| 149 | + $request, |
| 150 | + HttpKernelInterface::MASTER_REQUEST |
| 151 | + ); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +class FakeInvokableClass |
| 156 | +{ |
| 157 | + public function __invoke(): void |
| 158 | + { |
| 159 | + } |
| 160 | +} |
0 commit comments