Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests #8

Merged
merged 6 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand All @@ -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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.

69 changes: 69 additions & 0 deletions tests/PirschTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Pirsch\Facades\Pirsch;

it('can track a request', function () {
config(['pirsch.token' => 'test_token']);
Http::fake([
'https://api.pirsch.io/api/v1/hit' => Http::response(),
]);

Pirsch::track();
$this->get('/');

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([
'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;
});
});
48 changes: 48 additions & 0 deletions tests/TrackPageviewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use Illuminate\Support\Facades\Route;
use Pirsch\Facades\Pirsch;
use Pirsch\Http\Middleware\TrackPageview;

it('handles request', function () {
Pirsch::shouldReceive('track')
->once();

Route::middleware(TrackPageview::class)
->get('/', fn () => 'Hello World');

$this->get('/');
});

it('skips redirects', function () {
Pirsch::spy();

Route::middleware(TrackPageview::class)
->get('/', fn () => redirect('/home'));

$this->get('/');

Pirsch::shouldNotHaveBeenCalled();
});

it('skips Livewire', function () {
Pirsch::spy();

Route::middleware(TrackPageview::class)
->get('/', fn () => 'Hello World');

$this->get('/', ['X-Livewire' => 'true']);

Pirsch::shouldNotHaveBeenCalled();
});

it('skips Telescope', function () {
Pirsch::spy();

Route::middleware(TrackPageview::class)
->get('telescope/test', fn () => 'Hello World');

$this->get('/telescope/test');

Pirsch::shouldNotHaveBeenCalled();
});
Loading