Overview
FacturaScripts uses PHPUnit for automated testing. The test suite covers core functionality, models, controllers, and utilities.Test Structure
Tests are organized in theTest/ directory:
PHPUnit Configuration
Thephpunit.xml file configures the test runner:
Running Tests
Run All Tests
Run Specific Test Suite
Run with Verbose Output
Writing Tests
Basic Test Structure
Test Database Operations
Test/Core/DbQueryTest.php (excerpt):Test Models
Test Translations
Test/Core/TranslatorTest.php (excerpt):Test Traits
Reusable test functionality: Test/Traits/LogErrorsTrait.php:Testing Best Practices
Arrange-Act-Assert pattern
Arrange-Act-Assert pattern
Structure tests clearly:
Test one thing at a time
Test one thing at a time
Each test should verify a single behavior:
Use descriptive test names
Use descriptive test names
Test names should describe what is being tested:
Clean up test data
Clean up test data
Remove test data after tests:
Skip tests when dependencies are missing
Skip tests when dependencies are missing
Gracefully handle missing tables or configuration:
Use data providers for multiple scenarios
Use data providers for multiple scenarios
Test multiple inputs efficiently:
Common Assertions
Equality Assertions
Boolean Assertions
Array Assertions
String Assertions
Exception Assertions
Testing Plugin Code
Create tests for your plugin: Plugins/MyPlugin/Test/MyFeatureTest.php:Continuous Integration
Integrate tests into your CI/CD pipeline: .github/workflows/tests.yml:Reference
PHPUnit Commands
| Command | Description |
|---|---|
phpunit | Run all tests |
phpunit --filter testName | Run specific test |
phpunit --testdox | Verbose output |
phpunit --coverage-html coverage/ | Generate coverage report |
phpunit --stop-on-failure | Stop on first failure |
Common Assertions
| Assertion | Description |
|---|---|
assertEquals($expected, $actual) | Values are equal |
assertTrue($condition) | Condition is true |
assertFalse($condition) | Condition is false |
assertNull($value) | Value is null |
assertEmpty($value) | Value is empty |
assertCount($n, $array) | Array has n items |
assertArrayHasKey($key, $array) | Array has key |
assertContains($needle, $haystack) | Array contains value |
assertStringContainsString($needle, $haystack) | String contains substring |
expectException($class) | Exception will be thrown |

