PHPUnit 테스트 모범 사례
XOOPS의 PHPUnit 테스트 모범 사례
섹션 제목: “XOOPS의 PHPUnit 테스트 모범 사례”테스트는 코드 품질을 보장하고, 회귀를 방지하며, 자신감 있는 리팩토링을 활성화하는 데 필수적입니다.
PHPUnit 설치
섹션 제목: “PHPUnit 설치”# Using Composercomposer require --dev phpunit/phpunit ^9.0
# Run tests./vendor/bin/phpunitphpunit.xml 구성
섹션 제목: “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>단위 테스트 작성
섹션 제목: “단위 테스트 작성”<?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'); }}?>데이터 개체 테스트
섹션 제목: “데이터 개체 테스트”<?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모범 사례
섹션 제목: “모범 사례”- 방법/시나리오당 하나의 테스트 작성
- 설명이 포함된 테스트 이름을 사용하세요.
- 배열-행위-어설션 패턴을 따릅니다.
- 모의 외부 의존성
- 테스트를 집중적이고 독립적으로 유지
- 80% 이상의 코드 커버리지를 목표로 합니다.
- 테스트 오류 조건
- 테스트 경계 사례
테스트 조직
섹션 제목: “테스트 조직”tests/├── unit/│ ├── UserServiceTest.php│ ├── UserRepositoryTest.php│ └── UserDTOTest.php├── integration/│ ├── UserControllerTest.php│ └── UserServiceTest.php├── fixtures/│ └── users.php├── bootstrap.php└── phpunit.xml관련 문서
섹션 제목: “관련 문서”참조:
- 예외 테스트를 위한 오류 처리
- 저장소 테스트를 위한../Patterns/Repository-Pattern
- 서비스 테스트를 위한../Patterns/Service-Layer
- 테스트 구조에 대한 코드 구성
태그: #모범 사례 #테스트 #phpunit #코드 적용 범위 #모듈 개발