/** * Descomplicar® Crescimento Digital * https://descomplicar.pt */ 'https://www.moloni.pt', 'token_url' => 'https://api.moloni.pt' ]; if (file_exists($oauth_file)) { $content = file_get_contents($oauth_file); $endpoints_found = 0; foreach ($expected_endpoints as $endpoint => $domain) { if (strpos($content, $domain) !== false) { echo " ✅ {$endpoint} contains correct domain: {$domain}\n"; $endpoints_found++; } else { echo " ❌ {$endpoint} missing or incorrect domain\n"; } } $test_results['endpoints_configured'] = ($endpoints_found === count($expected_endpoints)); } else { echo " ❌ Cannot test endpoints - file does not exist\n"; $test_results['endpoints_configured'] = false; } // Test 4: Security Features Contract echo "\n4. 🧪 Testing Security Features Contract...\n"; $security_features = [ 'PKCE' => ['pkce', 'code_verifier', 'code_challenge'], 'State validation' => ['state', 'csrf'], 'Token encryption' => ['encrypt', 'decrypt', 'token_manager'], 'Rate limiting' => ['rate_limit', 'throttle', 'request_count'] ]; if (file_exists($oauth_file)) { $content = file_get_contents($oauth_file); $security_score = 0; foreach ($security_features as $feature => $keywords) { $feature_found = false; foreach ($keywords as $keyword) { if (stripos($content, $keyword) !== false) { $feature_found = true; break; } } if ($feature_found) { echo " ✅ {$feature} implementation found\n"; $security_score++; } else { echo " ❌ {$feature} implementation missing\n"; } } $test_results['security_features'] = ($security_score >= 3); echo " 📊 Security features: {$security_score}/" . count($security_features) . "\n"; } else { echo " ❌ Cannot test security features - file does not exist\n"; $test_results['security_features'] = false; } // Test 5: Database Integration Contract echo "\n5. 🧪 Testing Database Integration Contract...\n"; $config_model_file = __DIR__ . '/../../models/Desk_moloni_config_model.php'; if (file_exists($config_model_file)) { echo " ✅ Config model exists for OAuth storage\n"; $content = file_get_contents($config_model_file); if (strpos($content, 'oauth') !== false) { echo " ✅ Config model supports OAuth settings\n"; $test_results['database_integration'] = true; } else { echo " ⚠️ Config model may not support OAuth settings\n"; $test_results['database_integration'] = false; } } else { echo " ❌ Config model missing for OAuth storage\n"; $test_results['database_integration'] = false; } // Test 6: Token Manager Integration echo "\n6. 🧪 Testing Token Manager Integration...\n"; $token_manager_file = __DIR__ . '/../../libraries/TokenManager.php'; if (file_exists($token_manager_file)) { echo " ✅ TokenManager library exists\n"; $content = file_get_contents($token_manager_file); if (strpos($content, 'save_tokens') !== false || strpos($content, 'get_token') !== false) { echo " ✅ TokenManager has token management methods\n"; $test_results['token_manager_integration'] = true; } else { echo " ❌ TokenManager missing required methods\n"; $test_results['token_manager_integration'] = false; } } else { echo " ❌ TokenManager library missing\n"; $test_results['token_manager_integration'] = false; } // Generate Final Report $execution_time = microtime(true) - $start_time; echo "\n" . str_repeat("=", 80) . "\n"; echo "MOLONI OAUTH CONTRACT TEST REPORT\n"; echo str_repeat("=", 80) . "\n"; $passed_tests = array_filter($test_results, function($result) { return $result === true; }); $failed_tests = array_filter($test_results, function($result) { return $result === false; }); echo "Execution Time: " . number_format($execution_time, 2) . "s\n"; echo "Tests Passed: " . count($passed_tests) . "\n"; echo "Tests Failed: " . count($failed_tests) . " (EXPECTED in TDD)\n"; if (count($failed_tests) > 0) { echo "\n🔴 TDD STATUS: TESTS FAILING AS EXPECTED\n"; echo "Next Step: Implement Moloni_oauth library to make tests pass\n"; echo "\nFailed Test Categories:\n"; foreach ($test_results as $test => $result) { if ($result === false) { echo " ❌ " . ucwords(str_replace('_', ' ', $test)) . "\n"; } } } else { echo "\n🟢 ALL TESTS PASSING\n"; echo "OAuth implementation appears to be complete\n"; } echo "\n📋 IMPLEMENTATION REQUIREMENTS:\n"; echo " 1. Create libraries/Moloni_oauth.php with class Moloni_oauth\n"; echo " 2. Implement all required methods listed above\n"; echo " 3. Support OAuth 2.0 with PKCE for security\n"; echo " 4. Integrate with TokenManager for secure storage\n"; echo " 5. Use Config model for persistent settings\n"; echo " 6. Implement comprehensive error handling\n"; echo " 7. Add rate limiting and security features\n"; echo "\n🎯 SUCCESS CRITERIA:\n"; echo " - All contract tests must pass\n"; echo " - OAuth flow must work with real Moloni API\n"; echo " - Tokens must be securely encrypted\n"; echo " - PKCE must be implemented for security\n"; echo " - Proper error handling and logging\n"; // Save results $reports_dir = __DIR__ . '/../reports'; if (!is_dir($reports_dir)) { mkdir($reports_dir, 0755, true); } $report_file = $reports_dir . '/oauth_contract_test_' . date('Y-m-d_H-i-s') . '.json'; file_put_contents($report_file, json_encode([ 'timestamp' => date('Y-m-d H:i:s'), 'test_type' => 'oauth_contract_standalone', 'status' => count($failed_tests) > 0 ? 'failing' : 'passing', 'results' => $test_results, 'execution_time' => $execution_time, 'tdd_status' => 'Tests failing as expected - ready for implementation' ], JSON_PRETTY_PRINT)); echo "\n📄 Contract test results saved to: {$report_file}\n"; echo str_repeat("=", 80) . "\n";