Skip to content

Commit

Permalink
Merge pull request #224 from liberu-crm/sweep/Enhance-Analytics-Dashb…
Browse files Browse the repository at this point in the history
…oard-and-Calendar-Integration-Tests

Enhance Analytics Dashboard and Calendar Integration Tests
  • Loading branch information
curtisdelicata authored Oct 16, 2024
2 parents c69e728 + 31317da commit d79da5f
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 17 deletions.
85 changes: 79 additions & 6 deletions tests/Feature/AnalyticsDashboardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@
namespace Tests\Feature;

use App\Models\User;
use App\Models\Contact;
use App\Models\Lead;
use App\Models\Deal;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AnalyticsDashboardTest extends TestCase
{
use RefreshDatabase;

public function testAnalyticsDashboardAccess()
protected $user;

protected function setUp(): void
{
$user = User::factory()->create();
parent::setUp();
$this->user = User::factory()->create();
}

$response = $this->actingAs($user)
public function testAnalyticsDashboardAccess()
{
$response = $this->actingAs($this->user)
->get('/analytics-dashboard');

$response->assertStatus(200);
Expand All @@ -23,14 +32,78 @@ public function testAnalyticsDashboardAccess()

public function testAnalyticsDashboardContainsRequiredComponents()
{
$user = User::factory()->create();

$response = $this->actingAs($user)
$response = $this->actingAs($this->user)
->get('/analytics-dashboard');

$response->assertStatus(200);
$response->assertSee('Contact Stats Overview');
$response->assertSee('Sales Pipeline Chart');
$response->assertSee('Customer Engagement Chart');
}

public function testContactStatsOverview()
{
Contact::factory()->count(5)->create();
Lead::factory()->count(3)->create();

$response = $this->actingAs($this->user)
->get('/analytics-dashboard');

$response->assertStatus(200);
$response->assertSee('Total Contacts: 5');
$response->assertSee('Total Leads: 3');
}

public function testSalesPipelineChart()
{
$dealStages = ['Prospecting', 'Qualification', 'Proposal', 'Negotiation', 'Closed Won', 'Closed Lost'];
foreach ($dealStages as $stage) {
Deal::factory()->count(rand(1, 5))->create(['stage' => $stage]);
}

$response = $this->actingAs($this->user)
->get('/analytics-dashboard');

$response->assertStatus(200);
foreach ($dealStages as $stage) {
$response->assertSee($stage);
}
}

public function testCustomerEngagementChart()
{
$engagementTypes = ['Email', 'Phone', 'Meeting', 'Social Media'];
foreach ($engagementTypes as $type) {
Contact::factory()->count(rand(1, 5))->create(['last_engagement_type' => $type]);
}

$response = $this->actingAs($this->user)
->get('/analytics-dashboard');

$response->assertStatus(200);
foreach ($engagementTypes as $type) {
$response->assertSee($type);
}
}

public function testDataRetrievalForCharts()
{
Deal::factory()->count(10)->create();
Contact::factory()->count(20)->create();

$response = $this->actingAs($this->user)
->get('/analytics-dashboard');

$response->assertStatus(200);
$response->assertViewHas('salesPipelineData');
$response->assertViewHas('customerEngagementData');

$salesPipelineData = $response->viewData('salesPipelineData');
$customerEngagementData = $response->viewData('customerEngagementData');

$this->assertIsArray($salesPipelineData);
$this->assertIsArray($customerEngagementData);
$this->assertNotEmpty($salesPipelineData);
$this->assertNotEmpty($customerEngagementData);
}
}
90 changes: 81 additions & 9 deletions tests/Feature/CalendarIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testCreateTaskAndSyncWithGoogleCalendar()
$user = User::factory()->create();
$this->actingAs($user);

$this->mockGoogleCalendarService->shouldReceive('createEvent')->once()->andReturn(true);
$this->mockGoogleCalendarService->shouldReceive('createEvent')->once()->andReturn('google_event_id');

$response = $this->post('/tasks', [
'name' => 'Test Google Task',
Expand All @@ -43,6 +43,7 @@ public function testCreateTaskAndSyncWithGoogleCalendar()
$this->assertDatabaseHas('tasks', [
'name' => 'Test Google Task',
'calendar_type' => 'google',
'calendar_event_id' => 'google_event_id',
]);
}

Expand All @@ -51,7 +52,7 @@ public function testCreateTaskAndSyncWithOutlookCalendar()
$user = User::factory()->create();
$this->actingAs($user);

$this->mockOutlookCalendarService->shouldReceive('createEvent')->once()->andReturn(true);
$this->mockOutlookCalendarService->shouldReceive('createEvent')->once()->andReturn('outlook_event_id');

$response = $this->post('/tasks', [
'name' => 'Test Outlook Task',
Expand All @@ -64,21 +65,22 @@ public function testCreateTaskAndSyncWithOutlookCalendar()
$this->assertDatabaseHas('tasks', [
'name' => 'Test Outlook Task',
'calendar_type' => 'outlook',
'calendar_event_id' => 'outlook_event_id',
]);
}

public function testUpdateTaskAndSyncWithCalendar()
public function testUpdateTaskAndSyncWithGoogleCalendar()
{
$user = User::factory()->create();
$this->actingAs($user);

$task = Task::factory()->create([
'user_id' => $user->id,

'calendar_type' => 'google',
'calendar_event_id' => 'existing_google_event_id',
]);

$this->mockGoogleCalendarService->shouldReceive('updateEvent')->once()->andReturn(true);
$this->mockGoogleCalendarService->shouldReceive('updateEvent')->once()->with('existing_google_event_id', Mockery::any())->andReturn(true);

$response = $this->patch("/tasks/{$task->id}", [
'name' => 'Updated Google Task',
Expand All @@ -91,20 +93,69 @@ public function testUpdateTaskAndSyncWithCalendar()
'id' => $task->id,
'name' => 'Updated Google Task',
'calendar_type' => 'google',
'calendar_event_id' => 'existing_google_event_id',
]);
}

public function testUpdateTaskAndSyncWithOutlookCalendar()
{
$user = User::factory()->create();
$this->actingAs($user);

$task = Task::factory()->create([
'user_id' => $user->id,
'calendar_type' => 'outlook',
'calendar_event_id' => 'existing_outlook_event_id',
]);

$this->mockOutlookCalendarService->shouldReceive('updateEvent')->once()->with('existing_outlook_event_id', Mockery::any())->andReturn(true);

$response = $this->patch("/tasks/{$task->id}", [
'name' => 'Updated Outlook Task',
'description' => 'Updated Description',
'due_date' => now()->addDays(3),
]);

$response->assertSessionHasNoErrors();
$this->assertDatabaseHas('tasks', [
'id' => $task->id,
'name' => 'Updated Outlook Task',
'calendar_type' => 'outlook',
'calendar_event_id' => 'existing_outlook_event_id',
]);
}

public function testDeleteTaskAndRemoveFromGoogleCalendar()
{
$user = User::factory()->create();
$this->actingAs($user);

$task = Task::factory()->create([
'user_id' => $user->id,
'calendar_type' => 'google',
'calendar_event_id' => 'google_event_id_to_delete',
]);

$this->mockGoogleCalendarService->shouldReceive('deleteEvent')->once()->with('google_event_id_to_delete')->andReturn(true);

$response = $this->delete("/tasks/{$task->id}");

$response->assertSessionHasNoErrors();
$this->assertDatabaseMissing('tasks', ['id' => $task->id]);
}

public function testDeleteTaskAndRemoveFromCalendar()
public function testDeleteTaskAndRemoveFromOutlookCalendar()
{
$user = User::factory()->create();
$this->actingAs($user);

$task = Task::factory()->create([
'user_id' => $user->id,
'calendar_type' => 'outlook',
'calendar_event_id' => 'outlook_event_id_to_delete',
]);

$this->mockOutlookCalendarService->shouldReceive('deleteEvent')->once()->andReturn(true);
$this->mockOutlookCalendarService->shouldReceive('deleteEvent')->once()->with('outlook_event_id_to_delete')->andReturn(true);

$response = $this->delete("/tasks/{$task->id}");

Expand All @@ -120,10 +171,11 @@ public function testSwitchCalendarType()
$task = Task::factory()->create([
'user_id' => $user->id,
'calendar_type' => 'google',
'calendar_event_id' => 'old_google_event_id',
]);

$this->mockGoogleCalendarService->shouldReceive('deleteEvent')->once()->andReturn(true);
$this->mockOutlookCalendarService->shouldReceive('createEvent')->once()->andReturn(true);
$this->mockGoogleCalendarService->shouldReceive('deleteEvent')->once()->with('old_google_event_id')->andReturn(true);
$this->mockOutlookCalendarService->shouldReceive('createEvent')->once()->andReturn('new_outlook_event_id');

$response = $this->patch("/tasks/{$task->id}", [
'calendar_type' => 'outlook',
Expand All @@ -133,9 +185,29 @@ public function testSwitchCalendarType()
$this->assertDatabaseHas('tasks', [
'id' => $task->id,
'calendar_type' => 'outlook',
'calendar_event_id' => 'new_outlook_event_id',
]);
}

public function testHandleCalendarSyncFailure()
{
$user = User::factory()->create();
$this->actingAs($user);

$this->mockGoogleCalendarService->shouldReceive('createEvent')->once()->andThrow(new \Exception('Failed to sync with Google Calendar'));

$response = $this->post('/tasks', [
'name' => 'Test Google Task',

'description' => 'Test Description',
'due_date' => now()->addDays(2),
'calendar_type' => 'google',
]);

$response->assertSessionHasErrors(['calendar_sync' => 'Failed to sync task with Google Calendar']);
$this->assertDatabaseMissing('tasks', ['name' => 'Test Google Task']);
}

protected function tearDown(): void
{
Mockery::close();
Expand Down
Loading

0 comments on commit d79da5f

Please sign in to comment.