Skip to content

Commit

Permalink
major change for always defining domain when creating short urls
Browse files Browse the repository at this point in the history
  • Loading branch information
yordadev committed Jun 28, 2024
1 parent e626a11 commit eddc3ee
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/.idea
/.vscode
.phpunit.cache/test-results
.phpunit.cache/test-results
1 change: 1 addition & 0 deletions src/Builders/UrlBuilder/Options/WithExpiration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function resolve(Collection &$shortUrlCollection): void
{
UrlRepository::updateShortUrl(
$shortUrlCollection->get('identifier'),
$shortUrlCollection->get('domain'),
['expiration' => $shortUrlCollection->get('expiration')]
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Builders/UrlBuilder/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand Down
6 changes: 3 additions & 3 deletions src/Repositories/UrlRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
8 changes: 4 additions & 4 deletions src/Services/UrlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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(
Expand Down
6 changes: 4 additions & 2 deletions tests/Unit/Repositories/ClickRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Repositories/UrlRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Services/ClickServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 7 additions & 8 deletions tests/Unit/Services/UrlServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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'));
}

/**
Expand All @@ -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',
Expand All @@ -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();

Expand Down

0 comments on commit eddc3ee

Please sign in to comment.