|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodeOfDigital\LaravelUrlShortener\Tests\Unit; |
| 4 | + |
| 5 | +use CodeOfDigital\LaravelUrlShortener\Drivers\FirebaseDriverShortener; |
| 6 | +use CodeOfDigital\LaravelUrlShortener\Exceptions\BadRequestException; |
| 7 | +use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidApiTokenException; |
| 8 | +use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException; |
| 9 | + |
| 10 | +class FirebaseShortenerTest extends TestCase |
| 11 | +{ |
| 12 | + protected $shortener; |
| 13 | + |
| 14 | + protected function setUp(): void |
| 15 | + { |
| 16 | + parent::setUp(); |
| 17 | + $this->shortener = new FirebaseDriverShortener($this->client, 'API_TOKEN', 'URI_PREFIX', 'UNGUESSABLE'); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Test the shortening of URLs through Firebase |
| 22 | + * |
| 23 | + * @throws ShortUrlException |
| 24 | + */ |
| 25 | + public function testUrlShortening() |
| 26 | + { |
| 27 | + $this->client->queue(require __DIR__ . '/../Responses/firebase/http-200.php'); |
| 28 | + |
| 29 | + $shortUrl = $this->shortener->shorten('https://laravel.com'); |
| 30 | + $request = $this->client->getRequest(0); |
| 31 | + |
| 32 | + $this->assertNotNull($request); |
| 33 | + $this->assertEquals('POST', $request->getMethod()); |
| 34 | + $this->assertEquals('firebasedynamiclinks.googleapis.com', $request->getUri()->getHost()); |
| 35 | + $this->assertEquals('/v1/shortLinks?key=API_TOKEN', $request->getRequestTarget()); |
| 36 | + $this->assertEquals('application/json', $request->getHeader('Content-Type')[0]); |
| 37 | + $this->assertEquals('application/json', $request->getHeader('Accept')[0]); |
| 38 | + |
| 39 | + $expected = '{"dynamicLinkInfo":{"domainUriPrefix":"URI_PREFIX","link":"https:\/\/laravel.com"},"suffix":{"option":"UNGUESSABLE"}}'; |
| 40 | + $this->assertJsonStringEqualsJsonString($expected, $request->getBody()->getContents()); |
| 41 | + |
| 42 | + $this->assertEquals('https://codeofdigital.page.link/CBNYHKkLc6FZmuYU9', $shortUrl); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Test failure to authenticate with Firebase |
| 47 | + * |
| 48 | + * @throws ShortUrlException |
| 49 | + */ |
| 50 | + public function testUnauthorized() |
| 51 | + { |
| 52 | + $this->client->queue(require __DIR__ . '/../Responses/firebase/http-400.php'); |
| 53 | + $this->expectException(BadRequestException::class); |
| 54 | + $this->shortener->shorten('https://laravel.com'); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Test failure if parsed URL is invalid or incorrect format |
| 59 | + * |
| 60 | + * @throws ShortUrlException |
| 61 | + */ |
| 62 | + public function testInvalidUrl() |
| 63 | + { |
| 64 | + $this->expectException(ShortUrlException::class); |
| 65 | + $this->shortener->shorten('some-string.com'); |
| 66 | + } |
| 67 | +} |
0 commit comments