This commit is contained in:
Stefanescu Mihai
2026-03-19 18:54:50 +02:00
parent 82ebb697f3
commit 49b1d3581a
19 changed files with 1496 additions and 11 deletions
+182
View File
@@ -0,0 +1,182 @@
<?php
namespace Tests\Feature;
use App\Jobs\ProcessTrackingPayload;
use App\Models\Website;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Tests\TestCase;
class CollectControllerTest extends TestCase
{
use RefreshDatabase;
private Website $website;
protected function setUp(): void
{
parent::setUp();
$this->website = Website::factory()->create(['domain' => 'example.com']);
}
private function validPayload(array $overrides = []): array
{
return array_merge([
'tid' => $this->website->tracking_id,
'vid' => 'visitor-abc-123',
'sid' => 'session-abc-123',
'type' => 'pageview',
'url' => 'https://example.com/hello',
'path' => '/hello',
'title' => 'Hello Page',
], $overrides);
}
// -- Store endpoint --
public function test_valid_pageview_payload_dispatches_job(): void
{
Bus::fake();
$response = $this->postJson('/api/collect', $this->validPayload());
$response->assertOk()->assertJson(['ok' => true]);
Bus::assertDispatched(ProcessTrackingPayload::class);
}
public function test_valid_event_payload_dispatches_job(): void
{
Bus::fake();
$response = $this->postJson('/api/collect', $this->validPayload([
'type' => 'event',
'event_name' => 'signup',
'event_category' => 'conversion',
]));
$response->assertOk()->assertJson(['ok' => true]);
Bus::assertDispatched(ProcessTrackingPayload::class);
}
public function test_missing_required_fields_returns_validation_error(): void
{
$response = $this->postJson('/api/collect', []);
$response->assertUnprocessable();
$response->assertJsonValidationErrors(['tid', 'vid', 'sid', 'type']);
}
public function test_invalid_type_returns_validation_error(): void
{
$response = $this->postJson('/api/collect', $this->validPayload([
'type' => 'invalid_type',
]));
$response->assertUnprocessable();
$response->assertJsonValidationErrors(['type']);
}
public function test_domain_mismatch_returns_error(): void
{
Bus::fake();
$response = $this->postJson('/api/collect', $this->validPayload([
'url' => 'https://evil-site.com/phishing',
]));
$response->assertStatus(400);
$response->assertJson(['ok' => false, 'reason' => 'domain_mismatch']);
Bus::assertNotDispatched(ProcessTrackingPayload::class);
}
public function test_subdomain_is_allowed(): void
{
Bus::fake();
$response = $this->postJson('/api/collect', $this->validPayload([
'url' => 'https://www.example.com/page',
]));
$response->assertOk()->assertJson(['ok' => true]);
Bus::assertDispatched(ProcessTrackingPayload::class);
}
public function test_unknown_tracking_id_still_succeeds(): void
{
Bus::fake();
$response = $this->postJson('/api/collect', $this->validPayload([
'tid' => 'TA-NONEXISTENT',
'url' => null,
]));
$response->assertOk()->assertJson(['ok' => true]);
Bus::assertDispatched(ProcessTrackingPayload::class);
}
public function test_bot_requests_are_rejected(): void
{
Bus::fake();
$response = $this->postJson('/api/collect', $this->validPayload(), [
'User-Agent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
]);
$response->assertOk()->assertJson(['ok' => false, 'reason' => 'bot']);
Bus::assertNotDispatched(ProcessTrackingPayload::class);
}
public function test_payload_with_utm_parameters(): void
{
Bus::fake();
$response = $this->postJson('/api/collect', $this->validPayload([
'utm_source' => 'google',
'utm_medium' => 'cpc',
'utm_campaign' => 'spring_sale',
'utm_term' => 'shoes',
'utm_content' => 'ad_v1',
]));
$response->assertOk()->assertJson(['ok' => true]);
Bus::assertDispatched(ProcessTrackingPayload::class);
}
// -- Pixel endpoint --
public function test_pixel_returns_gif(): void
{
Bus::fake();
$response = $this->get('/api/pixel?tid='.$this->website->tracking_id);
$response->assertOk();
$response->assertHeader('Content-Type', 'image/gif');
Bus::assertDispatched(ProcessTrackingPayload::class);
}
public function test_pixel_without_tid_returns_gif_without_dispatching(): void
{
Bus::fake();
$response = $this->get('/api/pixel');
$response->assertOk();
$response->assertHeader('Content-Type', 'image/gif');
Bus::assertNotDispatched(ProcessTrackingPayload::class);
}
public function test_pixel_rejects_bots(): void
{
Bus::fake();
$response = $this->get('/api/pixel?tid='.$this->website->tracking_id, [
'User-Agent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
]);
$response->assertOk();
$response->assertHeader('Content-Type', 'image/gif');
Bus::assertNotDispatched(ProcessTrackingPayload::class);
}
}
+204
View File
@@ -0,0 +1,204 @@
<?php
namespace Tests\Feature;
use App\Models\User;
use App\Models\Website;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class WebsiteControllerTest extends TestCase
{
use RefreshDatabase;
private User $user;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
}
// -- Index --
public function test_guests_cannot_access_website_list(): void
{
$this->get(route('websites.index'))->assertRedirect(route('login'));
}
public function test_authenticated_user_can_see_their_websites(): void
{
$website = Website::factory()->for($this->user)->create();
Website::factory()->create(); // another user's website
$response = $this->actingAs($this->user)->get(route('websites.index'));
$response->assertOk();
$response->assertSeeText($website->name);
}
// -- Create / Store --
public function test_user_can_view_create_form(): void
{
$this->actingAs($this->user)
->get(route('websites.create'))
->assertOk();
}
public function test_user_can_create_website(): void
{
$response = $this->actingAs($this->user)->post(route('websites.store'), [
'name' => 'My Site',
'domain' => 'mysite.com',
]);
$response->assertRedirect(route('websites.index'));
$this->assertDatabaseHas('websites', [
'user_id' => $this->user->id,
'name' => 'My Site',
'domain' => 'mysite.com',
]);
}
public function test_create_website_generates_tracking_id(): void
{
$this->actingAs($this->user)->post(route('websites.store'), [
'name' => 'My Site',
'domain' => 'mysite.com',
]);
$website = Website::where('domain', 'mysite.com')->first();
$this->assertNotNull($website->tracking_id);
$this->assertStringStartsWith('TA-', $website->tracking_id);
}
public function test_create_website_validates_domain_format(): void
{
$response = $this->actingAs($this->user)->post(route('websites.store'), [
'name' => 'My Site',
'domain' => 'not a domain!!',
]);
$response->assertSessionHasErrors('domain');
}
public function test_create_website_requires_name(): void
{
$response = $this->actingAs($this->user)->post(route('websites.store'), [
'name' => '',
'domain' => 'mysite.com',
]);
$response->assertSessionHasErrors('name');
}
// -- Edit / Update --
public function test_owner_can_edit_website(): void
{
$website = Website::factory()->for($this->user)->create();
$this->actingAs($this->user)
->get(route('websites.edit', $website))
->assertOk();
}
public function test_non_owner_cannot_edit_website(): void
{
$otherUser = User::factory()->create();
$website = Website::factory()->for($otherUser)->create();
$this->actingAs($this->user)
->get(route('websites.edit', $website))
->assertForbidden();
}
public function test_owner_can_update_website(): void
{
$website = Website::factory()->for($this->user)->create();
$response = $this->actingAs($this->user)->put(route('websites.update', $website), [
'name' => 'Updated Name',
'domain' => 'updated.com',
]);
$response->assertRedirect(route('dashboard.show', $website));
$this->assertDatabaseHas('websites', [
'id' => $website->id,
'name' => 'Updated Name',
'domain' => 'updated.com',
]);
}
public function test_non_owner_cannot_update_website(): void
{
$otherUser = User::factory()->create();
$website = Website::factory()->for($otherUser)->create();
$this->actingAs($this->user)
->put(route('websites.update', $website), [
'name' => 'Hacked',
'domain' => 'hacked.com',
])
->assertForbidden();
}
// -- Delete --
public function test_owner_can_delete_website(): void
{
$website = Website::factory()->for($this->user)->create();
$response = $this->actingAs($this->user)
->delete(route('websites.destroy', $website));
$response->assertRedirect(route('websites.index'));
$this->assertDatabaseMissing('websites', ['id' => $website->id]);
}
public function test_non_owner_cannot_delete_website(): void
{
$otherUser = User::factory()->create();
$website = Website::factory()->for($otherUser)->create();
$this->actingAs($this->user)
->delete(route('websites.destroy', $website))
->assertForbidden();
$this->assertDatabaseHas('websites', ['id' => $website->id]);
}
// -- Show redirects to dashboard --
public function test_show_redirects_to_dashboard(): void
{
$website = Website::factory()->for($this->user)->create();
$this->actingAs($this->user)
->get(route('websites.show', $website))
->assertRedirect(route('dashboard.show', $website));
}
// -- Snippet --
public function test_owner_can_view_snippet(): void
{
$website = Website::factory()->for($this->user)->create();
$this->actingAs($this->user)
->get(route('websites.snippet', $website))
->assertOk()
->assertSeeText($website->tracking_id);
}
public function test_non_owner_cannot_view_snippet(): void
{
$otherUser = User::factory()->create();
$website = Website::factory()->for($otherUser)->create();
$this->actingAs($this->user)
->get(route('websites.snippet', $website))
->assertForbidden();
}
}
+285
View File
@@ -0,0 +1,285 @@
<?php
namespace Tests\Unit;
use App\Models\AnalyticsSession;
use App\Models\DailyAggregate;
use App\Models\PageView;
use App\Models\Visitor;
use App\Models\Website;
use App\Services\AnalyticsQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;
class AnalyticsQueryServiceTest extends TestCase
{
use RefreshDatabase;
private Website $website;
protected function setUp(): void
{
parent::setUp();
$this->website = Website::factory()->create();
}
private function makeService(?Carbon $from = null, ?Carbon $to = null): AnalyticsQueryService
{
return AnalyticsQueryService::for(
$this->website,
$from ?? Carbon::today()->subDays(6),
$to ?? Carbon::today(),
);
}
private function createVisitorWithPageViews(int $pvCount, ?Carbon $viewedAt = null): void
{
$visitor = Visitor::factory()->for($this->website)->create();
$session = AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor->id,
'started_at' => $viewedAt ?? now(),
'ended_at' => $viewedAt ?? now(),
'duration' => 60,
'pages_count' => $pvCount,
'is_bounce' => $pvCount <= 1,
]);
for ($i = 0; $i < $pvCount; $i++) {
PageView::factory()->for($this->website)->create([
'visitor_id' => $visitor->id,
'analytics_session_id' => $session->id,
'viewed_at' => $viewedAt ?? now(),
'path' => '/page-'.$i,
'time_on_page' => 30,
]);
}
}
// -- Summary metrics from raw data --
public function test_total_page_views_counts_correctly(): void
{
$this->createVisitorWithPageViews(3);
$this->createVisitorWithPageViews(2);
$service = $this->makeService();
$this->assertEquals(5, $service->totalPageViews());
}
public function test_unique_visitors_counts_distinct(): void
{
$this->createVisitorWithPageViews(2);
$this->createVisitorWithPageViews(1);
$service = $this->makeService();
$this->assertEquals(2, $service->uniqueVisitors());
}
public function test_total_sessions(): void
{
$this->createVisitorWithPageViews(2);
$this->createVisitorWithPageViews(1);
$service = $this->makeService();
$this->assertEquals(2, $service->totalSessions());
}
public function test_bounce_rate_calculation(): void
{
// Non-bounce session
$this->createVisitorWithPageViews(3);
// Bounce session
$visitor = Visitor::factory()->for($this->website)->create();
AnalyticsSession::factory()->for($this->website)->bounce()->create([
'visitor_id' => $visitor->id,
'started_at' => now(),
]);
$service = $this->makeService();
$this->assertEquals(50.0, $service->bounceRate());
}
public function test_bounce_rate_zero_when_no_sessions(): void
{
$service = $this->makeService();
$this->assertEquals(0, $service->bounceRate());
}
public function test_avg_session_duration(): void
{
$visitor1 = Visitor::factory()->for($this->website)->create();
AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor1->id,
'duration' => 100,
'started_at' => now(),
]);
$visitor2 = Visitor::factory()->for($this->website)->create();
AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor2->id,
'duration' => 200,
'started_at' => now(),
]);
$service = $this->makeService();
$this->assertEquals(150, $service->avgSessionDuration());
}
public function test_summary_returns_all_keys(): void
{
$service = $this->makeService();
$summary = $service->summary();
$this->assertArrayHasKey('page_views', $summary);
$this->assertArrayHasKey('visitors', $summary);
$this->assertArrayHasKey('sessions', $summary);
$this->assertArrayHasKey('bounce_rate', $summary);
$this->assertArrayHasKey('avg_duration', $summary);
$this->assertArrayHasKey('avg_pages', $summary);
}
// -- Date range filtering --
public function test_only_counts_data_within_date_range(): void
{
$this->createVisitorWithPageViews(2, Carbon::today());
$this->createVisitorWithPageViews(1, Carbon::today()->subDays(30));
$service = $this->makeService(Carbon::today(), Carbon::today());
$this->assertEquals(2, $service->totalPageViews());
$this->assertEquals(1, $service->uniqueVisitors());
}
// -- Aggregate data --
public function test_reads_from_aggregate_tables_for_past_days(): void
{
DailyAggregate::create([
'website_id' => $this->website->id,
'date' => Carbon::yesterday()->toDateString(),
'visitors' => 50,
'sessions' => 80,
'page_views' => 200,
'bounce_count' => 20,
'total_duration' => 4000,
]);
$service = $this->makeService(
Carbon::yesterday(),
Carbon::yesterday(),
);
$this->assertEquals(200, $service->totalPageViews());
$this->assertEquals(50, $service->uniqueVisitors());
$this->assertEquals(80, $service->totalSessions());
}
// -- Top pages --
public function test_top_pages_returns_ordered_results(): void
{
$visitor = Visitor::factory()->for($this->website)->create();
$session = AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor->id,
'started_at' => now(),
]);
PageView::factory()->for($this->website)->count(3)->create([
'visitor_id' => $visitor->id,
'analytics_session_id' => $session->id,
'path' => '/popular',
'viewed_at' => now(),
]);
PageView::factory()->for($this->website)->create([
'visitor_id' => $visitor->id,
'analytics_session_id' => $session->id,
'path' => '/less-popular',
'viewed_at' => now(),
]);
$service = $this->makeService();
$pages = $service->topPages(10);
$this->assertGreaterThanOrEqual(2, $pages->count());
$this->assertEquals('/popular', $pages->first()->path);
}
// -- Visitors over time --
public function test_visitors_over_time_returns_collection(): void
{
$this->createVisitorWithPageViews(1);
$service = $this->makeService();
$timeseries = $service->visitorsOverTime();
$this->assertNotEmpty($timeseries);
$first = $timeseries->first();
$this->assertArrayHasKey('period', $first);
$this->assertArrayHasKey('visitors', $first);
$this->assertArrayHasKey('page_views', $first);
$this->assertArrayHasKey('sessions', $first);
}
// -- Traffic sources --
public function test_traffic_sources_groups_by_referrer(): void
{
$visitor = Visitor::factory()->for($this->website)->create();
AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor->id,
'referrer_domain' => 'google.com',
'started_at' => now(),
]);
AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor->id,
'referrer_domain' => '',
'started_at' => now(),
]);
$service = $this->makeService();
$sources = $service->trafficSources();
$this->assertGreaterThanOrEqual(1, $sources->count());
}
// -- Segment filtering --
public function test_segment_filters_data_by_visitor_ids(): void
{
$visitor1 = Visitor::factory()->for($this->website)->create(['country' => 'DE']);
$visitor2 = Visitor::factory()->for($this->website)->create(['country' => 'US']);
$session1 = AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor1->id,
'started_at' => now(),
]);
$session2 = AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor2->id,
'started_at' => now(),
]);
PageView::factory()->for($this->website)->count(3)->create([
'visitor_id' => $visitor1->id,
'analytics_session_id' => $session1->id,
'viewed_at' => now(),
]);
PageView::factory()->for($this->website)->count(2)->create([
'visitor_id' => $visitor2->id,
'analytics_session_id' => $session2->id,
'viewed_at' => now(),
]);
$service = $this->makeService();
$service->withSegment(collect([$visitor1->id]));
$this->assertEquals(3, $service->totalPageViews());
$this->assertEquals(1, $service->uniqueVisitors());
$this->assertEquals(1, $service->totalSessions());
}
}
+284
View File
@@ -0,0 +1,284 @@
<?php
namespace Tests\Unit;
use App\Models\AnalyticsSession;
use App\Models\Goal;
use App\Models\GoalConversion;
use App\Models\Visitor;
use App\Models\Website;
use App\Services\GoalEvaluationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;
class GoalEvaluationServiceTest extends TestCase
{
use RefreshDatabase;
private GoalEvaluationService $service;
private Website $website;
private Visitor $visitor;
private AnalyticsSession $session;
protected function setUp(): void
{
parent::setUp();
$this->service = new GoalEvaluationService;
$this->website = Website::factory()->create();
$this->visitor = Visitor::factory()->for($this->website)->create();
$this->session = AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $this->visitor->id,
'duration' => 120,
'pages_count' => 5,
]);
}
// -- URL Destination --
public function test_url_destination_exact_match(): void
{
$goal = Goal::factory()->for($this->website)->urlDestination('/thank-you')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/thank-you',
], Carbon::now());
$this->assertDatabaseHas('goal_conversions', [
'goal_id' => $goal->id,
'visitor_id' => $this->visitor->id,
]);
}
public function test_url_destination_exact_mismatch(): void
{
Goal::factory()->for($this->website)->urlDestination('/thank-you')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/other-page',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 0);
}
public function test_url_destination_starts_with(): void
{
$goal = Goal::factory()->for($this->website)->urlDestination('/checkout', 'starts_with')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/checkout/step-2',
], Carbon::now());
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
}
public function test_url_destination_contains(): void
{
$goal = Goal::factory()->for($this->website)->urlDestination('success', 'contains')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/order/success/123',
], Carbon::now());
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
}
public function test_url_destination_regex(): void
{
$goal = Goal::factory()->for($this->website)->urlDestination('/^\\/order\\/\\d+$/', 'regex')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/order/42',
], Carbon::now());
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
}
// -- Event Match --
public function test_event_match_by_name(): void
{
$goal = Goal::factory()->for($this->website)->eventMatch('purchase')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'event',
'event_name' => 'purchase',
], Carbon::now());
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
}
public function test_event_match_wrong_name_does_not_convert(): void
{
Goal::factory()->for($this->website)->eventMatch('purchase')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'event',
'event_name' => 'signup',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 0);
}
public function test_event_match_with_category(): void
{
$goal = Goal::factory()->for($this->website)->eventMatch('click', 'cta')->create();
// wrong category
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'event',
'event_name' => 'click',
'event_category' => 'navigation',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 0);
// right category
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'event',
'event_name' => 'click',
'event_category' => 'cta',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 1);
}
public function test_event_match_requires_event_type(): void
{
Goal::factory()->for($this->website)->eventMatch('purchase')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'event_name' => 'purchase',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 0);
}
// -- Duration --
public function test_duration_goal_converts_when_threshold_met(): void
{
$goal = Goal::factory()->for($this->website)->duration(60)->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/',
], Carbon::now());
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
}
public function test_duration_goal_does_not_convert_below_threshold(): void
{
Goal::factory()->for($this->website)->duration(300)->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 0);
}
// -- Pages per session --
public function test_pages_per_session_goal_converts(): void
{
$goal = Goal::factory()->for($this->website)->pagesPerSession(3)->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/',
], Carbon::now());
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
}
public function test_pages_per_session_goal_does_not_convert_below_threshold(): void
{
Goal::factory()->for($this->website)->pagesPerSession(10)->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 0);
}
// -- De-duplication --
public function test_does_not_duplicate_conversion_within_same_session(): void
{
$goal = Goal::factory()->for($this->website)->urlDestination('/thank-you')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/thank-you',
], Carbon::now());
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/thank-you',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 1);
}
// -- Inactive goals --
public function test_inactive_goals_are_skipped(): void
{
Goal::factory()->for($this->website)->urlDestination('/thank-you')->inactive()->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/thank-you',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 0);
}
// -- Conversion value --
public function test_conversion_stores_goal_default_value(): void
{
$goal = Goal::factory()->for($this->website)->urlDestination('/buy')->create([
'default_value' => 49.99,
]);
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/buy',
], Carbon::now());
$conversion = GoalConversion::where('goal_id', $goal->id)->first();
$this->assertNotNull($conversion);
$this->assertEquals(49.99, (float) $conversion->value);
}
// -- Multiple goals --
public function test_evaluates_multiple_goals_independently(): void
{
$goal1 = Goal::factory()->for($this->website)->urlDestination('/thank-you')->create();
$goal2 = Goal::factory()->for($this->website)->duration(60)->create();
Goal::factory()->for($this->website)->urlDestination('/other-page')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/thank-you',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 2);
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal1->id]);
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal2->id]);
}
}