da1bd48444
- 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
89 lines
2.4 KiB
PHP
89 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\{{Namespace}};
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use Mockery;
|
|
use Mockery\MockInterface;
|
|
use {{TargetClassFullName}};
|
|
|
|
class {{ClassName}}Test extends TestCase
|
|
{
|
|
private MockInterface $dependencyMock;
|
|
private {{ClassName}} $service;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
// Giả lập dependencies qua Mockery
|
|
$this->dependencyMock = Mockery::mock('DependencyInterface');
|
|
$this->service = new {{ClassName}}($this->dependencyMock);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
Mockery::close();
|
|
parent::tearDown();
|
|
}
|
|
|
|
#[Test]
|
|
public function it_executes_successfully_with_valid_input(): void
|
|
{
|
|
// 1. Arrange
|
|
$input = 'valid_input';
|
|
$expectedResult = 'expected_output';
|
|
|
|
$this->dependencyMock
|
|
->shouldReceive('execute')
|
|
->once()
|
|
->with($input)
|
|
->andReturn($expectedResult);
|
|
|
|
// 2. Act
|
|
$actualResult = $this->service->process($input);
|
|
|
|
// 3. Assert
|
|
$this->assertSame($expectedResult, $actualResult);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_handles_edge_cases_with_null_or_empty_input(): void
|
|
{
|
|
// 1. Arrange - Test kịch bản biên với giá trị rỗng/mặc định
|
|
$fallbackResult = 'default_fallback';
|
|
|
|
$this->dependencyMock
|
|
->shouldReceive('execute')
|
|
->once()
|
|
->with('')
|
|
->andReturn($fallbackResult);
|
|
|
|
// 2. Act
|
|
$result = $this->service->process('');
|
|
|
|
// 3. Assert - Xác minh xử lý đúng giá trị biên
|
|
$this->assertSame($fallbackResult, $result);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_throws_exception_and_handles_error_when_dependency_fails(): void
|
|
{
|
|
// 1. Arrange - Ép mock ném ngoại lệ để kiểm thử khối catch / exception handling
|
|
$this->dependencyMock
|
|
->shouldReceive('execute')
|
|
->once()
|
|
->andThrow(new \RuntimeException('Service unavailable'));
|
|
|
|
// 2. Assert Expectation
|
|
$this->expectException(\RuntimeException::class);
|
|
$this->expectExceptionMessage('Service unavailable');
|
|
|
|
// 3. Act
|
|
$this->service->process('invalid_trigger');
|
|
}
|
|
}
|