---
type: Document
title: Testing Setup
description: >-
  Documentação da infraestrutura de testes do plugin KiviCare/Care API: configuração PHPUnit 10.x, bootstrap WordPress e setup da base de dados de testes.
timestamp: 2025-09-12T21:10:45.485415+00:00
layer: wiki
---
# 🧪 KiviCare API Testing & Code Quality Setup

## ✅ Phase 3.1 Completed - Testing Infrastructure Ready

### 📋 **T003**: PHPUnit Configuration with WordPress Testing Framework ✅

**Configured Components:**

1. **PHPUnit 10.x Configuration** (`phpunit.xml`)
   - WordPress testing framework integration
   - Multiple test suites: Unit, Integration, Contract, Performance
   - Code coverage reporting (HTML + text)
   - Proper test environment variables
   - CI/CD ready JUnit XML output

2. **WordPress Test Bootstrap** (`tests/bootstrap.php`)
   - WordPress testing environment setup
   - Plugin activation/deactivation hooks
   - Custom test case base class (`Care_API_Test_Case`)
   - REST API server initialization
   - Test user creation (admin, doctor, patient, receptionist)

3. **Database Test Setup** (`tests/setup/test-database.php`)
   - Complete KiviCare table schema creation
   - Sample data insertion for testing
   - Cleanup procedures
   - 8 core tables: clinics, appointments, patients, doctors, services, bills, encounters, prescriptions

4. **KiviCare Mock** (`tests/mocks/mock-kivicare.php`)
   - Mock KiviCare plugin functionality for testing
   - User roles and capabilities
   - Essential helper functions
   - Constants and activation hooks

### 📋 **T004**: WordPress Coding Standards (WPCS) Setup ✅

**Configured Components:**

1. **PHPCS Configuration** (`phpcs.xml`)
   - WordPress Coding Standards (WPCS 3.0+)
   - PHP 8.1+ compatibility checks
   - PSR-4 namespace support
   - Security rules (escaping, nonces, sanitization)
   - Performance optimizations (VIP-Go standards)
   - Custom prefixes: `kivicare_api`, `KiviCare_API`, `KIVICARE_API`

2. **Code Quality Tools**
   - PHP_CodeSniffer 3.13+
   - PHPCompatibility checks
   - WordPress VIP Go standards
   - Security and performance validation

## 🛠️ Development Scripts

### Testing Scripts
- `bin/install-wp-tests.sh` - WordPress test environment setup
- `bin/run-tests.sh` - Comprehensive test runner with coverage
- `bin/code-quality.sh` - Code quality checker with auto-fix

### Composer Commands
```bash
# Quality Checks
composer run phpcs         # Check coding standards
composer run phpcbf        # Auto-fix coding standards
composer run quality       # Full quality check
composer run quality:fix   # Quality check + auto-fix

# Testing
composer run phpunit       # Run all tests
composer run test:unit     # Unit tests only
composer run test:integration # Integration tests
composer run test:contract # API contract tests
composer run test:coverage # Tests with coverage

# Setup
composer run setup:tests  # Install WordPress test environment
```

## 📊 Test Structure

```
tests/
├── bootstrap.php          # Test environment bootstrap
├── unit/                  # Unit tests
│   └── ConfigTest.php     # Configuration validation test
├── integration/           # Integration tests
├── contract/              # API contract tests  
├── performance/           # Performance tests
├── mocks/                 # Test mocks
│   └── mock-kivicare.php
├── setup/                 # Test setup utilities
│   └── test-database.php
└── _output/              # Test artifacts (JUnit, TestDox)
```

## 🔧 Configuration Files

| File | Purpose | Status |
|------|---------|---------|
| `phpunit.xml` | PHPUnit 10.x configuration | ✅ Ready |
| `phpcs.xml` | WPCS + security standards | ✅ Ready |
| `composer.json` | Dependencies + scripts | ✅ Updated |
| `tests/bootstrap.php` | WordPress test bootstrap | ✅ Ready |

## 🚀 Quick Start

1. **Install dependencies:**
   ```bash
   composer install
   ```

2. **Setup WordPress test environment:**
   ```bash
   composer run setup:tests
   ```

3. **Run code quality checks:**
   ```bash
   composer run quality:fix
   ```

4. **Run tests:**
   ```bash
   composer run test
   ```

## 📈 Technical Specifications Met

### PHPUnit Requirements ✅
- ✅ PHPUnit 10.x compatibility with PHP 8.1+
- ✅ WordPress testing framework integration
- ✅ Multiple test suites configuration
- ✅ Code coverage reporting
- ✅ CI/CD ready (JUnit XML output)
- ✅ Custom test base class with helpers

### WPCS Requirements ✅  
- ✅ WordPress Coding Standards 3.0+
- ✅ PHP 8.1+ compatibility validation
- ✅ Security rules enforcement
- ✅ Performance optimization checks
- ✅ PSR-4 namespace support
- ✅ Auto-fix capabilities

## 🐛 Troubleshooting

### WordPress Test Environment
If "Could not find WordPress test suite":
```bash
bin/install-wp-tests.sh wordpress_test root '' localhost latest
```

### PHP Extensions  
Missing extensions error:
```bash
# Install required extensions or use:
composer install --ignore-platform-reqs
```

### Database Connection
Ensure MySQL/MariaDB is running and accessible with test credentials.

---

## 🎯 Next Steps

With Phase 3.1 complete, the testing and code quality infrastructure is fully configured and ready for:

- ✅ Unit test development
- ✅ Integration test implementation  
- ✅ API contract validation
- ✅ Performance testing
- ✅ Continuous integration setup
- ✅ Code quality enforcement

The foundation is solid for professional WordPress plugin development with comprehensive testing coverage.