|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace In2code\Lux\Tests\Unit\Domain\Tracker; |
| 4 | + |
| 5 | +use In2code\Lux\Domain\Cache\RateLimiterCache; |
| 6 | +use In2code\Lux\Domain\Model\Fingerprint; |
| 7 | +use In2code\Lux\Domain\Tracker\RateLimiter; |
| 8 | +use In2code\Lux\Events\StopAnyProcessBeforePersistenceEvent; |
| 9 | +use In2code\Lux\Exception\RateLimitException; |
| 10 | +use In2code\Lux\Tests\Helper\TestingHelper; |
| 11 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 12 | +use PHPUnit\Framework\Attributes\Test; |
| 13 | +use Psr\Log\LoggerInterface; |
| 14 | +use TYPO3\TestingFramework\Core\Unit\UnitTestCase; |
| 15 | + |
| 16 | +#[CoversClass(RateLimiter::class)] |
| 17 | +class RateLimiterTest extends UnitTestCase |
| 18 | +{ |
| 19 | + protected bool $resetSingletonInstances = true; |
| 20 | + |
| 21 | + public function setUp(): void |
| 22 | + { |
| 23 | + parent::setUp(); |
| 24 | + TestingHelper::setDefaultConstants(); |
| 25 | + } |
| 26 | + |
| 27 | + #[Test] |
| 28 | + public function itAllowsRequestsUnderLimit(): void |
| 29 | + { |
| 30 | + // Mock cache to return count below limit |
| 31 | + $cacheMock = $this->createMock(RateLimiterCache::class); |
| 32 | + $cacheMock->expects(self::once()) |
| 33 | + ->method('incrementAndGet') |
| 34 | + ->with('test1234567890abcdef1234567890ab1234567890abcdef1234567890abcdef') |
| 35 | + ->willReturn(10); // 10 < 20 (default limit) |
| 36 | + |
| 37 | + $loggerMock = $this->createMock(LoggerInterface::class); |
| 38 | + $loggerMock->expects(self::never()) |
| 39 | + ->method('warning'); // Should not log when under limit |
| 40 | + |
| 41 | + $rateLimiter = new RateLimiter($cacheMock, $loggerMock); |
| 42 | + |
| 43 | + // Create real fingerprint and event objects (they are final) |
| 44 | + $fingerprint = $this->createFingerprintWithValue('test1234567890abcdef1234567890ab1234567890abcdef1234567890abcdef'); |
| 45 | + $event = new StopAnyProcessBeforePersistenceEvent($fingerprint); |
| 46 | + |
| 47 | + // Should not throw exception |
| 48 | + $rateLimiter->__invoke($event); |
| 49 | + |
| 50 | + // If we reach here, the test passes |
| 51 | + self::assertTrue(true); |
| 52 | + } |
| 53 | + |
| 54 | + #[Test] |
| 55 | + public function itBlocksRequestsOverLimit(): void |
| 56 | + { |
| 57 | + // Mock cache to return count over limit |
| 58 | + $cacheMock = $this->createMock(RateLimiterCache::class); |
| 59 | + $cacheMock->expects(self::once()) |
| 60 | + ->method('incrementAndGet') |
| 61 | + ->with('test1234567890abcdef1234567890ab1234567890abcdef1234567890abcdef') |
| 62 | + ->willReturn(21); // 21 > 20 (default limit) |
| 63 | + |
| 64 | + $loggerMock = $this->createMock(LoggerInterface::class); |
| 65 | + |
| 66 | + $rateLimiter = new RateLimiter($cacheMock, $loggerMock); |
| 67 | + |
| 68 | + // Create real fingerprint and event objects (they are final) |
| 69 | + $fingerprint = $this->createFingerprintWithValue('test1234567890abcdef1234567890ab1234567890abcdef1234567890abcdef'); |
| 70 | + $event = new StopAnyProcessBeforePersistenceEvent($fingerprint); |
| 71 | + |
| 72 | + // Should throw exception |
| 73 | + $this->expectException(RateLimitException::class); |
| 74 | + $this->expectExceptionMessage('Rate limit exceeded'); |
| 75 | + $this->expectExceptionCode(1768214806); |
| 76 | + |
| 77 | + $rateLimiter->__invoke($event); |
| 78 | + } |
| 79 | + |
| 80 | + #[Test] |
| 81 | + public function itHandlesEmptyFingerprint(): void |
| 82 | + { |
| 83 | + // Mock cache should not be called |
| 84 | + $cacheMock = $this->createMock(RateLimiterCache::class); |
| 85 | + $cacheMock->expects(self::never()) |
| 86 | + ->method('incrementAndGet'); |
| 87 | + |
| 88 | + $loggerMock = $this->createMock(LoggerInterface::class); |
| 89 | + |
| 90 | + $rateLimiter = new RateLimiter($cacheMock, $loggerMock); |
| 91 | + |
| 92 | + // Create fingerprint with empty value |
| 93 | + $fingerprint = new Fingerprint('example.com', 'TestUA'); |
| 94 | + // Note: value is empty by default since we don't call setValue() |
| 95 | + $event = new StopAnyProcessBeforePersistenceEvent($fingerprint); |
| 96 | + |
| 97 | + // Should return early without exception |
| 98 | + $rateLimiter->__invoke($event); |
| 99 | + |
| 100 | + // If we reach here, the test passes |
| 101 | + self::assertTrue(true); |
| 102 | + } |
| 103 | + |
| 104 | + #[Test] |
| 105 | + public function itIncrementsCounterOnEachRequest(): void |
| 106 | + { |
| 107 | + // Mock cache to verify incrementAndGet is called |
| 108 | + $cacheMock = $this->createMock(RateLimiterCache::class); |
| 109 | + $cacheMock->expects(self::once()) |
| 110 | + ->method('incrementAndGet') |
| 111 | + ->with('test1234567890abcdef1234567890ab1234567890abcdef1234567890abcdef') |
| 112 | + ->willReturn(1); // First request |
| 113 | + |
| 114 | + $loggerMock = $this->createMock(LoggerInterface::class); |
| 115 | + |
| 116 | + $rateLimiter = new RateLimiter($cacheMock, $loggerMock); |
| 117 | + |
| 118 | + // Create real fingerprint and event objects (they are final) |
| 119 | + $fingerprint = $this->createFingerprintWithValue('test1234567890abcdef1234567890ab1234567890abcdef1234567890abcdef'); |
| 120 | + $event = new StopAnyProcessBeforePersistenceEvent($fingerprint); |
| 121 | + |
| 122 | + $rateLimiter->__invoke($event); |
| 123 | + |
| 124 | + // Test passes if incrementAndGet was called |
| 125 | + self::assertTrue(true); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Helper method to create a Fingerprint with a specific value |
| 130 | + * Uses reflection to set the protected value property |
| 131 | + * |
| 132 | + * @param string $value |
| 133 | + * @return Fingerprint |
| 134 | + */ |
| 135 | + protected function createFingerprintWithValue(string $value): Fingerprint |
| 136 | + { |
| 137 | + $fingerprint = new Fingerprint('example.com', 'TestUA'); |
| 138 | + $reflection = new \ReflectionClass($fingerprint); |
| 139 | + $property = $reflection->getProperty('value'); |
| 140 | + $property->setAccessible(true); |
| 141 | + $property->setValue($fingerprint, $value); |
| 142 | + return $fingerprint; |
| 143 | + } |
| 144 | +} |
0 commit comments