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(); +});