Files
toolbox/tests/Unit/JiraServiceTest.php
2025-12-02 10:16:32 +08:00

185 lines
5.7 KiB
PHP

<?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);
}
}