51 خطوط
2.2 KiB
PHP
51 خطوط
2.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Campaign;
|
|
use App\Models\Lead;
|
|
use App\Models\Product;
|
|
use App\Models\User;
|
|
use Database\Seeders\RolePermissionSeeder;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class CampaignMetricsTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_campaign_list_reports_real_contact_conversion_target_and_revenue_metrics(): void
|
|
{
|
|
$this->seed(RolePermissionSeeder::class);
|
|
$admin = User::factory()->create(['is_active' => true]);
|
|
$admin->assignRole('admin');
|
|
$product = Product::create(['name' => 'اشتراک سالانه', 'base_price' => 500000, 'is_active' => true]);
|
|
$campaign = Campaign::create([
|
|
'name' => 'فروش تابستان',
|
|
'target' => 10,
|
|
'status' => 'active',
|
|
'product_id' => $product->id,
|
|
'channel' => 'phone',
|
|
'budget' => 150000,
|
|
'actual_cost' => 100000,
|
|
]);
|
|
|
|
Lead::create(['campaign_id' => $campaign->id, 'company' => 'اول', 'first_name' => '', 'last_name' => '', 'phone' => '02111111111', 'last_call_at' => now(), 'final_result' => 'موفق', 'deal_value' => 500000]);
|
|
Lead::create(['campaign_id' => $campaign->id, 'company' => 'دوم', 'first_name' => '', 'last_name' => '', 'phone' => '02122222222', 'last_call_at' => now()]);
|
|
Lead::create(['campaign_id' => $campaign->id, 'company' => 'سوم', 'first_name' => '', 'last_name' => '', 'phone' => '02133333333']);
|
|
|
|
$this->actingAs($admin)->getJson('/api/campaigns')
|
|
->assertOk()
|
|
->assertJsonPath('data.0.leads_count', 3)
|
|
->assertJsonPath('data.0.contacted_leads_count', 2)
|
|
->assertJsonPath('data.0.won_leads_count', 1)
|
|
->assertJsonPath('data.0.won_value', 500000)
|
|
->assertJsonPath('data.0.conversion_rate', 33.3)
|
|
->assertJsonPath('data.0.target_progress', 10)
|
|
->assertJsonPath('data.0.product.id', $product->id)
|
|
->assertJsonPath('data.0.channel', 'phone')
|
|
->assertJsonPath('data.0.cost_per_lead', 33333.33)
|
|
->assertJsonPath('data.0.roi', 400);
|
|
}
|
|
}
|