Skip to content

Commit 5d9f55e

Browse files
committed
🍺 👍
1 parent bde132a commit 5d9f55e

File tree

7 files changed

+32
-11
lines changed

7 files changed

+32
-11
lines changed

.phpunit.cache/test-results

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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}}
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,"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}}

src/Builders/UrlBuilder/Options/BaseOption.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function resolve(Collection &$shortUrlCollection): void
2323
$domain = $shortUrlCollection->get('domain');
2424
$identifier = $shortUrlCollection->has('identifier')
2525
? $shortUrlCollection->get('identifier')
26-
: $this->generateUrlIdentifier();
26+
: $this->generateUrlIdentifier($domain);
2727

2828
$shortUrlCollection = $shortUrlCollection->merge([
2929
'identifier' => $identifier,

src/Repositories/UrlRepository.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ public static function identifierExists(string $identifier): bool
114114
return (new ShortUrl())->where('identifier', $identifier)->exists();
115115
}
116116

117+
public static function domainIdentifierExists(string $domain, string $identifier): bool
118+
{
119+
return (new ShortUrl())->where([
120+
'identifier' => $identifier,
121+
'domain' => $domain
122+
])->exists();
123+
}
124+
117125
public static function hashExists(string $hashed): ?ShortUrl
118126
{
119127
return (new ShortUrl())->where('hashed', $hashed)->first();

src/Traits/ShortUrlHelper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ public static function filterClickValidation(array $filter): array
8484
return $filter;
8585
}
8686

87-
private function generateUrlIdentifier(): string
87+
private function generateUrlIdentifier(string $domain): string
8888
{
8989
$identifier = Str::random(
9090
config('urlshortener.branding.identifier.length') ?? 6
9191
);
9292

93-
if (UrlRepository::identifierExists($identifier)) {
94-
return $this->generateUrlIdentifier();
93+
if (UrlRepository::domainIdentifierExists($domain, $identifier)) {
94+
return $this->generateUrlIdentifier($domain);
9595
}
9696

9797
return $identifier;

src/Utility/Factories/ShortUrlFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public function definition()
2929
$plain_text = 'something-really-really-long.com/even/longer/thanks?ref=please&no=more&ref='.rand(4, 999999);
3030

3131
return [
32-
'domain' => 'short.url',
32+
'domain' => $domain = 'short.url',
3333
'plain_text' => $plain_text,
3434
'hashed' => md5($plain_text),
35-
'identifier' => $this->generateUrlIdentifier(),
35+
'identifier' => $this->generateUrlIdentifier($domain),
3636
'activation' => null,
3737
'expiration' => Carbon::now()->addDays(60)->timestamp,
3838
'password' => null,

tests/Unit/Repositories/UrlRepositoryTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ public function test_accessor_removes_duplicate_query_tag()
118118
$this->assertNotEquals($link, $record);
119119
}
120120

121+
public function test_domain_identifier_exists()
122+
{
123+
$shortUrl = ShortUrl::factory()->create([
124+
'domain' => $domain = 'test.domain',
125+
'plain_text' => $link = 'http://test.com'.$this->getDuplicateShortUrlQueryTag(),
126+
'hashed' => md5($link),
127+
]);
128+
129+
$this->assertFalse(UrlRepository::domainIdentifierExists($shortUrl->domain, $shortUrl->identifier.'333'));
130+
131+
$this->assertTrue(UrlRepository::domainIdentifierExists($shortUrl->domain, $shortUrl->identifier));
132+
}
133+
121134
/**
122135
* @test
123136
*

tests/Unit/Services/UrlServiceTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class UrlServiceTest extends TestCase
2727
public function it_can_find_a_short_url_by_utm_combination()
2828
{
2929
// extra url to filter through
30-
UrlService::shorten('testing.com/something/so/long/i/need/a/short/url'.rand(999, 999999))
30+
UrlService::shorten('testing.com/something/so/long/i/need/a/short/url'.rand(999, 999999), $domain = 'test.domain')
3131
->withTracing([
3232
'utm_campaign' => 'alpha',
3333
'utm_source' => 'alpha',
@@ -43,12 +43,12 @@ public function it_can_find_a_short_url_by_utm_combination()
4343
'utm_medium' => 'testing',
4444
];
4545

46-
UrlService::shorten($targetPlainText)
46+
UrlService::shorten($targetPlainText, $domain)
4747
->withTracing($targetUtmCombination)
4848
->build();
4949

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

107107
$this->assertEquals($shortUrl->plain_text, $this->plain_text);
108108
}

0 commit comments

Comments
 (0)