ysato/spectator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

ysato/spectator

Composer 安装命令:

composer require ysato/spectator

包简介

An Artisan command package for Laravel to improve code quality and project structure.

关键字:

README 文档

README

OpenAPI specification coverage visualization library for Laravel Feature tests

PHP Version Laravel License

Overview

Ysato.Spectator is a library for visualizing OpenAPI specification coverage in Laravel Feature tests. By integrating it into your existing Feature tests, you can clearly see which endpoints defined in your OpenAPI specification are tested and which ones are missing from your test suite.

Key Features

  • Test Coverage Visualization: Display test coverage for OpenAPI specifications in a table format
  • Automatic Tracking: Automatically monitor Feature test execution and record endpoint usage
  • Laravel Integration: Easy integration with Laravel Feature tests

Installation

Install via Composer:

composer require --dev ysato/spectator

Usage

1. Prepare OpenAPI Specification File

Place an OpenAPI specification file (YAML format) in your project:

# openapi.yaml
openapi: 3.0.0
info:
  title: My API
  version: 1.0.0
paths:
  /api/users:
    get:
      responses:
        '200':
          description: Success
        '404':
          description: Not Found
    post:
      responses:
        '201':
          description: Created
        '422':
          description: Validation Error
  /api/users/{id}:
    get:
      responses:
        '200':
          description: Success
        '404':
          description: Not Found

2. Use Spectator in Feature Tests

Use the Spectatable trait in tests/Feature/TestCase.php:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Ysato\Spectator\Spectatable;

abstract class TestCase extends BaseTestCase
{
    use Spectatable;
}

3. Create Feature Tests

Create regular Feature tests. The Spectatable trait will automatically monitor API calls:

<?php

namespace Tests\Feature;

class UserTest extends TestCase
{
    public function test_can_list_users()
    {
        // Regular Feature test - automatically monitored
        $response = $this->get('/api/users');
        
        $response->assertStatus(200);
        $response->assertJsonStructure([
            'data' => [
                '*' => ['id', 'name', 'email']
            ]
        ]);
    }

    public function test_can_create_user()
    {
        $userData = [
            'name' => 'Test User',
            'email' => 'test@example.com',
            'password' => 'password123'
        ];

        $response = $this->post('/api/users', $userData);
        
        $response->assertStatus(201);
    }

    public function test_returns_404_for_nonexistent_user()
    {
        $response = $this->get('/api/users/9999');
        
        $response->assertStatus(404);
    }
}

4. Display Coverage Report

Set environment variable and run tests to display coverage report:

ENABLE_SPECTATION_REPORT=true ./vendor/bin/phpunit --no-progress --no-results

Example output:

┌─────────────┬────────┬─────────────────┬─────────────┐
│ IMPLEMENTED │ METHOD │ ENDPOINT        │ STATUS CODE │
├─────────────┼────────┼─────────────────┼─────────────┤
│ ✅          │ GET    │ /api/users      │ 200         │
│ ❌          │ GET    │ /api/users      │ 404         │
│ ✅          │ POST   │ /api/users      │ 201         │
│ ❌          │ POST   │ /api/users      │ 422         │
│ ❌          │ GET    │ /api/users/{id} │ 200         │
│ ✅          │ GET    │ /api/users/{id} │ 404         │
└─────────────┴────────┴─────────────────┴─────────────┘
  • ✅ = Tested
  • ❌ = Not tested

Configuration Options

Environment Variable Configuration

Set the OpenAPI specification file path with the OPENAPI_SPEC_PATH environment variable:

# .env
OPENAPI_SPEC_PATH=/path/to/your/openapi.yaml

Requirements

  • PHP 8.2 or higher
  • Laravel 11.43 or higher, or 12.0 or higher

License

This project is licensed under the MIT License. See the LICENSE file for details.

Support

If you encounter any issues or have questions, please report them on GitHub Issues.

ysato/spectator 适用场景与选型建议

ysato/spectator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.13k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.13k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 17
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-06