|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodeOfDigital\LaravelUrlShortener\Tests\Unit; |
| 4 | + |
| 5 | +use CodeOfDigital\LaravelUrlShortener\Drivers\BitLyDriverShortener; |
| 6 | +use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidApiTokenException; |
| 7 | +use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException; |
| 8 | + |
| 9 | +class BitLyShortenerTest extends TestCase |
| 10 | +{ |
| 11 | + protected $shortener; |
| 12 | + |
| 13 | + protected function setUp(): void |
| 14 | + { |
| 15 | + parent::setUp(); |
| 16 | + $this->shortener = new BitLyDriverShortener($this->client, 'API_TOKEN', false); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Test the URL Shortening through Bit.ly |
| 21 | + * |
| 22 | + * @throws \CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException |
| 23 | + */ |
| 24 | + public function testUrlShortening() |
| 25 | + { |
| 26 | + $this->client->queue(require __DIR__ . '/../Responses/bit_ly/http-200.php'); |
| 27 | + |
| 28 | + $shortUrl = $this->shortener->shorten('https://laravel.com'); |
| 29 | + $request = $this->client->getRequest(0); |
| 30 | + |
| 31 | + $this->assertNotNull($request); |
| 32 | + $this->assertEquals('POST', $request->getMethod()); |
| 33 | + $this->assertEquals('api-ssl.bitly.com', $request->getUri()->getHost()); |
| 34 | + $this->assertEquals('/v4/shorten', $request->getRequestTarget()); |
| 35 | + $this->assertEquals('Bearer API_TOKEN', $request->getHeader('Authorization')[0]); |
| 36 | + $this->assertEquals('application/json', $request->getHeader('Content-Type')[0]); |
| 37 | + |
| 38 | + $this->assertEquals('https://bit.ly/3iSAOvF', $shortUrl); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Test failure to authenticate with Bit.ly |
| 43 | + * |
| 44 | + * @throws \CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException |
| 45 | + */ |
| 46 | + public function testUnauthorized() |
| 47 | + { |
| 48 | + $this->client->queue(require __DIR__ . '/../Responses/bit_ly/http-403.php'); |
| 49 | + $this->expectException(InvalidApiTokenException::class); |
| 50 | + $this->shortener->shorten('https://laravel.com'); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Test failure if parsed URL is invalid or incorrect format |
| 55 | + * |
| 56 | + * @throws ShortUrlException |
| 57 | + */ |
| 58 | + public function testInvalidUrl() |
| 59 | + { |
| 60 | + $this->expectException(ShortUrlException::class); |
| 61 | + $this->shortener->shorten('some-string.com'); |
| 62 | + } |
| 63 | +} |
0 commit comments