diff --git a/.gitignore b/.gitignore index 0bb5f94..9e317c3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ .phpunit.result.cache /.idea /.vscode +.phpunit.cache/test-results diff --git a/src/Repositories/UrlRepository.php b/src/Repositories/UrlRepository.php index 8f236e1..f8ca893 100644 --- a/src/Repositories/UrlRepository.php +++ b/src/Repositories/UrlRepository.php @@ -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) diff --git a/src/Services/ClickService.php b/src/Services/ClickService.php index 7edd08a..510e2fd 100644 --- a/src/Services/ClickService.php +++ b/src/Services/ClickService.php @@ -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) { diff --git a/src/Services/UtilityService.php b/src/Services/UtilityService.php index a31286c..8e896a4 100644 --- a/src/Services/UtilityService.php +++ b/src/Services/UtilityService.php @@ -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 ); } diff --git a/tests/Unit/Repositories/ClickRepositoryTest.php b/tests/Unit/Repositories/ClickRepositoryTest.php index 44966b3..2bc7dcf 100644 --- a/tests/Unit/Repositories/ClickRepositoryTest.php +++ b/tests/Unit/Repositories/ClickRepositoryTest.php @@ -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; @@ -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( diff --git a/tests/Unit/Services/ClickServiceTest.php b/tests/Unit/Services/ClickServiceTest.php index a621d89..84f56d5 100644 --- a/tests/Unit/Services/ClickServiceTest.php +++ b/tests/Unit/Services/ClickServiceTest.php @@ -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; @@ -14,6 +15,7 @@ class ClickServiceTest extends TestCase { use DatabaseTransactions; + /** * @test * @@ -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( @@ -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([