/** * Descomplicar® Crescimento Digital * https://descomplicar.pt */ true, 'run_integration_tests' => true, 'run_performance_tests' => true, 'generate_coverage' => false, // Set to true if XDebug is available 'output_format' => 'html', // html, json, text 'detailed_output' => true ]; // Test results storage $test_results = [ 'start_time' => microtime(true), 'total_tests' => 0, 'passed_tests' => 0, 'failed_tests' => 0, 'skipped_tests' => 0, 'test_suites' => [], 'errors' => [], 'warnings' => [], 'performance_metrics' => [], 'memory_usage' => [] ]; /** * Execute a test class and capture results */ function run_test_class($class_name, $test_file) { global $test_results; $suite_start = microtime(true); $suite_results = [ 'class' => $class_name, 'file' => $test_file, 'tests' => [], 'passed' => 0, 'failed' => 0, 'skipped' => 0, 'execution_time' => 0, 'memory_used' => 0, 'errors' => [] ]; try { // Load test file if (!file_exists($test_file)) { throw new Exception("Test file not found: {$test_file}"); } require_once $test_file; if (!class_exists($class_name)) { throw new Exception("Test class not found: {$class_name}"); } // Create test instance $test_instance = new $class_name(); $reflection = new ReflectionClass($class_name); // Get all test methods $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC); $test_methods = array_filter($methods, function($method) { return strpos($method->getName(), 'test') === 0; }); echo "Running {$class_name} (" . count($test_methods) . " tests)...\n"; foreach ($test_methods as $method) { $test_name = $method->getName(); $test_start = microtime(true); $test_memory_start = memory_get_usage(true); try { // Setup if (method_exists($test_instance, 'setUp')) { $test_instance->setUp(); } // Execute test $method->invoke($test_instance); // Test passed $suite_results['passed']++; $test_results['passed_tests']++; $status = 'PASSED'; $error = null; } catch (Exception $e) { // Test failed $suite_results['failed']++; $test_results['failed_tests']++; $status = 'FAILED'; $error = $e->getMessage(); $suite_results['errors'][] = [ 'test' => $test_name, 'error' => $error, 'trace' => $e->getTraceAsString() ]; } finally { // Teardown if (method_exists($test_instance, 'tearDown')) { try { $test_instance->tearDown(); } catch (Exception $e) { // Teardown error $test_results['warnings'][] = "Teardown error in {$test_name}: " . $e->getMessage(); } } } $test_execution_time = microtime(true) - $test_start; $test_memory_used = memory_get_usage(true) - $test_memory_start; $suite_results['tests'][] = [ 'name' => $test_name, 'status' => $status, 'execution_time' => $test_execution_time, 'memory_used' => $test_memory_used, 'error' => $error ]; $test_results['total_tests']++; // Progress output echo " {$test_name}: {$status}"; if ($test_execution_time > 1.0) { echo " (slow: " . number_format($test_execution_time, 2) . "s)"; } echo "\n"; } } catch (Exception $e) { $suite_results['errors'][] = [ 'test' => 'Suite Setup', 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]; $test_results['errors'][] = "Error in {$class_name}: " . $e->getMessage(); } $suite_results['execution_time'] = microtime(true) - $suite_start; $suite_results['memory_used'] = memory_get_peak_usage(true); $test_results['test_suites'][] = $suite_results; echo " Completed in " . number_format($suite_results['execution_time'], 2) . "s\n\n"; } /** * Generate test report */ function generate_test_report($format = 'html') { global $test_results; $test_results['end_time'] = microtime(true); $test_results['total_execution_time'] = $test_results['end_time'] - $test_results['start_time']; $test_results['peak_memory'] = memory_get_peak_usage(true); $test_results['success_rate'] = $test_results['total_tests'] > 0 ? ($test_results['passed_tests'] / $test_results['total_tests']) * 100 : 0; switch ($format) { case 'html': return generate_html_report(); case 'json': return json_encode($test_results, JSON_PRETTY_PRINT); case 'text': default: return generate_text_report(); } } /** * Generate HTML report */ function generate_html_report() { global $test_results; $html = '
Generated on: ' . date('Y-m-d H:i:s') . '
'; $html .= 'Environment: ' . (defined('ENVIRONMENT') ? ENVIRONMENT : 'development') . '
'; $html .= 'Total Tests
'; $html .= 'Passed
'; $html .= 'Failed
'; $html .= 'Success Rate
'; $html .= 'Total Execution Time: ' . number_format($test_results['total_execution_time'], 2) . ' seconds
'; $html .= 'Peak Memory Usage: ' . number_format($test_results['peak_memory'] / 1024 / 1024, 2) . ' MB
'; $html .= '