From d8a68c0f0025e7b72a84adee0264685ba04300db Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Tue, 5 Mar 2024 23:01:46 +0100 Subject: [PATCH 1/6] Add tests --- .github/workflows/run-tests.yml | 5 ++- composer.json | 3 +- tests/ExampleTest.php | 5 --- tests/PirschTest.php | 69 +++++++++++++++++++++++++++++++++ tests/TrackPageviewTest.php | 53 +++++++++++++++++++++++++ 5 files changed, 127 insertions(+), 8 deletions(-) delete mode 100644 tests/ExampleTest.php create mode 100644 tests/PirschTest.php create mode 100644 tests/TrackPageviewTest.php diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 86842c5..9e4f64f 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -33,7 +33,8 @@ jobs: with: php-version: ${{ matrix.php }} extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo - coverage: none + coverage: pcov + ini-values: pcov.directory=., pcov.exclude="~vendor~" - name: Setup problem matchers run: | @@ -46,4 +47,4 @@ jobs: composer update --${{ matrix.stability }} --prefer-dist --no-interaction - name: Execute tests - run: vendor/bin/pest + run: vendor/bin/pest --coverage diff --git a/composer.json b/composer.json index 652556b..8f6a103 100644 --- a/composer.json +++ b/composer.json @@ -27,9 +27,10 @@ "spatie/laravel-package-tools": "^1.9.2" }, "require-dev": { + "guzzlehttp/guzzle": "^7.8", + "larastan/larastan": "^2.0.1", "laravel/pint": "^0.2.2", "nunomaduro/collision": "^6.0|^7.0|^8.0", - "larastan/larastan": "^2.0.1", "orchestra/testbench": "^7.0|^8.0|^9.0", "pestphp/pest-plugin-laravel": "^1.1|^2.0", "phpstan/extension-installer": "^1.1", diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php deleted file mode 100644 index 5d36321..0000000 --- a/tests/ExampleTest.php +++ /dev/null @@ -1,5 +0,0 @@ -toBeTrue(); -}); diff --git a/tests/PirschTest.php b/tests/PirschTest.php new file mode 100644 index 0000000..e530708 --- /dev/null +++ b/tests/PirschTest.php @@ -0,0 +1,69 @@ + 'test_token']); + Http::fake([ + 'https://api.pirsch.io/api/v1/hit' => Http::response(), + ]); + + Pirsch::track(); + $this->get('/'); + + Http::assertSent(function (Request $request) { + expect($request->url())->toBe('https://api.pirsch.io/api/v1/hit'); + expect($request->hasHeader('Authorization', 'Bearer test_token'))->toBeTrue(); + expect($request->data())->toBe([ + 'url' => 'http://localhost', + 'ip' => '127.0.0.1', + 'user_agent' => 'Symfony', + 'accept_language' => 'en-us,en;q=0.5', + 'sec_ch_ua' => null, + 'sec_ch_ua_mobile' => null, + 'sec_ch_ua_platform' => null, + 'sec_ch_ua_platform_version' => null, + 'sec_ch_width' => null, + 'sec_ch_viewport_width' => null, + 'referrer' => null, + ]); + + return true; + }); +}); + +it('can send an event', function () { + config(['pirsch.token' => 'test_token']); + Http::fake([ + 'https://api.pirsch.io/api/v1/event' => Http::response(), + ]); + + Pirsch::track('name', ['meta' => 'data']); + $this->get('/'); + + Http::assertSent(function (Request $request) { + expect($request->url())->toBe('https://api.pirsch.io/api/v1/event'); + expect($request->hasHeader('Authorization', 'Bearer test_token'))->toBeTrue(); + expect($request->data())->toBe([ + 'url' => 'http://localhost', + 'ip' => '127.0.0.1', + 'user_agent' => 'Symfony', + 'accept_language' => 'en-us,en;q=0.5', + 'sec_ch_ua' => null, + 'sec_ch_ua_mobile' => null, + 'sec_ch_ua_platform' => null, + 'sec_ch_ua_platform_version' => null, + 'sec_ch_width' => null, + 'sec_ch_viewport_width' => null, + 'referrer' => null, + 'event_name' => 'name', + 'event_meta' => [ + 'meta' => 'data', + ], + ]); + + return true; + }); +}); diff --git a/tests/TrackPageviewTest.php b/tests/TrackPageviewTest.php new file mode 100644 index 0000000..1c2c97d --- /dev/null +++ b/tests/TrackPageviewTest.php @@ -0,0 +1,53 @@ +once(); + + Route::middleware(TrackPageview::class) + ->get('/', fn () => 'Hello World'); + + $this->get('/'); + + expect(true)->toBeTrue(); +}); + +it('skips redirects', function () { + Pirsch::spy(); + + Route::middleware(TrackPageview::class) + ->get('/', fn () => redirect('/home')); + + $this->get('/'); + + Pirsch::shouldNotHaveBeenCalled(); + expect(true)->toBeTrue(); +}); + +it('skips livewire', function () { + Pirsch::spy(); + + Route::middleware(TrackPageview::class) + ->get('/', fn () => 'Hello World'); + + $this->get('/', ['X-Livewire' => 'true']); + + Pirsch::shouldNotHaveBeenCalled(); + expect(true)->toBeTrue(); +}); + +it('skips telescope', function () { + Pirsch::spy(); + + Route::middleware(TrackPageview::class) + ->get('telescope/test', fn () => 'Hello World'); + + $this->get('/telescope/test'); + + Pirsch::shouldNotHaveBeenCalled(); + expect(true)->toBeTrue(); +}); From 3599ff9b05156a9338510705793f16192317ef24 Mon Sep 17 00:00:00 2001 From: Zep Fietje Date: Thu, 7 Mar 2024 20:19:17 +0100 Subject: [PATCH 2/6] Update tests/PirschTest.php --- tests/PirschTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PirschTest.php b/tests/PirschTest.php index e530708..d9f469a 100644 --- a/tests/PirschTest.php +++ b/tests/PirschTest.php @@ -13,7 +13,7 @@ Pirsch::track(); $this->get('/'); - Http::assertSent(function (Request $request) { + Http::assertSent(function (Request $request): bool { expect($request->url())->toBe('https://api.pirsch.io/api/v1/hit'); expect($request->hasHeader('Authorization', 'Bearer test_token'))->toBeTrue(); expect($request->data())->toBe([ From f59474ec27e21ba980aad4c7e37a773591309085 Mon Sep 17 00:00:00 2001 From: Zep Fietje Date: Thu, 7 Mar 2024 20:19:55 +0100 Subject: [PATCH 3/6] Update tests/PirschTest.php --- tests/PirschTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PirschTest.php b/tests/PirschTest.php index d9f469a..c43a453 100644 --- a/tests/PirschTest.php +++ b/tests/PirschTest.php @@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Http; use Pirsch\Facades\Pirsch; -it('can track request', function () { +it('can track a request', function () { config(['pirsch.token' => 'test_token']); Http::fake([ 'https://api.pirsch.io/api/v1/hit' => Http::response(), From 38f6b663a5f0c89676141790f16d5c7bc330b3db Mon Sep 17 00:00:00 2001 From: Zep Fietje Date: Thu, 7 Mar 2024 20:22:14 +0100 Subject: [PATCH 4/6] Update tests/TrackPageviewTest.php --- tests/TrackPageviewTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TrackPageviewTest.php b/tests/TrackPageviewTest.php index 1c2c97d..03ab91b 100644 --- a/tests/TrackPageviewTest.php +++ b/tests/TrackPageviewTest.php @@ -28,7 +28,7 @@ expect(true)->toBeTrue(); }); -it('skips livewire', function () { +it('skips Livewire', function () { Pirsch::spy(); Route::middleware(TrackPageview::class) From 221d918b8a0eaae61a03622fdda07fce5e1f2af0 Mon Sep 17 00:00:00 2001 From: Zep Fietje Date: Thu, 7 Mar 2024 20:22:27 +0100 Subject: [PATCH 5/6] Update tests/TrackPageviewTest.php --- tests/TrackPageviewTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TrackPageviewTest.php b/tests/TrackPageviewTest.php index 03ab91b..caa8c57 100644 --- a/tests/TrackPageviewTest.php +++ b/tests/TrackPageviewTest.php @@ -40,7 +40,7 @@ expect(true)->toBeTrue(); }); -it('skips telescope', function () { +it('skips Telescope', function () { Pirsch::spy(); Route::middleware(TrackPageview::class) From 909a41556cbf64925be8864afdd27585f9897e02 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Fri, 8 Mar 2024 14:41:43 +0100 Subject: [PATCH 6/6] fix --- tests/TrackPageviewTest.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/TrackPageviewTest.php b/tests/TrackPageviewTest.php index caa8c57..317ba74 100644 --- a/tests/TrackPageviewTest.php +++ b/tests/TrackPageviewTest.php @@ -12,8 +12,6 @@ ->get('/', fn () => 'Hello World'); $this->get('/'); - - expect(true)->toBeTrue(); }); it('skips redirects', function () { @@ -25,7 +23,6 @@ $this->get('/'); Pirsch::shouldNotHaveBeenCalled(); - expect(true)->toBeTrue(); }); it('skips Livewire', function () { @@ -37,7 +34,6 @@ $this->get('/', ['X-Livewire' => 'true']); Pirsch::shouldNotHaveBeenCalled(); - expect(true)->toBeTrue(); }); it('skips Telescope', function () { @@ -49,5 +45,4 @@ $this->get('/telescope/test'); Pirsch::shouldNotHaveBeenCalled(); - expect(true)->toBeTrue(); });