定制 testflowlabs/testlink 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

testflowlabs/testlink

Composer 安装命令:

composer require --dev testflowlabs/testlink

包简介

Framework-agnostic test traceability for PHP. Supports Pest and PHPUnit with bidirectional code-to-test linking.

README 文档

README

TestLink Logo

TestLink

Framework-Agnostic Test Traceability for PHP

PHP Version Pest Support PHPUnit Support License

TestLink creates bidirectional links between your tests and production code. Know exactly which tests cover each method, and which methods each test exercises.

Supports both Pest and PHPUnit - use whichever testing framework you prefer, or both in the same project.

Features

  • Framework Agnostic - Works with Pest, PHPUnit, or both
  • Bidirectional Linking - Link tests to methods AND methods to tests
  • Two Link Types - Coverage links (linksAndCovers) and traceability-only links (links)
  • Sync Validation - Detect missing or orphaned links
  • Auto-Sync - Generate link calls from #[TestedBy] attributes
  • Standalone CLI - Use testlink command independently of test runner
  • JSON Export - CI/CD integration

Quick Start

Installation

# Production dependency - attributes for production code
composer require testflowlabs/test-attributes

# Dev dependency - CLI tools, scanners, validators
composer require --dev testflowlabs/testlink

Why two packages?

  • test-attributes must be a production dependency because #[TestedBy], #[LinksAndCovers], and #[Links] attributes are placed on production classes. PHP needs these attribute classes available when autoloading your production code.
  • testlink can be a dev dependency because it only provides CLI tools (testlink report, testlink validate, testlink sync) that run during development.

Link from Production Code (Recommended)

// app/Services/UserService.php
use TestFlowLabs\TestingAttributes\TestedBy;

class UserService
{
    #[TestedBy(UserServiceTest::class, 'it creates a user')]
    #[TestedBy(UserServiceTest::class, 'it validates email format')]
    public function create(array $data): User
    {
        // ...
    }
}

Link from Tests

Pest

// tests/Unit/UserServiceTest.php

// Link + Coverage (triggers coverage tracking)
test('it creates a user')
    ->linksAndCovers(UserService::class.'::create');

// Link only (traceability without coverage)
test('it creates a user integration')
    ->links(UserService::class.'::create');

// Multiple methods
test('it validates and creates')
    ->linksAndCovers(UserService::class.'::validate')
    ->linksAndCovers(UserService::class.'::create');

PHPUnit

// tests/Unit/UserServiceTest.php
use TestFlowLabs\TestingAttributes\LinksAndCovers;
use TestFlowLabs\TestingAttributes\Links;

class UserServiceTest extends TestCase
{
    // Link + Coverage
    #[LinksAndCovers(UserService::class, 'create')]
    public function testItCreatesUser(): void
    {
        // ...
    }

    // Link only
    #[Links(UserService::class, 'create')]
    public function testItCreatesUserIntegration(): void
    {
        // ...
    }

    // Multiple methods
    #[LinksAndCovers(UserService::class, 'validate')]
    #[LinksAndCovers(UserService::class, 'create')]
    public function testItValidatesAndCreates(): void
    {
        // ...
    }
}

CLI Commands

# Show coverage links report
testlink report

# Validate bidirectional sync
testlink validate

# Auto-sync from #[TestedBy] to test files
testlink sync

# Preview sync changes (dry run)
testlink sync --dry-run

# Sync and prune orphaned links
testlink sync --prune --force

# Export as JSON
testlink report --json

# Show help
testlink --help
testlink sync --help

Sample Output

  Coverage Links Report
  ─────────────────────

  App\Services\UserService

    create()
      → Tests\Unit\UserServiceTest::it creates a user
      → Tests\Unit\UserServiceTest::it validates email format

    update()
      → Tests\Unit\UserServiceTest::it updates a user

  Summary
    Methods with tests: 2
    Total test links: 3

Validation

The validate command checks for:

  • Missing Coverage: Production methods with #[TestedBy] but no matching link call
  • Orphaned Links: Tests claiming links that don't exist
  • Sync Issues: Mismatched bidirectional links
$ testlink validate

  Validation Report
  ─────────────────

  Missing Link Calls in Tests
  These #[TestedBy] attributes have no corresponding link calls:

    ✗ App\Services\OrderService::process
      → Tests\Unit\OrderServiceTest::it processes order

  Orphaned Link Calls in Tests
  These link calls have no corresponding #[TestedBy] attributes:

    ! Tests\Unit\PaymentTest::it charges card
      → App\Services\PaymentService::charge

  Validation failed. Run "testlink sync" to fix issues.

Auto-Sync

Automatically generate link calls from #[TestedBy] attributes:

# Preview what will change
testlink sync --dry-run

# Apply sync
testlink sync

# Sync and remove orphaned links
testlink sync --prune --force

How It Works

  1. Scans production code for #[TestedBy] attributes
  2. Locates corresponding test files and test cases
  3. Adds missing linksAndCovers() calls (Pest) or #[LinksAndCovers] attributes (PHPUnit)

Before Sync (Pest)

// Production code has the attribute
#[TestedBy(UserServiceTest::class, 'it creates a user')]
public function create(): User { }

// Test file is missing link
test('it creates a user', function () { });

After Sync (Pest)

test('it creates a user', function () {
    // ...
})->linksAndCovers(UserService::class.'::create');

Before Sync (PHPUnit)

// Production code has the attribute
#[TestedBy(UserServiceTest::class, 'testItCreatesUser')]
public function create(): User { }

// Test file is missing attribute
public function testItCreatesUser(): void { }

After Sync (PHPUnit)

#[LinksAndCovers(UserService::class, 'create')]
public function testItCreatesUser(): void { }

Link Types

Type Pest Method PHPUnit Attribute Purpose
Link + Coverage ->linksAndCovers() #[LinksAndCovers] Traceability + triggers coverage tracking
Link Only ->links() #[Links] Traceability only, no coverage

Use Link + Coverage for unit tests where you want coverage tracking. Use Link Only for integration/e2e tests where unit coverage is already tracked elsewhere.

JSON Export

For CI/CD integration:

testlink report --json > coverage-links.json
{
  "links": {
    "App\\Services\\UserService::create": [
      "Tests\\Unit\\UserServiceTest::it creates a user"
    ]
  },
  "summary": {
    "total_methods": 1,
    "total_tests": 1
  }
}

Bootstrap (Pest)

Add to tests/Pest.php to enable linksAndCovers() and links() methods:

use TestFlowLabs\TestLink\Runtime\RuntimeBootstrap;

RuntimeBootstrap::init();

Best Practices

1. Prefer #[TestedBy] Attributes

Placing links in production code keeps coverage visible where it matters:

#[TestedBy(UserServiceTest::class, 'it creates a user')]
public function create(): User
{
    // Reader immediately knows this method is tested
}

2. Use Link Types Appropriately

// Unit test - use linksAndCovers for coverage
test('it creates a user with valid data')
    ->linksAndCovers(UserService::class.'::create');

// Integration test - use links for traceability only
test('it creates user through API endpoint')
    ->links(UserService::class.'::create');

3. Run Validation in CI

# .github/workflows/test.yml
- name: Validate coverage links
  run: testlink validate --strict

Hybrid Projects

TestLink seamlessly supports projects using both Pest and PHPUnit:

$ testlink report

  Coverage Links Report
  ─────────────────────

  Detected frameworks: pest, phpunit

  App\Services\UserService
    create()
      → Tests\Unit\UserServiceTest::it creates a user (pest)
      → Tests\Integration\UserApiTest::testCreateUser (phpunit)

Documentation

Full documentation is available at the TestLink Documentation.

  • Tutorials - Learn TestLink step-by-step with TDD and BDD workflows
  • How-to Guides - Solve specific problems and tasks
  • Reference - CLI commands, attributes, and configuration
  • Explanation - Understand bidirectional linking concepts

Ecosystem

TestLink is part of the TestFlowLabs ecosystem:

Package Description
test-attributes PHP attributes for test metadata (#[LinksAndCovers], #[Links])
testlink This package - Test traceability, #[TestedBy] attribute, CLI tools

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Run composer test to ensure all checks pass
  4. Submit a pull request

License

MIT License. See LICENSE for details.

testflowlabs/testlink 适用场景与选型建议

testflowlabs/testlink 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 5, 最近一次更新时间为 2025 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「testing」 「phpunit」 「coverage」 「pest」 「testlink」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 testflowlabs/testlink 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 testflowlabs/testlink 我们能提供哪些服务?
定制开发 / 二次开发

基于 testflowlabs/testlink 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-13