Skip to content

Commit

Permalink
Misc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
yordadev committed Jun 27, 2024
1 parent bd7459c commit c6c200d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.phpunit.result.cache
/.idea
/.vscode
.phpunit.cache/test-results
2 changes: 1 addition & 1 deletion src/Repositories/UrlRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static function findByIdentifier(string $identifier): ?ShortUrl
)->with(self::defaultWithRelationship())->first();
}

public static function findByDomainIdentifier(string $domain, string $identifier): ?ShortUrl
public static function findByDomainIdentifier(?string $domain, string $identifier): ?ShortUrl
{
return ShortUrl::where('identifier', $identifier)
->where('domain', $domain)
Expand Down
8 changes: 6 additions & 2 deletions src/Services/ClickService.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ public static function track(string $identifier, string $request_ip, int $outcom

try {
ClickRepository::createClick(
UrlRepository::findByDomainIdentifier($domain, $identifier)->id,
LocationRepository::findOrCreateLocationRecord(LocationRepository::getLocationFrom($request_ip))->id,
UrlRepository::findByIdentifier($identifier, $domain)->id,
LocationRepository::findOrCreateLocationRecord(
! config('location.testing.enabled')
? LocationRepository::getLocationFrom($request_ip)
: LocationRepository::locationUnknown($request_ip)
)->id,
$outcome_id
);
} catch (Exception $exception) {
Expand Down
4 changes: 2 additions & 2 deletions src/Services/UtilityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public static function constructRedirectHeaders(array $dynamic_headers = []): ar
{
return array_merge(
config('urlshortener.redirect.headers') ?? [
'Referer' => 'localhost:1337',
],
'Referer' => 'localhost:1337',
],
$dynamic_headers
);
}
Expand Down
7 changes: 5 additions & 2 deletions tests/Unit/Repositories/ClickRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace YorCreative\UrlShortener\Tests\Unit\Repositories;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Config;
use YorCreative\UrlShortener\Models\ShortUrl;
use YorCreative\UrlShortener\Repositories\ClickRepository;
use YorCreative\UrlShortener\Services\ClickService;
use YorCreative\UrlShortener\Tests\TestCase;
Expand All @@ -18,11 +20,12 @@ class ClickRepositoryTest extends TestCase
*/
public function it_can_find_a_click_by_its_id()
{
Config::set('location.testing.enabled', true);

ClickService::track(
$this->identifier,
'0.0.0.0',
ClickService::$SUCCESS_ROUTED,
true
ClickService::$SUCCESS_ROUTED
);

$this->assertEquals(
Expand Down
12 changes: 8 additions & 4 deletions tests/Unit/Services/ClickServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace YorCreative\UrlShortener\Tests\Unit\Services;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Config;
use Throwable;
use YorCreative\UrlShortener\Exceptions\FilterClicksStrategyException;
use YorCreative\UrlShortener\Exceptions\UrlRepositoryException;
Expand All @@ -14,6 +15,7 @@ class ClickServiceTest extends TestCase
{
use DatabaseTransactions;


/**
* @test
*
Expand All @@ -23,11 +25,12 @@ public function it_can_can_track_a_click()
{
$ip = '0.0.0.0';

Config::set('location.testing.enabled', true);

ClickService::track(
$this->identifier,
$ip,
ClickService::$SUCCESS_ROUTED,
true
ClickService::$SUCCESS_ROUTED
);

$this->assertDatabaseHas(
Expand Down Expand Up @@ -59,11 +62,12 @@ public function it_can_get_basic_scoped_clicks_for_short_url()
{
$ip = '0.0.0.0';

Config::set('location.testing.enabled', true);

ClickService::track(
$this->identifier,
$ip,
ClickService::$SUCCESS_ROUTED,
true
ClickService::$SUCCESS_ROUTED
);

$clicks = ClickService::get([
Expand Down

0 comments on commit c6c200d

Please sign in to comment.