|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace BikeShare\Test\Unit\App\Api; |
| 6 | + |
| 7 | +use BikeShare\App\Api\ClientVersionDetector; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Symfony\Component\HttpFoundation\Request; |
| 11 | + |
| 12 | +class ClientVersionDetectorTest extends TestCase |
| 13 | +{ |
| 14 | + private ClientVersionDetector $detector; |
| 15 | + |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + $this->detector = new ClientVersionDetector(); |
| 19 | + } |
| 20 | + |
| 21 | + #[DataProvider('userAgentProvider')] |
| 22 | + public function testGetClientVersion(string $userAgent, string $expectedVersion): void |
| 23 | + { |
| 24 | + $request = Request::create('/api/v1/admin/users'); |
| 25 | + $request->headers->set('User-Agent', $userAgent); |
| 26 | + |
| 27 | + $this->assertSame($expectedVersion, $this->detector->getClientVersion($request)); |
| 28 | + } |
| 29 | + |
| 30 | + public static function userAgentProvider(): array |
| 31 | + { |
| 32 | + return [ |
| 33 | + 'Android 1.0.1' => ['BikeShare-Android/1.0.1 (2)', '1.0.1'], |
| 34 | + 'Android 2.0.0' => ['BikeShare-Android/2.0.0 (5)', '2.0.0'], |
| 35 | + 'Android 1.0.0' => ['BikeShare-Android/1.0.0 (1)', '1.0.0'], |
| 36 | + 'Android 0.9.0' => ['BikeShare-Android/0.9.0 (1)', '0.9.0'], |
| 37 | + 'custom app name' => ['WhiteBikes-Android/1.2.3 (4)', '1.2.3'], |
| 38 | + 'old Android okhttp' => ['okhttp/4.12.0', '0.0.0'], |
| 39 | + 'browser' => ['Mozilla/5.0 (Linux; Android 14) AppleWebKit/537.36', '999.0.0'], |
| 40 | + 'empty UA' => ['', '999.0.0'], |
| 41 | + 'curl' => ['curl/8.5.0', '999.0.0'], |
| 42 | + ]; |
| 43 | + } |
| 44 | + |
| 45 | + public function testNoUserAgentHeader(): void |
| 46 | + { |
| 47 | + $request = Request::create('/api/v1/admin/users'); |
| 48 | + $request->headers->remove('User-Agent'); |
| 49 | + |
| 50 | + $this->assertSame('999.0.0', $this->detector->getClientVersion($request)); |
| 51 | + } |
| 52 | +} |
0 commit comments