--- 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.