๐ ๐งช TuskLang PHP Testing and Debugging Guide
๐งช TuskLang PHP Testing and Debugging Guide
"We don't bow to any king" - PHP Edition
Master TuskLang's testing and debugging capabilities in PHP! This guide covers unit testing, integration testing, debugging tools, and testing patterns that will ensure your applications are robust, reliable, and maintainable.
๐ฏ Testing Overview
TuskLang provides comprehensive testing and debugging features that transform your configuration from a potential source of bugs into a thoroughly tested, reliable system. This guide shows you how to implement enterprise-grade testing while maintaining TuskLang's power.
<?php
// config/testing-overview.tsk
[testing_features]
unit_tests: @test.unit("config_parser", @request.test_data)
integration_tests: @test.integration("database_queries", @request.test_scenarios)
debug_mode: @debug.enable(@env("DEBUG_MODE", "false"))
performance_tests: @test.performance("query_execution", @request.benchmark_data)
๐งช Unit Testing
Basic Unit Tests
<?php
// config/unit-tests.tsk
[parser_tests]
Test TuskLang parser functionality
test_basic_parsing: @test.unit("parser", {
"input": "name: John\nage: 30",
"expected": {"name": "John", "age": 30},
"description": "Basic key-value parsing"
})test_array_parsing: @test.unit("parser", {
"input": "colors: [red, green, blue]",
"expected": {"colors": ["red", "green", "blue"]},
"description": "Array parsing"
})
test_nested_parsing: @test.unit("parser", {
"input": "[user]\nname: John\n[user.profile]\nage: 30",
"expected": {"user": {"name": "John", "profile": {"age": 30}}},
"description": "Nested section parsing"
})
[operator_tests]
Test @ operator functionality
test_env_operator: @test.unit("env_operator", {
"input": "@env('TEST_VAR', 'default')",
"expected": "default",
"setup": {"env": {"TEST_VAR": null}},
"description": "Environment variable operator with default"
})test_query_operator: @test.unit("query_operator", {
"input": "@query('SELECT COUNT(*) FROM users')",
"expected": 0,
"setup": {"database": "test_db"},
"description": "Database query operator"
})
Advanced Unit Tests
<?php
// config/advanced-unit-tests.tsk
[complex_tests]
Test complex scenarios
test_conditional_logic: @test.unit("conditional_logic", {
"input": "debug: @if(@env('DEBUG') == 'true', true, false)",
"scenarios": [
{"env": {"DEBUG": "true"}, "expected": true},
{"env": {"DEBUG": "false"}, "expected": false},
{"env": {"DEBUG": null}, "expected": false}
],
"description": "Conditional logic with environment variables"
})test_custom_operators: @test.unit("custom_operators", {
"input": "@custom_operator('test_value', {'option': 'value'})",
"expected": "processed_test_value",
"setup": {"custom_operators": {"custom_operator": "test_handler"}},
"description": "Custom operator functionality"
})
[error_handling_tests]
Test error handling
test_invalid_syntax: @test.unit("error_handling", {
"input": "invalid: syntax: here",
"expected_error": "SyntaxError",
"description": "Invalid syntax handling"
})test_missing_operator: @test.unit("error_handling", {
"input": "@nonexistent_operator()",
"expected_error": "OperatorNotFound",
"description": "Missing operator handling"
})
๐ Integration Testing
Database Integration Tests
<?php
// config/integration-tests.tsk
[database_tests]
Test database integration
test_query_execution: @test.integration("database", {
"setup": {
"tables": [
"CREATE TABLE users (id INT, name VARCHAR(100))",
"INSERT INTO users VALUES (1, 'John'), (2, 'Jane')"
]
},
"tests": [
{
"query": "@query('SELECT COUNT(*) FROM users')",
"expected": 2
},
{
"query": "@query('SELECT name FROM users WHERE id = ?', 1)",
"expected": "John"
}
],
"cleanup": ["DROP TABLE users"]
})test_transaction_handling: @test.integration("database", {
"setup": {"tables": ["CREATE TABLE accounts (id INT, balance DECIMAL(10,2))"]},
"tests": [
{
"transaction": [
"INSERT INTO accounts VALUES (1, 100.00)",
"UPDATE accounts SET balance = balance - 50 WHERE id = 1",
"@query('SELECT balance FROM accounts WHERE id = 1')"
],
"expected": 50.00
}
],
"cleanup": ["DROP TABLE accounts"]
})
Cache Integration Tests
<?php
// config/cache-integration-tests.tsk
[cache_tests]
Test cache integration
test_cache_operations: @test.integration("cache", {
"setup": {"cache_backend": "redis"},
"tests": [
{
"operation": "@cache('5m', 'test_value')",
"expected": "test_value"
},
{
"operation": "@cache.get('test_key')",
"expected": "cached_value"
},
{
"operation": "@cache.invalidate('test_key')",
"expected": true
}
]
})test_cache_invalidation: @test.integration("cache", {
"setup": {"cache_backend": "redis"},
"tests": [
{
"setup": "@cache('1h', 'initial_value')",
"operation": "@cache.invalidate_pattern('test_*')",
"verification": "@cache.get('test_key')",
"expected": null
}
]
})
Environment Integration Tests
<?php
// config/environment-integration-tests.tsk
[environment_tests]
Test environment variable integration
test_env_loading: @test.integration("environment", {
"setup": {
"env_vars": {
"APP_NAME": "TestApp",
"APP_ENV": "testing",
"DEBUG": "true"
}
},
"tests": [
{
"config": "app_name: @env('APP_NAME')\nenvironment: @env('APP_ENV')",
"expected": {
"app_name": "TestApp",
"environment": "testing"
}
}
]
})
๐ Debugging Tools
Debug Mode Configuration
<?php
// config/debug-configuration.tsk
[debug_settings]
Debug mode configuration
debug_enabled: @debug.enable(@env("DEBUG_MODE", "false"))
debug_level: @debug.level(@env("DEBUG_LEVEL", "info"))
debug_output: @debug.output(@env("DEBUG_OUTPUT", "log"))[debug_features]
Debug features
query_debugging: @debug.queries(@env("DEBUG_QUERIES", "false"))
cache_debugging: @debug.cache(@env("DEBUG_CACHE", "false"))
operator_debugging: @debug.operators(@env("DEBUG_OPERATORS", "false"))
performance_debugging: @debug.performance(@env("DEBUG_PERFORMANCE", "false"))
Debug Logging
<?php
// config/debug-logging.tsk
[debug_logging]
Debug logging configuration
log_levels: @debug.log_levels({
"error": true,
"warning": true,
"info": @env("DEBUG_INFO", "false"),
"debug": @env("DEBUG_DEBUG", "false"),
"trace": @env("DEBUG_TRACE", "false")
})log_outputs: @debug.log_outputs({
"file": "/var/log/tusklang/debug.log",
"console": @env("DEBUG_CONSOLE", "false"),
"syslog": @env("DEBUG_SYSLOG", "false")
})
[debug_context]
Debug context information
context_info: @debug.context({
"request_id": @request.id,
"user_id": @request.user_id,
"timestamp": @date.now(),
"memory_usage": @memory.usage()
})
Interactive Debugging
<?php
// config/interactive-debugging.tsk
[interactive_debug]
Interactive debugging features
debug_breakpoint: @debug.breakpoint(@env("DEBUG_BREAKPOINT", "false"))
debug_inspector: @debug.inspector({
"variables": true,
"call_stack": true,
"memory_usage": true,
"query_log": true
})[debug_console]
Debug console
console_enabled: @debug.console(@env("DEBUG_CONSOLE", "false"))
console_commands: @debug.console_commands([
"help",
"variables",
"queries",
"cache",
"memory"
])
๐ Performance Testing
Benchmark Tests
<?php
// config/performance-tests.tsk
[benchmark_tests]
Performance benchmark tests
parser_benchmark: @test.performance("parser", {
"iterations": 1000,
"input": "large_config_file.tsk",
"metrics": ["execution_time", "memory_usage", "cpu_usage"]
})query_benchmark: @test.performance("database_queries", {
"iterations": 100,
"queries": [
"@query('SELECT COUNT(*) FROM users')",
"@query('SELECT * FROM users WHERE active = 1')",
"@query('SELECT * FROM orders WHERE user_id = ?', 1)"
],
"metrics": ["query_time", "connection_time", "result_size"]
})
cache_benchmark: @test.performance("cache_operations", {
"iterations": 10000,
"operations": [
"@cache('1h', 'test_value')",
"@cache.get('test_key')",
"@cache.invalidate('test_key')"
],
"metrics": ["operation_time", "hit_rate", "memory_usage"]
})
Load Testing
<?php
// config/load-tests.tsk
[load_tests]
Load testing configuration
concurrent_users: @test.load({
"users": 100,
"duration": "5m",
"ramp_up": "1m",
"scenarios": [
"parse_config_file",
"execute_database_query",
"cache_operation"
]
})stress_test: @test.stress({
"max_users": 1000,
"duration": "10m",
"failure_threshold": "5%",
"metrics": ["response_time", "error_rate", "throughput"]
})
๐ Testing Patterns
Test Data Management
<?php
// config/test-data-management.tsk
[test_data]
Test data management
fixtures: @test.fixtures({
"users": [
{"id": 1, "name": "John", "email": "john@example.com"},
{"id": 2, "name": "Jane", "email": "jane@example.com"}
],
"orders": [
{"id": 1, "user_id": 1, "amount": 100.00},
{"id": 2, "user_id": 2, "amount": 200.00}
]
})test_scenarios: @test.scenarios({
"happy_path": {
"input": "valid_config.tsk",
"expected": "success_result"
},
"error_path": {
"input": "invalid_config.tsk",
"expected": "error_result"
},
"edge_case": {
"input": "edge_case_config.tsk",
"expected": "edge_case_result"
}
})
Test Automation
<?php
// config/test-automation.tsk
[automation]
Test automation configuration
auto_run_tests: @test.auto_run({
"on_change": true,
"on_commit": true,
"on_deploy": true,
"parallel": true
})test_scheduling: @test.schedule({
"unit_tests": "every_commit",
"integration_tests": "daily",
"performance_tests": "weekly",
"load_tests": "monthly"
})
๐ ๏ธ Testing Tools
Test Runners
<?php
// config/test-runners.tsk
[test_runners]
Test runner configuration
phpunit_integration: @test.runner("phpunit", {
"config_file": "phpunit.xml",
"test_suite": "integration",
"coverage": true,
"parallel": true
})pest_integration: @test.runner("pest", {
"config_file": "Pest.php",
"test_suite": "integration",
"coverage": true,
"parallel": true
})
custom_runner: @test.runner("custom", {
"script": "run_tests.php",
"arguments": ["--verbose", "--coverage"],
"timeout": 300
})
Coverage Analysis
<?php
// config/coverage-analysis.tsk
[coverage]
Code coverage analysis
coverage_enabled: @test.coverage(@env("COVERAGE_ENABLED", "true"))
coverage_threshold: @test.coverage_threshold({
"statements": 80,
"branches": 70,
"functions": 80,
"lines": 80
})coverage_reports: @test.coverage_reports({
"html": "coverage/html",
"xml": "coverage/coverage.xml",
"text": "coverage/coverage.txt"
})
๐ง Debugging Configuration
Environment-Specific Debugging
<?php
// config/environment-debugging.tsk
[development]
Development debugging settings
debug_enabled: true
debug_level: "debug"
debug_output: "console"
query_debugging: true
cache_debugging: true[staging]
Staging debugging settings
debug_enabled: true
debug_level: "info"
debug_output: "log"
query_debugging: false
cache_debugging: false[production]
Production debugging settings
debug_enabled: false
debug_level: "error"
debug_output: "syslog"
query_debugging: false
cache_debugging: false
Debug Tools Integration
<?php
// config/debug-tools-integration.tsk
[tools_integration]
Debug tools integration
xdebug_integration: @debug.xdebug({
"enabled": @env("XDEBUG_ENABLED", "false"),
"port": @env("XDEBUG_PORT", "9003"),
"ide_key": @env("XDEBUG_IDE_KEY", "VSCODE")
})blackfire_integration: @debug.blackfire({
"enabled": @env("BLACKFIRE_ENABLED", "false"),
"client_id": @env("BLACKFIRE_CLIENT_ID"),
"client_token": @env("BLACKFIRE_CLIENT_TOKEN")
})
tideways_integration: @debug.tideways({
"enabled": @env("TIDEWAYS_ENABLED", "false"),
"api_key": @env("TIDEWAYS_API_KEY")
})
๐ Best Practices
Testing Best Practices
<?php
// config/testing-best-practices.tsk
[best_practices]
Testing best practices
test_isolation: @test.isolation({
"database": "separate_test_db",
"cache": "separate_test_cache",
"filesystem": "separate_test_dir"
})test_naming: @test.naming({
"pattern": "test_{functionality}_{scenario}",
"description": "Should {expected_behavior} when {condition}"
})
test_organization: @test.organization({
"unit_tests": "tests/unit",
"integration_tests": "tests/integration",
"performance_tests": "tests/performance"
})
Debugging Best Practices
<?php
// config/debugging-best-practices.tsk
[debugging_practices]
Debugging best practices
log_rotation: @debug.log_rotation({
"max_size": "100MB",
"max_files": 10,
"compress": true
})error_reporting: @debug.error_reporting({
"display_errors": @env("DISPLAY_ERRORS", "false"),
"log_errors": @env("LOG_ERRORS", "true"),
"error_log": "/var/log/tusklang/errors.log"
})
memory_management: @debug.memory_management({
"track_usage": true,
"leak_detection": @env("LEAK_DETECTION", "false"),
"gc_collection": true
})
๐ Next Steps
Now that you've mastered TuskLang's testing and debugging features in PHP, explore:
1. Advanced Testing Strategies - Implement sophisticated testing patterns 2. Continuous Integration - Set up automated testing pipelines 3. Performance Profiling - Master performance analysis tools 4. Debugging Techniques - Advanced debugging methodologies 5. Test-Driven Development - Implement TDD with TuskLang
๐ Need Help?
- Documentation: https://tuskt.sk/documents/php/testing - Examples: https://github.com/tusklang/php-examples - Community: https://community.tuskt.sk
---
Ready to test and debug your PHP applications with TuskLang? You're now a TuskLang testing and debugging master! ๐