Laravel 测试设计与评审
testing-best-practices
Laravel 测试设计与评审。在选择测试覆盖率、命名或组织测试结构、选择断言或测试数据、隔离依赖、测试 HTTP 或安全边界、提升测试套件性能,或评估测试价值时使用。Pest 和 PHPUnit 语法请参考框架指南或 search-docs。
- License 许可
- MIT
- 最近更新
- 2026-11-12
- Stars
- ★ 6
- Forks
- 1
01 前言 Front Matter
- name
- testing-best-practices
- license
- MIT
- metadata
- { "author": "laravel" }
- description
- Laravel test design and review. Use when selecting coverage, naming or structuring tests, choosing assertions or test data, isolating dependencies, testing HTTP or security boundaries, improving suite performance, or reviewing test value. Use framework guidance or search-docs for Pest and PHPUnit syntax.
02 资源文件 14
- 参考文档avalonia-reactive-rules.md1.9 KB
- 参考文档core-technical-skills.md1.0 KB
- 参考文档naming-standards.md739 B
- 参考文档patterns.md1.3 KB
- 参考文档rules/assertions.md3.3 KB
- 参考文档rules/endpoint-tests.md3.0 KB
- 参考文档rules/finding-features.md2.4 KB
- 参考文档rules/isolation.md3.8 KB
- 参考文档rules/naming.md2.2 KB
- 参考文档rules/performance.md3.3 KB
- 参考文档rules/review.md2.9 KB
- 参考文档rules/security.md1.6 KB
- 参考文档rules/test-data.md2.2 KB
- 参考文档zafiro-shortcuts.md1.5 KB
03 SKILL.md 正文
Testing Best Practices
This skill provides rules for designing Laravel tests. Each rule file explains what to do and why. Use search-docs for Laravel and Pest API syntax.
This project uses Pest. Follow the corresponding guidance in each rule.
Consistency First
Read nearby tests before you choose syntax and organization.
A pattern repeated throughout the project is a convention, and project conventions take precedence over this skill. Follow them and give new tests the same structure.
These rules govern the tests you write now. An existing test that follows a project convention is not defective merely because it conflicts with this skill. Do not delete or rewrite it. If the convention has drawbacks, explain them and let the user decide.
What to Test
Read this section before you write a test.
- Test observable behavior and application contracts. A test must pass after an implementation change if the behavior stays the same.
- Cover every changed decision and each applicable high-value failure mode. A decision is a branch, a validation, a calculation, or an authorization.
- Exercise declarations through behavior instead of repeating their text.
- Leave framework behavior to framework tests. Testing project configuration is not testing the framework. A constrained relationship, cast, scope, or validation rule belongs to this project.
- Keep every test that can detect a distinct defect. When two tests detect the same defect, trim the higher-layer test to one case and report the duplication. Do not delete an existing test.
- Write a feature test first. Write a unit test only for logic that does not use the framework.
- Write a feature test for every behavior reachable through a request. Real-browser tests require
pestphp/pest-plugin-browserand a browser download, neither of which this project installs. Mention the package only if the user asks for a real-browser test. - Judge an architecture test by the convention it protects, not by the rules above. An
arch()test declares a rule for an entire directory, such as the parent class of every model, the classes that may use an enum, or the methods every factory declares. It intentionally checks declarations and fails when a new file breaks the convention. - Use the test tools that the project installs. Add a new test dependency, plugin, or browser only after the user asks for it.
How to Apply
- Read the code under test. Read the tests in the same directory. Identify every decision in the code.
- Select every applicable branch in the rule index. Read every selected rule file.
- Report each defect in the code before you write a test. Examples are a method with no body, a policy that no action calls, and a write action with no validation. Test the actual behavior. Report the defect to the user.
- Write the tests. Run the smallest set of tests that covers the change. The tests must pass.
- Check every applicable item in
rules/review.mdand every selected rule file. Resolve every mismatch before completion.
Rule Index
Most changes need more than one rule file.
| Subject | Rule File |
|---|---|
| Test framework features that may already do the work | rules/finding-features.md |
| File layout, test names, and groups | rules/naming.md |
| Arrange-act-assert and choosing the correct assertion | rules/assertions.md |
| Endpoint coverage, authentication, authorization, tenant isolation, validation, and browser tests | rules/endpoint-tests.md |
| Factories, test data ownership, and repeated input values | rules/test-data.md |
| Fakes, mocks, outbound HTTP, time, randomness, and databases | rules/isolation.md |
| Escaping, injection, cross-tenant access, and privilege checks | rules/security.md |
| Environment and CI settings for a slow suite | rules/performance.md |
| Reviewing a test or suite | rules/review.md |
04 同仓库更多 Skills
Laravel 测试设计与评审
testing-best-practices
Laravel 测试设计与评审。适用于选择覆盖范围、测试命名或结构安排、选择断言或测试数据、隔离依赖、测试 HTTP 或安全边界、提升测试套件性能,以及评估测试价值。Pest 和 PHPUnit 语法请使用框架指南或 search-docs 查询。
Tailwind CSS 界面样式开发
tailwindcss-development
当用户消息以任何形式包含 'tailwind' 时必须调用。以下情形也应调用:构建响应式网格布局(多列卡片网格、商品网格)、flex/grid 页面结构(带侧边栏的仪表盘、固定顶栏、移动端切换导航)、UI 组件样式设计(卡片、表格、导航栏、定价区块、表单、输入框、徽章)、添加暗色模式变体、修复间距或排版问题,以及 Tailwind v3/v4 相关工作。核心用途:在 HTML 模板(Blade、JSX、Vue)中编写或修复 Tailwind 工具类。以下情形跳过:后端 PHP 逻辑、数据库查询、API 路由、不含 HTML/CSS 组件的 JavaScript、CSS 文件审查、构建工具配置,以及原生 CSS。
Laravel Slug 与自愈 URL 开发
sluggable-development
适用场景:让 Eloquent 模型的某个字段成为 slug、为 slug 列生成迁移、切换到自愈 URL,或在路由中根据 slug 解析模型。当请求提及 slug、永久链接、spatie/laravel-sluggable 包、HasSlug 或 HasTranslatableSlug trait、#[Sluggable] 特性、SlugOptions、findBySlug、自愈 URL,或 Laravel 项目中的旧 slug 重定向时触发。内容涵盖:特性(attribute)与 trait 的选择、生成唯一 slug、配置分隔符/长度/语言/作用域/唯一性、防止 slug 被覆盖、通过 spatie/laravel-translatable 实现多语言 slug、基于 slug 列的路由模型绑定、自愈路由键、通过 SelfHealing facade 自定义 308 重定向,以及通过 config/sluggable.php 覆盖默认操作。不适用于一次性 URL 安全字符串、签名 URL,或结果不存储到模型上的 Laravel 内置 Str::slug 辅助函数。
Laravel Pulse 监控配置与卡片开发
pulse-development
处理 Laravel Pulse 的安装配置与自定义卡片开发。在以下情形触发:安装 Pulse;配置仪表盘或授权门禁(Gate);设置记录器(recorder)与过滤规则;构建自定义 Livewire 卡片;通过 Redis 摄取或采样进行优化;或用户提及 /pulse、pulse:check、pulse:work、Pulse::record(),以及应用监控相关内容。
Laravel 媒体库开发
medialibrary-development
构建并使用 spatie/laravel-medialibrary 的功能,包括将文件与 Eloquent 模型关联、定义媒体集合与转换、生成响应式图片,以及获取媒体 URL 和路径。
Livewire 组件开发
livewire-development
适用于任何涉及 Livewire 的任务或问题。当用户提及 Livewire、wire: 指令,或 wire:model、wire:click、wire:sort、islands 等 Livewire 专有概念时,调用此技能。涵盖构建新组件、调试响应式问题、实时表单验证、拖放操作、加载状态、从 Livewire 3 迁移到 4、组件格式转换(SFC/MFC/基于类)以及性能优化。不适用于非 Livewire 的响应式 UI(React、Vue、仅 Alpine、Inertia.js),也不适用于不含 Livewire 的标准 Laravel 表单。