“PHPUnit 测试最佳实践”
XOOPS 中的 PHPUnit 测试最佳实践
Section titled “XOOPS 中的 PHPUnit 测试最佳实践”测试对于确保代码质量、防止回归和实现自信的重构至关重要。
安装 PHPUnit
Section titled “安装 PHPUnit”# Using Composercomposer require --dev phpunit/phpunit ^9.0
# Run tests./vendor/bin/phpunitphpunit.xml 配置
Section titled “phpunit.xml 配置”<?xml version="1.0" encoding="UTF-8"?><phpunit bootstrap="tests/bootstrap.php" colors="true" verbose="true"> <testsuites> <testsuite name="Unit"> <directory>tests/unit</directory> </testsuite> <testsuite name="Integration"> <directory>tests/integration</directory> </testsuite> </testsuites>
<coverage processUncoveredFiles="true"> <include> <directory suffix=".php">class</directory> </include> <report> <html outputDirectory="coverage"/> </report> </coverage></phpunit>编写单元测试
Section titled “编写单元测试”<?phpnamespace Xoops\Module\Mymodule\Tests\Unit;
use PHPUnit\Framework\TestCase;use Xoops\Module\Mymodule\Service\UserService;
class UserServiceTest extends TestCase{ private $userService; private $mockRepository;
protected function setUp(): void { parent::setUp(); $this->mockRepository = $this->createMock( \Xoops\Module\Mymodule\Repository\UserRepositoryInterface::class ); $this->userService = new UserService($this->mockRepository); }
public function testRegisterSuccess() { // Arrange $this->mockRepository->expects($this->once()) ->method('findByUsername') ->willReturn(null);
$this->mockRepository->expects($this->once()) ->method('save') ->willReturn(1);
// Act $result = $this->userService->register('user', 'test@test.com', 'pass');
// Assert $this->assertNotNull($result); }
public function testRegisterDuplicate() { // Arrange $existingUser = new \stdClass(); $this->mockRepository->expects($this->once()) ->method('findByUsername') ->willReturn($existingUser);
// Act & Assert $this->expectException(\Exception::class); $this->userService->register('user', 'test@test.com', 'pass'); }}?>测试数据对象
Section titled “测试数据对象”<?phpclass UserDTOTest extends TestCase{ public function testDTOCreation() { $user = new User(); $user->setId(1) ->setUsername('testuser') ->setEmail('test@test.com');
$dto = new UserDTO($user);
$this->assertEquals(1, $dto->getId()); $this->assertEquals('testuser', $dto->getUsername()); }
public function testDTOToArray() { $user = new User(); $user->setId(1)->setUsername('testuser');
$dto = new UserDTO($user); $array = $dto->toArray();
$this->assertIsArray($array); $this->assertEquals(1, $array['id']); }}?># Generate coverage report./vendor/bin/phpunit --coverage-html coverage
# View coverage percentage./vendor/bin/phpunit --coverage-text- 根据 method/scenario 编写一项测试
- 使用描述性测试名称
- 遵循排列-Act-Assert模式
- 模拟外部依赖项
- 保持测试的重点和独立性
- 目标是 80% 以上的代码覆盖率
- 测试错误条件
- 测试边界情况
tests/├── unit/│ ├── UserServiceTest.php│ ├── UserRepositoryTest.php│ └── UserDTOTest.php├── integration/│ ├── UserControllerTest.php│ └── UserServiceTest.php├── fixtures/│ └── users.php├── bootstrap.php└── phpunit.xml另请参阅:
- 异常测试时出现错误-Handling
- ../Patterns/Repository-Pattern用于存储库测试
- ../Patterns/Service-Layer用于服务测试
- 测试结构代码-Organization
标签:#best-practices #testing #phpunit #code-coverage #module-development