Skip to content

Commit eddc3ee

Browse files
committed
major change for always defining domain when creating short urls
1 parent e626a11 commit eddc3ee

File tree

10 files changed

+32
-29
lines changed

10 files changed

+32
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/.idea
66
/.vscode
77
.phpunit.cache/test-results
8+
.phpunit.cache/test-results

src/Builders/UrlBuilder/Options/WithExpiration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function resolve(Collection &$shortUrlCollection): void
1616
{
1717
UrlRepository::updateShortUrl(
1818
$shortUrlCollection->get('identifier'),
19+
$shortUrlCollection->get('domain'),
1920
['expiration' => $shortUrlCollection->get('expiration')]
2021
);
2122
}

src/Builders/UrlBuilder/UrlBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use YorCreative\UrlShortener\Builders\UrlBuilder\Options\BaseOption;
1111
use YorCreative\UrlShortener\Builders\UrlBuilder\Options\WithActivation;
1212
use YorCreative\UrlShortener\Builders\UrlBuilder\Options\WithBrandedIdentifier;
13-
use YorCreative\UrlShortener\Builders\UrlBuilder\Options\WithDomain;
1413
use YorCreative\UrlShortener\Builders\UrlBuilder\Options\WithExpiration;
1514
use YorCreative\UrlShortener\Builders\UrlBuilder\Options\WithOpenLimit;
1615
use YorCreative\UrlShortener\Builders\UrlBuilder\Options\WithOwnership;
@@ -42,12 +41,13 @@ public function __construct()
4241
$this->shortUrlCollection = new Collection();
4342
}
4443

45-
public static function shorten(string $plain_text): UrlBuilder
44+
public static function shorten(string $plain_text, ?string $domain = null): UrlBuilder
4645
{
4746
$b = self::$builder = new static;
4847

4948
$b->shortUrlCollection->put('plain_text', $url = $plain_text.$b->getDuplicateShortUrlQueryTag());
5049
$b->shortUrlCollection->put('hashed', md5($url));
50+
$b->shortUrlCollection->put('domain', $domain);
5151

5252
$b->options->add(new BaseOption());
5353

src/Repositories/UrlRepository.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ public static function create(array $ShortUrl): ShortUrl
100100
public static function updateShortUrl(string $identifier, string $domain, array $updates): ShortUrl
101101
{
102102
try {
103-
$ShortUrlRecord = self::findByDomainIdentifier($domain, $identifier);
104-
$ShortUrlRecord->update($updates);
103+
$shortUrlRecord = self::findByDomainIdentifier($domain, $identifier);
104+
$shortUrlRecord->update($updates);
105105

106-
return $ShortUrlRecord;
106+
return $shortUrlRecord;
107107
} catch (Exception $exception) {
108108
throw new UrlRepositoryException($exception->getMessage());
109109
}

src/Services/UrlService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public static function findByUtmCombination(array $utm_combination): Collection
5353
* @throws UrlRepositoryException
5454
* @throws UtilityServiceException
5555
*/
56-
public static function attempt(string $identifier, string $password): ?ShortUrl
56+
public static function attempt(string $identifier, ?string $domain, string $password): ?ShortUrl
5757
{
58-
if (! $shortUrl = UrlRepository::findByIdentifier($identifier)) {
58+
if (! $shortUrl = UrlRepository::findByDomainIdentifier($domain, $identifier)) {
5959
return null;
6060
}
6161

@@ -85,9 +85,9 @@ public static function attachOwnership($domain, $identifier, $type, $id): void
8585
}
8686
}
8787

88-
public static function shorten(string $plain_text): UrlBuilder
88+
public static function shorten(string $plain_text, ?string $domain = null): UrlBuilder
8989
{
90-
return UrlBuilder::shorten($plain_text);
90+
return UrlBuilder::shorten($plain_text, $domain);
9191
}
9292

9393
/**

tests/TestCase.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public function setUp(): void
3939
$migration->up();
4040
});
4141

42-
$this->base = 'localhost.test/v1/';
42+
$this->base = 'localhost.test';
4343
$this->plain_text = $this->getPlainText();
4444
$this->hashed = md5($this->plain_text);
4545

46-
$this->url = UrlService::shorten($this->plain_text)->withTracing([
46+
$this->url = UrlService::shorten($this->plain_text, 'localhost.test')->withTracing([
4747
TracingRepository::$ID => 'testing',
4848
TracingRepository::$CAMPAIGN => 'testing',
4949
TracingRepository::$SOURCE => 'testing',
@@ -52,9 +52,9 @@ public function setUp(): void
5252
TracingRepository::$TERM => 'testing',
5353
])->build();
5454

55-
$this->identifier = str_replace($this->base, '', $this->url);
55+
$this->identifier = str_replace($this->base.'/v1/', '', $this->url);
5656

57-
$this->shortUrl = UrlService::findByIdentifier($this->identifier);
57+
$this->shortUrl = UrlService::findByIdentifier($this->identifier, $this->base);
5858

5959
$this->request = Request::create('something-short.com/not-really');
6060
$this->changeRequestIp(

tests/Unit/Repositories/ClickRepositoryTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ public function it_can_find_a_click_by_its_id()
2121
{
2222
Config::set('location.testing.enabled', true);
2323

24+
Config::set('location.testing.ip', $ip = '66.102.0.0');
25+
2426
ClickService::track(
2527
$this->identifier,
26-
'0.0.0.0',
28+
$ip,
2729
ClickService::$SUCCESS_ROUTED
2830
);
2931

3032
$this->assertEquals(
31-
'0.0.0.0',
33+
$ip,
3234
ClickRepository::findById(1)->toArray()['location']['ip']
3335
);
3436
}

tests/Unit/Repositories/UrlRepositoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ public function it_can_update_a_short_url()
9696
{
9797
$this->assertNull($this->shortUrl->activation);
9898

99-
UrlRepository::updateShortUrl($this->identifier, [
99+
UrlRepository::updateShortUrl($this->identifier, $this->base, [
100100
'activation' => Carbon::now()->timestamp,
101101
]);
102102

103-
$shortUrl = UrlRepository::findByIdentifier($this->identifier);
103+
$shortUrl = UrlRepository::findByIdentifier($this->identifier, $this->base);
104104

105105
$this->assertNotNull($shortUrl->activation);
106106
}

tests/Unit/Services/ClickServiceTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class ClickServiceTest extends TestCase
2222
*/
2323
public function it_can_can_track_a_click()
2424
{
25-
$ip = '0.0.0.0';
26-
2725
Config::set('location.testing.enabled', true);
2826

27+
Config::set('location.testing.ip', $ip = '66.102.0.0');
28+
2929
ClickService::track(
3030
$this->identifier,
3131
$ip,
@@ -59,10 +59,10 @@ public function it_can_can_track_a_click()
5959
*/
6060
public function it_can_get_basic_scoped_clicks_for_short_url()
6161
{
62-
$ip = '0.0.0.0';
63-
6462
Config::set('location.testing.enabled', true);
6563

64+
Config::set('location.testing.ip', $ip = '66.102.0.0');
65+
6666
ClickService::track(
6767
$this->identifier,
6868
$ip,

tests/Unit/Services/UrlServiceTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,12 @@ public function it_can_successfully_attempt_to_verify_password()
120120
{
121121
$plain_text = 'something.com/really-long'.rand(5, 9999);
122122

123-
$url = UrlBuilder::shorten($plain_text)
123+
$url = UrlBuilder::shorten($plain_text, $this->base)
124124
->withPassword('password')
125125
->build();
126126

127-
$identifier = str_replace($this->base, '', $url);
128-
129-
$shortUrl = UrlService::attempt($identifier, 'password');
127+
$identifier = str_replace($this->base .'/v1/', '', $url);
128+
$shortUrl = UrlService::attempt($identifier, $this->base, 'password');
130129

131130
$this->assertTrue($plain_text == $shortUrl->plain_text);
132131
}
@@ -144,13 +143,13 @@ public function it_can_successfully_attempt_to_verify_password_and_fail()
144143
{
145144
$plain_text = 'something.com/really-long'.rand(5, 9999);
146145

147-
$url = UrlBuilder::shorten($plain_text)
146+
$url = UrlBuilder::shorten($plain_text, $domain = 'test.domain')
148147
->withPassword('password')
149148
->build();
150149

151150
$identifier = str_replace($this->base, '', $url);
152151

153-
$this->assertNull(UrlService::attempt($identifier, 'not_password'));
152+
$this->assertNull(UrlService::attempt($identifier, $domain,'not_password'));
154153
}
155154

156155
/**
@@ -173,7 +172,7 @@ public function it_can_attach_ownership_to_short_url()
173172
'ownerable_id' => $owner->$primary_key,
174173
];
175174

176-
UrlService::attachOwnership($this->identifier, $ownership['ownerable_type'], $ownership['ownerable_id']);
175+
UrlService::attachOwnership($this->base, $this->identifier, $ownership['ownerable_type'], $ownership['ownerable_id']);
177176

178177
$this->assertDatabaseHas(
179178
'short_url_ownerships',
@@ -192,7 +191,7 @@ public function it_can_attach_ownership_to_short_url()
192191
*/
193192
public function it_can_set_an_activation_time_successfully()
194193
{
195-
$shortUrl = UrlService::shorten('something')
194+
$shortUrl = UrlService::shorten('something', $domain = 'test.domain')
196195
->withActivation(Carbon::now()->addMinute()->timestamp)
197196
->build();
198197

0 commit comments

Comments
 (0)