Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Mar 5, 2024
1 parent 3eb87aa commit d8a68c0
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 8 deletions.
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 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) {
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;
});
});
53 changes: 53 additions & 0 deletions tests/TrackPageviewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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('/');

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

0 comments on commit d8a68c0

Please sign in to comment.