Skip to content

Commit

Permalink
🍺 👍
Browse files Browse the repository at this point in the history
  • Loading branch information
yordadev committed Jun 28, 2024
1 parent bde132a commit 5d9f55e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":1,"defects":{"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::test_it_can_find_by_identifier_plain_text_does_not_have_duplicate_query_key":8,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_find_short_url_by_hash":8,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_can_find_short_url_by_the_hash":8,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_can_find_short_url_by_the_plain_text":7,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_can_find_short_url_by_the_identifier":8,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_set_an_activation_time_successfully":8,"ShortUrlHelperTest::it_can_validate_ownership_is_not_array_of_models_filter":5,"YorCreative\\UrlShortener\\Tests\\Feature\\ShortUrlBasicTest::it_can_create_a_basic_short_url":7},"times":{"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::test_it_can_find_by_identifier_plain_text_does_not_have_duplicate_query_key":0.066,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::test_accessor_removes_duplicate_query_tag":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\ClickRepositoryTest::it_can_find_a_click_by_its_id":0.022,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\ClickRepositoryTest::it_can_create_a_click_in_db":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\ClickRepositoryTest::it_can_get_correct_with_default_relations":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\TracingRepositoryTest::it_can_detect_all_utm_parameters":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\TracingRepositoryTest::it_has_correct_allowed_parameters":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\TracingRepositoryTest::it_can_create_a_trace_record":0.013,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_find_ownership_record":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_create_ownership_record":0.003,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_find_short_url_by_hash":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_create_a_short_url":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_update_a_short_url":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_bool_that_identifier_exists":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\ClickServiceTest::it_can_can_track_a_click":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\ClickServiceTest::it_can_get_basic_scoped_clicks_for_short_url":0.019,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_find_a_short_url_by_utm_combination":0.003,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_can_find_short_url_by_the_hash":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_can_find_short_url_by_the_plain_text":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_can_find_short_url_by_the_identifier":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_successfully_attempt_to_verify_password":0.005,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_successfully_attempt_to_verify_password_and_fail":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_attach_ownership_to_short_url":0.002,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_set_an_activation_time_successfully":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UtilityServiceTest::it_can_successfully_get_an_instance_of_the_encrypter":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UtilityServiceTest::it_can_get_the_redirect_code":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UtilityServiceTest::it_can_get_redirect_headers":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UtilityServiceTest::it_can_construct_redirect_headers_with_dynamic_headers":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UtilityServiceTest::it_can_construct_redirect_headers":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_add_filter_strategies":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_outcome_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_batch_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_identifier_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_status_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_ownership_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_tracing_utm_id_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_tracing_utm_campaign_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_tracing_utm_source_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_tracing_utm_medium_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_tracing_utm_content_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_tracing_utm_term_filter":0,"ShortUrlHelperTest::it_can_validate_ownership_is_array_of_models_filter":0.001,"ShortUrlHelperTest::it_can_validate_ownership_is_not_array_of_models_filter":0.005,"ShortUrlHelperTest::it_can_build_short_url":0,"YorCreative\\UrlShortener\\Tests\\Feature\\ShortUrlBasicTest::it_can_create_a_basic_short_url":0.001,"YorCreative\\UrlShortener\\Tests\\Feature\\ShortUrlClickTest::it_can_track_and_retrieve_successfully_routed_clicks":0.01,"YorCreative\\UrlShortener\\Tests\\Feature\\ShortUrlClickTest::it_can_successfully_routed_clicks_while_filtering_for_utm_source":0.008}}
{"version":1,"defects":{"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::test_it_can_find_by_identifier_plain_text_does_not_have_duplicate_query_key":8,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_find_short_url_by_hash":8,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_can_find_short_url_by_the_hash":8,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_can_find_short_url_by_the_plain_text":7,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_can_find_short_url_by_the_identifier":8,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_set_an_activation_time_successfully":8,"ShortUrlHelperTest::it_can_validate_ownership_is_not_array_of_models_filter":5,"YorCreative\\UrlShortener\\Tests\\Feature\\ShortUrlBasicTest::it_can_create_a_basic_short_url":7,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\TracingRepositoryTest::it_can_create_a_trace_record":8,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_find_ownership_record":8,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::test_accessor_removes_duplicate_query_tag":8,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::test_domain_identifier_exists":8,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_find_a_short_url_by_utm_combination":8},"times":{"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::test_it_can_find_by_identifier_plain_text_does_not_have_duplicate_query_key":0.066,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::test_accessor_removes_duplicate_query_tag":0.002,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\ClickRepositoryTest::it_can_find_a_click_by_its_id":0.03,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\ClickRepositoryTest::it_can_create_a_click_in_db":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\ClickRepositoryTest::it_can_get_correct_with_default_relations":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\TracingRepositoryTest::it_can_detect_all_utm_parameters":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\TracingRepositoryTest::it_has_correct_allowed_parameters":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\TracingRepositoryTest::it_can_create_a_trace_record":0.017,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_find_ownership_record":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_create_ownership_record":0.003,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_find_short_url_by_hash":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_create_a_short_url":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_update_a_short_url":0.002,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::it_can_bool_that_identifier_exists":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\ClickServiceTest::it_can_can_track_a_click":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\ClickServiceTest::it_can_get_basic_scoped_clicks_for_short_url":0.026,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_find_a_short_url_by_utm_combination":0.003,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_can_find_short_url_by_the_hash":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_can_find_short_url_by_the_plain_text":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_can_find_short_url_by_the_identifier":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_successfully_attempt_to_verify_password":0.005,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_successfully_attempt_to_verify_password_and_fail":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_attach_ownership_to_short_url":0.003,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UrlServiceTest::it_can_set_an_activation_time_successfully":0.002,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UtilityServiceTest::it_can_successfully_get_an_instance_of_the_encrypter":0.001,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UtilityServiceTest::it_can_get_the_redirect_code":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UtilityServiceTest::it_can_get_redirect_headers":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UtilityServiceTest::it_can_construct_redirect_headers_with_dynamic_headers":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\UtilityServiceTest::it_can_construct_redirect_headers":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_add_filter_strategies":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_outcome_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_batch_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_identifier_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_status_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_ownership_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_tracing_utm_id_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_tracing_utm_campaign_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_tracing_utm_source_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_tracing_utm_medium_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_tracing_utm_content_filter":0,"YorCreative\\UrlShortener\\Tests\\Unit\\Services\\FilterClicksStrategyTest::it_can_can_process_tracing_utm_term_filter":0,"ShortUrlHelperTest::it_can_validate_ownership_is_array_of_models_filter":0.001,"ShortUrlHelperTest::it_can_validate_ownership_is_not_array_of_models_filter":0.004,"ShortUrlHelperTest::it_can_build_short_url":0,"YorCreative\\UrlShortener\\Tests\\Feature\\ShortUrlBasicTest::it_can_create_a_basic_short_url":0.001,"YorCreative\\UrlShortener\\Tests\\Feature\\ShortUrlClickTest::it_can_track_and_retrieve_successfully_routed_clicks":0.009,"YorCreative\\UrlShortener\\Tests\\Feature\\ShortUrlClickTest::it_can_successfully_routed_clicks_while_filtering_for_utm_source":0.009,"YorCreative\\UrlShortener\\Tests\\Unit\\Repositories\\UrlRepositoryTest::test_domain_identifier_exists":0.001}}
2 changes: 1 addition & 1 deletion src/Builders/UrlBuilder/Options/BaseOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function resolve(Collection &$shortUrlCollection): void
$domain = $shortUrlCollection->get('domain');
$identifier = $shortUrlCollection->has('identifier')
? $shortUrlCollection->get('identifier')
: $this->generateUrlIdentifier();
: $this->generateUrlIdentifier($domain);

$shortUrlCollection = $shortUrlCollection->merge([
'identifier' => $identifier,
Expand Down
8 changes: 8 additions & 0 deletions src/Repositories/UrlRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ public static function identifierExists(string $identifier): bool
return (new ShortUrl())->where('identifier', $identifier)->exists();
}

public static function domainIdentifierExists(string $domain, string $identifier): bool
{
return (new ShortUrl())->where([
'identifier' => $identifier,
'domain' => $domain
])->exists();
}

public static function hashExists(string $hashed): ?ShortUrl
{
return (new ShortUrl())->where('hashed', $hashed)->first();
Expand Down
6 changes: 3 additions & 3 deletions src/Traits/ShortUrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ public static function filterClickValidation(array $filter): array
return $filter;
}

private function generateUrlIdentifier(): string
private function generateUrlIdentifier(string $domain): string
{
$identifier = Str::random(
config('urlshortener.branding.identifier.length') ?? 6
);

if (UrlRepository::identifierExists($identifier)) {
return $this->generateUrlIdentifier();
if (UrlRepository::domainIdentifierExists($domain, $identifier)) {
return $this->generateUrlIdentifier($domain);
}

return $identifier;
Expand Down
4 changes: 2 additions & 2 deletions src/Utility/Factories/ShortUrlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public function definition()
$plain_text = 'something-really-really-long.com/even/longer/thanks?ref=please&no=more&ref='.rand(4, 999999);

return [
'domain' => 'short.url',
'domain' => $domain = 'short.url',
'plain_text' => $plain_text,
'hashed' => md5($plain_text),
'identifier' => $this->generateUrlIdentifier(),
'identifier' => $this->generateUrlIdentifier($domain),
'activation' => null,
'expiration' => Carbon::now()->addDays(60)->timestamp,
'password' => null,
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Repositories/UrlRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ public function test_accessor_removes_duplicate_query_tag()
$this->assertNotEquals($link, $record);
}

public function test_domain_identifier_exists()
{
$shortUrl = ShortUrl::factory()->create([
'domain' => $domain = 'test.domain',
'plain_text' => $link = 'http://test.com'.$this->getDuplicateShortUrlQueryTag(),
'hashed' => md5($link),
]);

$this->assertFalse(UrlRepository::domainIdentifierExists($shortUrl->domain, $shortUrl->identifier.'333'));

$this->assertTrue(UrlRepository::domainIdentifierExists($shortUrl->domain, $shortUrl->identifier));
}

/**
* @test
*
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Services/UrlServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class UrlServiceTest extends TestCase
public function it_can_find_a_short_url_by_utm_combination()
{
// extra url to filter through
UrlService::shorten('testing.com/something/so/long/i/need/a/short/url'.rand(999, 999999))
UrlService::shorten('testing.com/something/so/long/i/need/a/short/url'.rand(999, 999999), $domain = 'test.domain')
->withTracing([
'utm_campaign' => 'alpha',
'utm_source' => 'alpha',
Expand All @@ -43,12 +43,12 @@ public function it_can_find_a_short_url_by_utm_combination()
'utm_medium' => 'testing',
];

UrlService::shorten($targetPlainText)
UrlService::shorten($targetPlainText, $domain)
->withTracing($targetUtmCombination)
->build();

// extra url to filter through
UrlService::shorten('testing.com/something/so/long/i/need/a/short/url'.rand(999, 999999))
UrlService::shorten('testing.com/something/so/long/i/need/a/short/url'.rand(999, 999999), $domain)
->withTracing([
'utm_campaign' => 'alpha',
'utm_source' => 'charlie',
Expand Down Expand Up @@ -102,7 +102,7 @@ public function it_can_can_find_short_url_by_the_plain_text()
*/
public function it_can_can_find_short_url_by_the_identifier()
{
$shortUrl = UrlService::findByIdentifier($this->identifier);
$shortUrl = UrlService::findByIdentifier($this->identifier, $this->base);

$this->assertEquals($shortUrl->plain_text, $this->plain_text);
}
Expand Down

0 comments on commit 5d9f55e

Please sign in to comment.