Migliori Pratiche di Testing PHPUnit
Migliori Pratiche di Testing PHPUnit in XOOPS
Sezione intitolata “Migliori Pratiche di Testing PHPUnit in XOOPS”Il testing è essenziale per garantire la qualità del codice, prevenire regressioni e abilitare refactoring confidente.
Installazione di PHPUnit
Sezione intitolata “Installazione di PHPUnit”# Using Composercomposer require --dev phpunit/phpunit ^9.0
# Run tests./vendor/bin/phpunitConfigurazione phpunit.xml
Sezione intitolata “Configurazione 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>Scrittura di Unit Test
Sezione intitolata “Scrittura di Unit Test”<?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'); }}?>Testing di Oggetti di Dati
Sezione intitolata “Testing di Oggetti di Dati”<?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']); }}?>Code Coverage
Sezione intitolata “Code Coverage”# Generate coverage report./vendor/bin/phpunit --coverage-html coverage
# View coverage percentage./vendor/bin/phpunit --coverage-textMigliori Pratiche
Sezione intitolata “Migliori Pratiche”- Scrivi un test per metodo/scenario
- Usa nomi di test descrittivi
- Segui il pattern Arrange-Act-Assert
- Mock le dipendenze esterne
- Mantieni i test focalizzati e indipendenti
- Mira a 80%+ code coverage
- Testa le condizioni di errore
- Testa i casi limite
Organizzazione dei Test
Sezione intitolata “Organizzazione dei Test”tests/├── unit/│ ├── UserServiceTest.php│ ├── UserRepositoryTest.php│ └── UserDTOTest.php├── integration/│ ├── UserControllerTest.php│ └── UserServiceTest.php├── fixtures/│ └── users.php├── bootstrap.php└── phpunit.xmlDocumentazione Correlata
Sezione intitolata “Documentazione Correlata”Vedi anche:
- Error-Handling per il testing delle eccezioni
- ../Patterns/Repository-Pattern per il testing del repository
- ../Patterns/Service-Layer per il testing del service
- Code-Organization per la struttura dei test
Tags: #best-practices #testing #phpunit #code-coverage #module-development