Files
agent-unit-testing/.claude/skills/cakephp-test-review/skill.md
T
Pham-Tan-Thien da1bd48444 feat: decouple Laravel and CakePHP installers, remove SonarQube artifacts, and enforce complete unit testing standards
- Decouple framework installation: add install-laravel.sh and install-cakephp.sh, isolate skills and docs per framework, and auto-clean leftover artifacts
- Remove all SonarQube configs, templates, and references
- Enforce comprehensive test completeness: 100% public methods, edge cases, full branch coverage, exception paths, and state assertions
- Replace all placeholder assertions in PHPUnit and Pest templates with full realistic implementations
- Rename and update 10-comprehensive-test-coverage-guide.md
2026-09-17 14:25:02 +07:00

68 lines
4.0 KiB
Markdown

---
name: cakephp-test-review
description: Đánh giá, chấm điểm chất lượng và phát hiện các lỗi anti-patterns trong file Unit / Integration Test CakePHP có sẵn theo thang điểm 100.
---
# Skill: CakePHP Test Reviewer
Skill này chịu trách nhiệm kiểm tra chất lượng và phát hiện lỗi anti-patterns trong một file test CakePHP đã viết sẵn.
## Cách kích hoạt
```text
/cakephp-test-review <đường_dẫn_file_test>
```
*Ví dụ*: `/cakephp-test-review tests/TestCase/Model/Table/ArticlesTableTest.php`
*Ví dụ*: `/cakephp-test-review tests/TestCase/Controller/ArticlesControllerTest.php`
---
## Tiêu Chí Đánh Giá (Scoring Criteria - Thang Điểm 100)
Agent sẽ quét file test qua 5 tiêu chí chuẩn:
### 1. Tính Cô Lập & An Toàn Dữ Liệu (Isolation & Database Safety - 25đ)
- Test có dùng đúng test datasource (`Datasources.test`) hoặc fixtures không, hay vô tình tác động lên database dev thật?
- Có bị gọi HTTP ra API ngoài thực tế mà quên mock không?
- Fixture có được khai báo tối giản chỉ những bảng cần thiết (tránh nạp thừa hàng chục bảng gây chậm)?
### 2. Sử Dụng Đúng Trait & Chuẩn CakePHP (CakePHP Conventions - 20đ)
- Controller test có dùng `Cake\TestSuite\IntegrationTestTrait` không (hay tạo `new Controller` thủ công - Anti-pattern)?
- Command test có dùng `Cake\Console\TestSuite\ConsoleIntegrationTestTrait` không?
- Mailer test có dùng `Cake\TestSuite\EmailTrait` không?
- Có bật `enableCsrfToken()` và `enableSecurityToken()` khi test POST/PUT không?
### 3. Chiến Lược Giả Lập & Fixtures (Mocking vs Fixtures - 20đ)
- Có bị over-mocking (Mock cả Entity, Table query cơ bản thay vì dùng Fixture) không?
- Các service bên thứ 3 (Payment, Notification, Cloud Storage) có được mock rõ ràng với số lần gọi (`->once()`, `->never()`) không?
### 4. Cấu Trúc & Assertions (Structure & Assertions - 20đ)
- Có tuân thủ mô hình **AAA** (`// Arrange`, `// Act`, `// Assert`) rõ ràng không?
- Dùng assertion đặc thù của CakePHP: `$this->assertResponseOk()`, `$this->assertResponseCode(200)`, `$this->assertRedirect()`, `$this->assertFlashMessage()` thay vì assert thủ công yếu ớt?
- Dùng strict assertions: `$this->assertSame()` thay vì `$this->assertEquals()`.
### 5. Tính Đầy Đủ & Độ Phủ Kịch Bản (Completeness & Scenario Coverage - 15đ)
- **100% Public Methods**: Tất cả các hàm public của class nguồn đã có bài test tương ứng chưa? Có method nào bị bỏ sót không?
- **Đầy đủ kịch bản**: Đã kiểm tra cả Happy Path lẫn Unhappy Path (validation lỗi, entity không tìm thấy 404, không có quyền 403, exception trong try/catch)?
- **Kịch bản biên & Nhánh rẽ**: Các giá trị biên (`null`, mảng rỗng, chuỗi trắng) và mọi nhánh `if/else`, `??`, `?:` đã được kiểm thử đầy đủ chưa?
- **Side Effects**: Dữ liệu DB thật sự thay đổi qua Fixture, Mailer/Command output có được verify đầy đủ không?
---
## Báo Cáo Đầu Ra Mẫu:
```markdown
### 🔍 BÁO CÁO REVIEW TEST CAKEPHP: `ArticlesControllerTest.php`
**Điểm tổng kết: 65/100 (Cần cải thiện)**
#### ⚠️ Các vấn đề phát hiện (Anti-Patterns):
1. **Thiếu CSRF/Security Token tại line 32**: Gọi POST `/articles/add` nhưng chưa kích hoạt `$this->enableCsrfToken()`, dễ dẫn đến test fail khi bật middleware CSRF.
2. **Assertion lỏng lẻo tại line 45**: Dùng `$this->assertTrue(true)` thay vì `$this->assertResponseSuccess()` hoặc kiểm tra dữ liệu thật được lưu vào DB.
3. **Chưa có kịch bản Validation Failure**: Chưa kiểm thử trường hợp người dùng submit form thiếu trường `title` bắt buộc.
#### 💡 Đề xuất Refactor:
```php
// Đoạn code được sửa lại chuẩn AAA và tận dụng đầy đủ IntegrationTestTrait...
```
```