Boas Práticas de Testes PHPUnit
Boas Práticas de Testes PHPUnit em XOOPS
Seção intitulada “Boas Práticas de Testes PHPUnit em XOOPS”Testes são essenciais para garantir qualidade de código, prevenir regressões e permitir refatoração confiante.
Instalando PHPUnit
Seção intitulada “Instalando PHPUnit”# Usando Composercomposer require --dev phpunit/phpunit ^9.0
# Executar testes./vendor/bin/phpunitConfiguração phpunit.xml
Seção intitulada “Configuração 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>Escrevendo Testes Unitários
Seção intitulada “Escrevendo Testes Unitários”<?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'); }}?>Testes de Objetos de Dados
Seção intitulada “Testes de Objetos de Dados”<?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']); }}?>Cobertura de Código
Seção intitulada “Cobertura de Código”# Gerar relatório de cobertura./vendor/bin/phpunit --coverage-html coverage
# Ver percentagem de cobertura./vendor/bin/phpunit --coverage-textBoas Práticas
Seção intitulada “Boas Práticas”- Escrever um teste por método/cenário
- Usar nomes de teste descritivos
- Seguir padrão Arrange-Act-Assert
- Mockar dependências externas
- Manter testes focados e independentes
- Almejar cobertura 80%+
- Testar condições de erro
- Testar casos limites
Organização de Testes
Seção intitulada “Organização de Testes”tests/├── unit/│ ├── UserServiceTest.php│ ├── UserRepositoryTest.php│ └── UserDTOTest.php├── integration/│ ├── UserControllerTest.php│ └── UserServiceTest.php├── fixtures/│ └── users.php├── bootstrap.php└── phpunit.xmlDocumentação Relacionada
Seção intitulada “Documentação Relacionada”Veja também:
- Tratamento-de-Erros para teste de exceção
- ../Padrões/Padrão-Repository para teste de repositório
- ../Padrões/Camada-de-Serviço para teste de serviço
- Organização-de-Código para estrutura de teste
Tags: #boas-práticas #testes #phpunit #cobertura-de-código #desenvolvimento-de-módulo