Files
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

86 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\{{Namespace}};
use Tests\TestCase;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\DataProvider;
use Illuminate\Support\Facades\Validator;
use {{TargetClassFullName}};
class {{ClassName}}Test extends TestCase
{
private {{ClassName}} $request;
protected function setUp(): void
{
parent::setUp();
$this->request = new {{ClassName}}();
}
#[Test]
public function it_authorizes_request_appropriately(): void
{
// Assert: Xác minh phương thức authorize() cho phép thực thi
$this->assertTrue($this->request->authorize());
}
#[Test]
public function it_passes_validation_with_valid_payload(): void
{
// 1. Arrange: Chuẩn bị dữ liệu hợp lệ thỏa mãn toàn bộ rules
$validPayload = [
'name' => 'John Doe',
'email' => 'john@example.com',
'status' => 'active',
];
// 2. Act
$validator = Validator::make($validPayload, $this->request->rules());
// 3. Assert
$this->assertFalse($validator->fails(), 'Validation should pass with valid payload');
}
#[Test]
public function it_fails_validation_when_required_fields_are_missing(): void
{
// 1. Arrange: Payload rỗng thiếu toàn bộ trường bắt buộc
$emptyPayload = [];
// 2. Act
$validator = Validator::make($emptyPayload, $this->request->rules());
// 3. Assert
$this->assertTrue($validator->fails(), 'Validation must fail when required fields are missing');
}
#[Test]
#[DataProvider('invalidPayloadDataProvider')]
public function it_fails_validation_for_specific_invalid_rules(array $invalidPayload, string $expectedFailedField): void
{
// Act
$validator = Validator::make($invalidPayload, $this->request->rules());
// Assert: Bắt buộc xác minh lỗi vi phạm đúng trường mong đợi
$this->assertTrue($validator->fails());
$this->assertArrayHasKey($expectedFailedField, $validator->errors()->toArray());
}
public static function invalidPayloadDataProvider(): array
{
return [
'invalid email format' => [
['name' => 'John Doe', 'email' => 'invalid-email-format'],
'email',
],
'whitespace name' => [
['name' => ' ', 'email' => 'john@example.com'],
'name',
],
];
}
}