CRM/backend/tests/Feature/CallContractTest.php

123 خطوط
3.9 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Call;
use App\Models\Lead;
use App\Models\User;
use Database\Seeders\RolePermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class CallContractTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seed(RolePermissionSeeder::class);
}
public function test_call_list_uses_the_canonical_envelope_and_fields(): void
{
$admin = $this->admin();
$lead = $this->lead('Needle Customer');
$call = Call::create([
'lead_id' => $lead->id,
'user_id' => $admin->id,
'direction' => 'outbound',
'phone' => '02112345678',
'duration' => 42,
'result' => 'پاسخ داده شد',
'provider_call_id' => 'provider-needle',
]);
$response = $this->actingAs($admin)->getJson('/api/calls?search=Needle');
$response->assertOk()
->assertJsonPath('data.0.id', $call->id)
->assertJsonPath('data.0.user_id', $admin->id)
->assertJsonPath('data.0.agent.name', $admin->name)
->assertJsonPath('data.0.direction', 'outbound')
->assertJsonPath('data.0.status', 'completed')
->assertJsonPath('data.0.duration_seconds', 42)
->assertJsonPath('meta.total', 1)
->assertJsonMissingPath('data.0.caller_id')
->assertJsonMissingPath('data.0.call_type')
->assertJsonMissingPath('data.0.call_status');
}
public function test_call_search_filters_instead_of_being_a_frontend_only_control(): void
{
$admin = $this->admin();
$matchingLead = $this->lead('Matching Company');
$otherLead = $this->lead('Other Company');
foreach ([$matchingLead, $otherLead] as $index => $lead) {
Call::create([
'lead_id' => $lead->id,
'user_id' => $admin->id,
'direction' => 'outbound',
'phone' => "0210000000{$index}",
]);
}
$this->actingAs($admin)->getJson('/api/calls?search=Matching')
->assertOk()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.lead.company', 'Matching Company');
}
public function test_manual_call_result_can_be_recorded_without_a_voip_provider(): void
{
$admin = $this->admin();
$lead = $this->lead('Manual Result Company');
$response = $this->actingAs($admin)->postJson('/api/calls/manual-result', [
'lead_id' => $lead->id,
'contact_phone_id' => $lead->contacts()->firstOrCreate([
'name' => 'Ali Karimi',
], [
'status' => 'active',
'is_primary' => true,
'created_by' => $admin->id,
])->phones()->create([
'phone' => $lead->phone,
'type' => 'mobile',
'status' => 'active',
])->id,
'result' => 'شماره اشتباه',
'notes' => 'ثبت دستی پس از تماس',
]);
$response->assertCreated()
->assertJsonPath('data.result', 'شماره اشتباه')
->assertJsonPath('data.is_manual', true);
$this->assertDatabaseHas('calls', [
'lead_id' => $lead->id,
'user_id' => $admin->id,
'result' => 'شماره اشتباه',
'provider_status' => 'completed',
]);
}
private function admin(): User
{
$user = User::factory()->create(['is_active' => true]);
$user->assignRole('admin');
return $user;
}
private function lead(string $company): Lead
{
return Lead::create([
'first_name' => 'Ali',
'last_name' => 'Karimi',
'company' => $company,
'phone' => fake()->unique()->numerify('021########'),
]);
}
}