Найкращі методи тестування PHPUnit
Найкращі методи тестування PHPUnit у XOOPS
Section titled “Найкращі методи тестування PHPUnit у XOOPS”Тестування має важливе значення для забезпечення якості коду, запобігання регресії та забезпечення впевненого рефакторинга.
Встановлення PHPUnit
Section titled “Встановлення PHPUnit”# Using Composercomposer require --dev phpunit/phpunit ^9.0
# Run tests./vendor/bin/phpunitКонфігурація phpunit.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']); }}?>Покриття коду
Section titled “Покриття коду”# Generate coverage report./vendor/bin/phpunit --coverage-html coverage
# View coverage percentage./vendor/bin/phpunit --coverage-textНайкращі практики
Section titled “Найкращі практики”- Напишіть один тест на method/scenario
- Використовуйте описові назви тестів
- Дотримуйтесь шаблону «Упорядкувати-Дій-Затвердити».
- Імітація зовнішніх залежностей
- Тримайте тести зосередженими та незалежними
- Прагніть до 80%+ покриття коду
- Умови помилки тесту
- Перевірка граничних випадків
Організація тестування
Section titled “Організація тестування”tests/├── unit/│ ├── UserServiceTest.php│ ├── UserRepositoryTest.php│ └── UserDTOTest.php├── integration/│ ├── UserControllerTest.php│ └── UserServiceTest.php├── fixtures/│ └── users.php├── bootstrap.php└── phpunit.xmlПов’язана документація
Section titled “Пов’язана документація”Дивіться також:
- Обробка помилок для тестування винятків
- ../Patterns/Repository-Pattern для тестування сховища
- ../Patterns/Service-Layer для тестування служби
- Організація коду для структури тесту
Теги: #best-practices #testing #phpunit #code-coverage #module-development