diff --git a/.gitignore b/.gitignore index 9e317c3..d10260e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /.idea /.vscode .phpunit.cache/test-results +.phpunit.cache/test-results diff --git a/src/Builders/UrlBuilder/Options/WithExpiration.php b/src/Builders/UrlBuilder/Options/WithExpiration.php index 74a15e3..8d83d76 100644 --- a/src/Builders/UrlBuilder/Options/WithExpiration.php +++ b/src/Builders/UrlBuilder/Options/WithExpiration.php @@ -16,6 +16,7 @@ public function resolve(Collection &$shortUrlCollection): void { UrlRepository::updateShortUrl( $shortUrlCollection->get('identifier'), + $shortUrlCollection->get('domain'), ['expiration' => $shortUrlCollection->get('expiration')] ); } diff --git a/src/Builders/UrlBuilder/UrlBuilder.php b/src/Builders/UrlBuilder/UrlBuilder.php index 840d62d..2653f34 100644 --- a/src/Builders/UrlBuilder/UrlBuilder.php +++ b/src/Builders/UrlBuilder/UrlBuilder.php @@ -10,7 +10,6 @@ use YorCreative\UrlShortener\Builders\UrlBuilder\Options\BaseOption; use YorCreative\UrlShortener\Builders\UrlBuilder\Options\WithActivation; use YorCreative\UrlShortener\Builders\UrlBuilder\Options\WithBrandedIdentifier; -use YorCreative\UrlShortener\Builders\UrlBuilder\Options\WithDomain; use YorCreative\UrlShortener\Builders\UrlBuilder\Options\WithExpiration; use YorCreative\UrlShortener\Builders\UrlBuilder\Options\WithOpenLimit; use YorCreative\UrlShortener\Builders\UrlBuilder\Options\WithOwnership; @@ -42,12 +41,13 @@ public function __construct() $this->shortUrlCollection = new Collection(); } - public static function shorten(string $plain_text): UrlBuilder + public static function shorten(string $plain_text, ?string $domain = null): UrlBuilder { $b = self::$builder = new static; $b->shortUrlCollection->put('plain_text', $url = $plain_text.$b->getDuplicateShortUrlQueryTag()); $b->shortUrlCollection->put('hashed', md5($url)); + $b->shortUrlCollection->put('domain', $domain); $b->options->add(new BaseOption()); diff --git a/src/Repositories/UrlRepository.php b/src/Repositories/UrlRepository.php index c037ca4..eb98af5 100644 --- a/src/Repositories/UrlRepository.php +++ b/src/Repositories/UrlRepository.php @@ -100,10 +100,10 @@ public static function create(array $ShortUrl): ShortUrl public static function updateShortUrl(string $identifier, string $domain, array $updates): ShortUrl { try { - $ShortUrlRecord = self::findByDomainIdentifier($domain, $identifier); - $ShortUrlRecord->update($updates); + $shortUrlRecord = self::findByDomainIdentifier($domain, $identifier); + $shortUrlRecord->update($updates); - return $ShortUrlRecord; + return $shortUrlRecord; } catch (Exception $exception) { throw new UrlRepositoryException($exception->getMessage()); } diff --git a/src/Services/UrlService.php b/src/Services/UrlService.php index 2d2b2b5..603f184 100644 --- a/src/Services/UrlService.php +++ b/src/Services/UrlService.php @@ -53,9 +53,9 @@ public static function findByUtmCombination(array $utm_combination): Collection * @throws UrlRepositoryException * @throws UtilityServiceException */ - public static function attempt(string $identifier, string $password): ?ShortUrl + public static function attempt(string $identifier, ?string $domain, string $password): ?ShortUrl { - if (! $shortUrl = UrlRepository::findByIdentifier($identifier)) { + if (! $shortUrl = UrlRepository::findByDomainIdentifier($domain, $identifier)) { return null; } @@ -85,9 +85,9 @@ public static function attachOwnership($domain, $identifier, $type, $id): void } } - public static function shorten(string $plain_text): UrlBuilder + public static function shorten(string $plain_text, ?string $domain = null): UrlBuilder { - return UrlBuilder::shorten($plain_text); + return UrlBuilder::shorten($plain_text, $domain); } /** diff --git a/tests/TestCase.php b/tests/TestCase.php index c19dac5..509bf24 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -39,11 +39,11 @@ public function setUp(): void $migration->up(); }); - $this->base = 'localhost.test/v1/'; + $this->base = 'localhost.test'; $this->plain_text = $this->getPlainText(); $this->hashed = md5($this->plain_text); - $this->url = UrlService::shorten($this->plain_text)->withTracing([ + $this->url = UrlService::shorten($this->plain_text, 'localhost.test')->withTracing([ TracingRepository::$ID => 'testing', TracingRepository::$CAMPAIGN => 'testing', TracingRepository::$SOURCE => 'testing', @@ -52,9 +52,9 @@ public function setUp(): void TracingRepository::$TERM => 'testing', ])->build(); - $this->identifier = str_replace($this->base, '', $this->url); + $this->identifier = str_replace($this->base.'/v1/', '', $this->url); - $this->shortUrl = UrlService::findByIdentifier($this->identifier); + $this->shortUrl = UrlService::findByIdentifier($this->identifier, $this->base); $this->request = Request::create('something-short.com/not-really'); $this->changeRequestIp( diff --git a/tests/Unit/Repositories/ClickRepositoryTest.php b/tests/Unit/Repositories/ClickRepositoryTest.php index 8352dfd..763a282 100644 --- a/tests/Unit/Repositories/ClickRepositoryTest.php +++ b/tests/Unit/Repositories/ClickRepositoryTest.php @@ -21,14 +21,16 @@ public function it_can_find_a_click_by_its_id() { Config::set('location.testing.enabled', true); + Config::set('location.testing.ip', $ip = '66.102.0.0'); + ClickService::track( $this->identifier, - '0.0.0.0', + $ip, ClickService::$SUCCESS_ROUTED ); $this->assertEquals( - '0.0.0.0', + $ip, ClickRepository::findById(1)->toArray()['location']['ip'] ); } diff --git a/tests/Unit/Repositories/UrlRepositoryTest.php b/tests/Unit/Repositories/UrlRepositoryTest.php index bd16618..4e80c5d 100644 --- a/tests/Unit/Repositories/UrlRepositoryTest.php +++ b/tests/Unit/Repositories/UrlRepositoryTest.php @@ -96,11 +96,11 @@ public function it_can_update_a_short_url() { $this->assertNull($this->shortUrl->activation); - UrlRepository::updateShortUrl($this->identifier, [ + UrlRepository::updateShortUrl($this->identifier, $this->base, [ 'activation' => Carbon::now()->timestamp, ]); - $shortUrl = UrlRepository::findByIdentifier($this->identifier); + $shortUrl = UrlRepository::findByIdentifier($this->identifier, $this->base); $this->assertNotNull($shortUrl->activation); } diff --git a/tests/Unit/Services/ClickServiceTest.php b/tests/Unit/Services/ClickServiceTest.php index d1b1e5e..6018635 100644 --- a/tests/Unit/Services/ClickServiceTest.php +++ b/tests/Unit/Services/ClickServiceTest.php @@ -22,10 +22,10 @@ class ClickServiceTest extends TestCase */ public function it_can_can_track_a_click() { - $ip = '0.0.0.0'; - Config::set('location.testing.enabled', true); + Config::set('location.testing.ip', $ip = '66.102.0.0'); + ClickService::track( $this->identifier, $ip, @@ -59,10 +59,10 @@ public function it_can_can_track_a_click() */ public function it_can_get_basic_scoped_clicks_for_short_url() { - $ip = '0.0.0.0'; - Config::set('location.testing.enabled', true); + Config::set('location.testing.ip', $ip = '66.102.0.0'); + ClickService::track( $this->identifier, $ip, diff --git a/tests/Unit/Services/UrlServiceTest.php b/tests/Unit/Services/UrlServiceTest.php index acc5603..d5b91b8 100644 --- a/tests/Unit/Services/UrlServiceTest.php +++ b/tests/Unit/Services/UrlServiceTest.php @@ -120,13 +120,12 @@ public function it_can_successfully_attempt_to_verify_password() { $plain_text = 'something.com/really-long'.rand(5, 9999); - $url = UrlBuilder::shorten($plain_text) + $url = UrlBuilder::shorten($plain_text, $this->base) ->withPassword('password') ->build(); - $identifier = str_replace($this->base, '', $url); - - $shortUrl = UrlService::attempt($identifier, 'password'); + $identifier = str_replace($this->base .'/v1/', '', $url); + $shortUrl = UrlService::attempt($identifier, $this->base, 'password'); $this->assertTrue($plain_text == $shortUrl->plain_text); } @@ -144,13 +143,13 @@ public function it_can_successfully_attempt_to_verify_password_and_fail() { $plain_text = 'something.com/really-long'.rand(5, 9999); - $url = UrlBuilder::shorten($plain_text) + $url = UrlBuilder::shorten($plain_text, $domain = 'test.domain') ->withPassword('password') ->build(); $identifier = str_replace($this->base, '', $url); - $this->assertNull(UrlService::attempt($identifier, 'not_password')); + $this->assertNull(UrlService::attempt($identifier, $domain,'not_password')); } /** @@ -173,7 +172,7 @@ public function it_can_attach_ownership_to_short_url() 'ownerable_id' => $owner->$primary_key, ]; - UrlService::attachOwnership($this->identifier, $ownership['ownerable_type'], $ownership['ownerable_id']); + UrlService::attachOwnership($this->base, $this->identifier, $ownership['ownerable_type'], $ownership['ownerable_id']); $this->assertDatabaseHas( 'short_url_ownerships', @@ -192,7 +191,7 @@ public function it_can_attach_ownership_to_short_url() */ public function it_can_set_an_activation_time_successfully() { - $shortUrl = UrlService::shorten('something') + $shortUrl = UrlService::shorten('something', $domain = 'test.domain') ->withActivation(Carbon::now()->addMinute()->timestamp) ->build();