#add jira & message sync
This commit is contained in:
70
tests/Feature/EventConsumerSyncTest.php
Normal file
70
tests/Feature/EventConsumerSyncTest.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class EventConsumerSyncTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* 测试对比事件消费者同步状态 API
|
||||
*/
|
||||
public function test_compare_event_consumer_sync_api()
|
||||
{
|
||||
$response = $this->postJson('/api/message-sync/compare-event-consumer', [
|
||||
'start_time' => Carbon::now()->subDays(7)->format('Y-m-d H:i:s'),
|
||||
'end_time' => Carbon::now()->format('Y-m-d H:i:s'),
|
||||
'exclude_messages' => []
|
||||
]);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonStructure([
|
||||
'success',
|
||||
'data' => [
|
||||
'crm_total',
|
||||
'agent_total',
|
||||
'missing_count',
|
||||
'sync_rate',
|
||||
'missing_messages',
|
||||
'summary'
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试导出缺失消息 API
|
||||
*/
|
||||
public function test_export_missing_messages_api()
|
||||
{
|
||||
$response = $this->postJson('/api/message-sync/export-missing-messages', [
|
||||
'start_time' => Carbon::now()->subDays(7)->format('Y-m-d H:i:s'),
|
||||
'end_time' => Carbon::now()->format('Y-m-d H:i:s'),
|
||||
'exclude_messages' => []
|
||||
]);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonStructure([
|
||||
'success',
|
||||
'data' => [
|
||||
'csv',
|
||||
'filename',
|
||||
'count'
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试无效的时间格式
|
||||
*/
|
||||
public function test_invalid_time_format()
|
||||
{
|
||||
$response = $this->postJson('/api/message-sync/compare-event-consumer', [
|
||||
'start_time' => 'invalid-date',
|
||||
'end_time' => 'invalid-date'
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
}
|
||||
}
|
||||
|
||||
98
tests/Feature/MessageSyncTest.php
Normal file
98
tests/Feature/MessageSyncTest.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class MessageSyncTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* 测试admin框架页面是否可以访问
|
||||
*/
|
||||
public function test_admin_page_accessible(): void
|
||||
{
|
||||
$response = $this->get('/');
|
||||
$response->assertStatus(200);
|
||||
$response->assertViewIs('admin.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试查询消息API端点
|
||||
*/
|
||||
public function test_query_messages_api_validation(): void
|
||||
{
|
||||
// 测试空请求
|
||||
$response = $this->postJson('/api/message-sync/query', []);
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonStructure([
|
||||
'success',
|
||||
'message',
|
||||
'errors'
|
||||
]);
|
||||
|
||||
// 测试无效的消息ID格式
|
||||
$response = $this->postJson('/api/message-sync/query', [
|
||||
'message_ids' => ['']
|
||||
]);
|
||||
$response->assertStatus(422);
|
||||
$response->assertJson([
|
||||
'success' => false
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试同步消息API端点
|
||||
*/
|
||||
public function test_sync_messages_api_validation(): void
|
||||
{
|
||||
// 测试空请求
|
||||
$response = $this->postJson('/api/message-sync/sync', []);
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonStructure([
|
||||
'success',
|
||||
'message',
|
||||
'errors'
|
||||
]);
|
||||
|
||||
// 测试无效的消息ID格式
|
||||
$response = $this->postJson('/api/message-sync/sync', [
|
||||
'message_ids' => ['']
|
||||
]);
|
||||
$response->assertStatus(422);
|
||||
$response->assertJson([
|
||||
'success' => false
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试获取agent配置API端点
|
||||
*/
|
||||
public function test_get_agent_config_api(): void
|
||||
{
|
||||
$response = $this->getJson('/api/message-sync/config');
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonStructure([
|
||||
'success',
|
||||
'data' => [
|
||||
'agent_url',
|
||||
'timeout'
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试数据库连接测试API端点
|
||||
*/
|
||||
public function test_database_connection_test_api(): void
|
||||
{
|
||||
$response = $this->getJson('/api/message-sync/test-connection');
|
||||
|
||||
// 由于测试环境可能没有配置crmslave数据库,这里测试端点是否存在
|
||||
// 可能返回200(连接成功)或500(连接失败),都是正常的
|
||||
$this->assertContains($response->status(), [200, 500]);
|
||||
$response->assertJsonStructure([
|
||||
'success'
|
||||
]);
|
||||
}
|
||||
}
|
||||
184
tests/Unit/JiraServiceTest.php
Normal file
184
tests/Unit/JiraServiceTest.php
Normal file
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Services\JiraService;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class JiraServiceTest extends TestCase
|
||||
{
|
||||
private JiraService $jiraService;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// 设置测试配置
|
||||
config([
|
||||
'jira.host' => 'https://test-jira.example.com',
|
||||
'jira.username' => 'test-user',
|
||||
'jira.password' => 'test-password',
|
||||
'jira.default_user' => 'test-user'
|
||||
]);
|
||||
|
||||
$this->jiraService = app(JiraService::class);
|
||||
}
|
||||
|
||||
public function test_is_task_completed_returns_false_for_incomplete_statuses()
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->jiraService);
|
||||
$method = $reflection->getMethod('isTaskCompleted');
|
||||
|
||||
// 测试不完成的状态
|
||||
$incompleteStatuses = [
|
||||
'开发中',
|
||||
'需求已排期',
|
||||
'需求已评审',
|
||||
'In Progress',
|
||||
'To Do',
|
||||
'Open'
|
||||
];
|
||||
|
||||
foreach ($incompleteStatuses as $status) {
|
||||
$this->assertFalse(
|
||||
$method->invoke($this->jiraService, $status),
|
||||
"状态 '{$status}' 应该返回 false"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_is_task_completed_returns_true_for_complete_statuses()
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->jiraService);
|
||||
$method = $reflection->getMethod('isTaskCompleted');
|
||||
|
||||
// 测试完成的状态
|
||||
$completeStatuses = [
|
||||
'Done',
|
||||
'Closed',
|
||||
'Resolved',
|
||||
'已完成',
|
||||
'Complete'
|
||||
];
|
||||
|
||||
foreach ($completeStatuses as $status) {
|
||||
$this->assertTrue(
|
||||
$method->invoke($this->jiraService, $status),
|
||||
"状态 '{$status}' 应该返回 true"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_organize_tasks_for_report_handles_empty_collection()
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->jiraService);
|
||||
$method = $reflection->getMethod('organizeTasksForReport');
|
||||
|
||||
$emptyWorkLogs = collect();
|
||||
$result = $method->invoke($this->jiraService, $emptyWorkLogs);
|
||||
|
||||
$this->assertInstanceOf(Collection::class, $result);
|
||||
$this->assertTrue($result->has('sprints'));
|
||||
$this->assertTrue($result->has('tasks'));
|
||||
$this->assertTrue($result->has('bugs'));
|
||||
}
|
||||
|
||||
public function test_organize_tasks_for_report_categorizes_by_sprint()
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->jiraService);
|
||||
$method = $reflection->getMethod('organizeTasksForReport');
|
||||
|
||||
$workLogs = collect([
|
||||
[
|
||||
'issue_key' => 'WP-1234',
|
||||
'issue_summary' => '测试需求',
|
||||
'issue_url' => 'https://test-jira.example.com/browse/WP-1234',
|
||||
'issue_status' => 'Done',
|
||||
'issue_type' => 'Story',
|
||||
'sprint' => '十月中需求',
|
||||
'bug_stage' => null,
|
||||
'bug_type' => null,
|
||||
'parent_task' => null,
|
||||
]
|
||||
]);
|
||||
|
||||
$result = $method->invoke($this->jiraService, $workLogs);
|
||||
|
||||
$this->assertTrue($result['sprints']->has('十月中需求'));
|
||||
$this->assertCount(1, $result['sprints']['十月中需求']);
|
||||
}
|
||||
|
||||
public function test_organize_tasks_for_report_categorizes_bugs_by_stage()
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->jiraService);
|
||||
$method = $reflection->getMethod('organizeTasksForReport');
|
||||
|
||||
$workLogs = collect([
|
||||
[
|
||||
'issue_key' => 'WP-5678',
|
||||
'issue_summary' => '测试Bug',
|
||||
'issue_url' => 'https://test-jira.example.com/browse/WP-5678',
|
||||
'issue_status' => 'Done',
|
||||
'issue_type' => 'Bug',
|
||||
'sprint' => null,
|
||||
'bug_stage' => 'SIT环境BUG',
|
||||
'bug_type' => '需求未说明',
|
||||
'parent_task' => null,
|
||||
]
|
||||
]);
|
||||
|
||||
$result = $method->invoke($this->jiraService, $workLogs);
|
||||
|
||||
$this->assertTrue($result['bugs']->has('SIT环境BUG'));
|
||||
$this->assertCount(1, $result['bugs']['SIT环境BUG']);
|
||||
$this->assertEquals('需求未说明', $result['bugs']['SIT环境BUG'][0]['bug_type']);
|
||||
}
|
||||
|
||||
public function test_extract_sprint_info_from_string()
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->jiraService);
|
||||
$method = $reflection->getMethod('extractSprintInfo');
|
||||
|
||||
$issue = (object)[
|
||||
'fields' => (object)[
|
||||
'customfield_10020' => [
|
||||
'com.atlassian.greenhopper.service.sprint.Sprint@xxx[name=十月中需求,state=ACTIVE]'
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
$result = $method->invoke($this->jiraService, $issue);
|
||||
$this->assertEquals('十月中需求', $result);
|
||||
}
|
||||
|
||||
public function test_extract_bug_stage_from_labels()
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->jiraService);
|
||||
$method = $reflection->getMethod('extractBugStage');
|
||||
|
||||
$issue = (object)[
|
||||
'fields' => (object)[
|
||||
'labels' => ['SIT', 'bug']
|
||||
]
|
||||
];
|
||||
|
||||
$result = $method->invoke($this->jiraService, $issue);
|
||||
$this->assertEquals('SIT环境BUG', $result);
|
||||
}
|
||||
|
||||
public function test_extract_bug_type_from_labels()
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->jiraService);
|
||||
$method = $reflection->getMethod('extractBugType');
|
||||
|
||||
$issue = (object)[
|
||||
'fields' => (object)[
|
||||
'labels' => ['需求未说明', 'bug']
|
||||
]
|
||||
];
|
||||
|
||||
$result = $method->invoke($this->jiraService, $issue);
|
||||
$this->assertEquals('需求未说明', $result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user