263 lines
8.7 KiB
PHP
263 lines
8.7 KiB
PHP
<?php
|
||
/**
|
||
* 华为支付证书内容格式化测试
|
||
*/
|
||
|
||
/**
|
||
* 直接测试证书内容格式化逻辑
|
||
*/
|
||
class CertificateFormatTest
|
||
{
|
||
/**
|
||
* 测试配置数据
|
||
* @var array
|
||
*/
|
||
protected $configData;
|
||
|
||
/**
|
||
* 构造函数,加载配置文件
|
||
*/
|
||
public function __construct()
|
||
{
|
||
// 加载配置文件
|
||
$yamlPath = __DIR__ . '/mock/data.yml';
|
||
if (file_exists($yamlPath)) {
|
||
if (function_exists('yaml_parse_file')) {
|
||
$this->configData = yaml_parse_file($yamlPath);
|
||
} else {
|
||
// 如果没有yaml_parse_file函数,手动解析
|
||
$this->configData = $this->parseYamlFile($yamlPath);
|
||
}
|
||
} else {
|
||
throw new Exception("配置文件不存在: {$yamlPath}");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 手动解析YAML文件
|
||
* @param string $filePath
|
||
* @return array
|
||
*/
|
||
private function parseYamlFile($filePath)
|
||
{
|
||
$content = file_get_contents($filePath);
|
||
$lines = explode("\n", $content);
|
||
$result = [];
|
||
$currentKey = '';
|
||
$isMultiline = false;
|
||
$multilineValue = '';
|
||
|
||
foreach ($lines as $line) {
|
||
$line = trim($line);
|
||
|
||
// 跳过注释和空行
|
||
if (empty($line) || strpos($line, '#') === 0) {
|
||
continue;
|
||
}
|
||
|
||
// 处理多行值
|
||
if ($isMultiline) {
|
||
if (strpos($line, '-----END') === 0) {
|
||
$multilineValue .= $line . "\n";
|
||
$result[$currentKey] = rtrim($multilineValue);
|
||
$isMultiline = false;
|
||
$multilineValue = '';
|
||
} else {
|
||
$multilineValue .= $line . "\n";
|
||
}
|
||
continue;
|
||
}
|
||
|
||
// 处理单行键值对
|
||
if (strpos($line, ':') !== false) {
|
||
list($key, $value) = explode(':', $line, 2);
|
||
$key = trim($key);
|
||
$value = trim($value);
|
||
|
||
// 处理多行值开始
|
||
if ($value === '|') {
|
||
$isMultiline = true;
|
||
$currentKey = $key;
|
||
continue;
|
||
}
|
||
|
||
// 处理普通值
|
||
if (!empty($value)) {
|
||
$result[$key] = $value;
|
||
}
|
||
}
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 模拟证书内容格式化方法
|
||
* 与HuaweiPayClient::formatCertificateContent方法实现一致
|
||
*/
|
||
private function formatCertificateContent($content, $type)
|
||
{
|
||
// 移除空白字符和换行
|
||
$content = preg_replace('/\s+/', '', $content);
|
||
|
||
// 检查是否已经包含格式头尾
|
||
if (preg_match('/-----BEGIN\s+.*?-----/', $content) && preg_match('/-----END\s+.*?-----/', $content)) {
|
||
return $content;
|
||
}
|
||
|
||
// 添加适当的格式头尾
|
||
$header = '';
|
||
$footer = '';
|
||
|
||
if ($type == 'private_key') {
|
||
$header = "-----BEGIN PRIVATE KEY-----";
|
||
$footer = "-----END PRIVATE KEY-----";
|
||
} elseif ($type == 'public_key') {
|
||
$header = "-----BEGIN PUBLIC KEY-----";
|
||
$footer = "-----END PUBLIC KEY-----";
|
||
}
|
||
|
||
// 移除可能存在的旧格式
|
||
$content = preg_replace('/-----BEGIN.*?-----/', '', $content);
|
||
$content = preg_replace('/-----END.*?-----/', '', $content);
|
||
|
||
// 组合最终格式
|
||
return $header . $content . $footer;
|
||
}
|
||
|
||
/**
|
||
* 测试证书内容格式化功能
|
||
*/
|
||
public function testFormatCertificateContent()
|
||
{
|
||
echo "开始测试证书内容格式化功能...\n\n";
|
||
|
||
// 从配置文件获取实际的密钥内容
|
||
$privateKey = $this->configData['privateKey'];
|
||
$publicKey = $this->configData['huawei_public_key'];
|
||
|
||
// 测试1:私钥格式化
|
||
echo "测试1:私钥格式化...";
|
||
$formattedPrivateKey = $this->formatCertificateContent($privateKey, 'private_key');
|
||
|
||
// 移除预期结果和实际结果中的所有换行符,统一比较
|
||
$expectedClean = str_replace("\n", '', $privateKey);
|
||
$actualClean = str_replace("\n", '', $formattedPrivateKey);
|
||
|
||
if (strpos($actualClean, '-----BEGIN PRIVATE KEY-----') !== false && strpos($actualClean, '-----END PRIVATE KEY-----') !== false) {
|
||
echo " ✅ 通过\n";
|
||
} else {
|
||
echo " ❌ 失败\n";
|
||
echo " 实际:$formattedPrivateKey\n";
|
||
return false;
|
||
}
|
||
|
||
// 测试2:公钥格式化
|
||
echo "测试2:公钥格式化...";
|
||
$formattedPublicKey = $this->formatCertificateContent($publicKey, 'public_key');
|
||
|
||
// 移除预期结果和实际结果中的所有换行符,统一比较
|
||
$expectedClean = str_replace("\n", '', $publicKey);
|
||
$actualClean = str_replace("\n", '', $formattedPublicKey);
|
||
|
||
if (strpos($actualClean, '-----BEGIN PUBLIC KEY-----') !== false && strpos($actualClean, '-----END PUBLIC KEY-----') !== false) {
|
||
echo " ✅ 通过\n";
|
||
} else {
|
||
echo " ❌ 失败\n";
|
||
echo " 实际:$formattedPublicKey\n";
|
||
return false;
|
||
}
|
||
|
||
// 测试3:带空格和换行的私钥格式化
|
||
echo "测试3:带空格和换行的私钥格式化...";
|
||
$privateKeyWithSpaces = ' ' . $privateKey . ' ';
|
||
$formattedPrivateKeyWithSpaces = $this->formatCertificateContent($privateKeyWithSpaces, 'private_key');
|
||
|
||
if (strpos($formattedPrivateKeyWithSpaces, '-----BEGIN PRIVATE KEY-----') !== false && strpos($formattedPrivateKeyWithSpaces, '-----END PRIVATE KEY-----') !== false) {
|
||
echo " ✅ 通过\n";
|
||
} else {
|
||
echo " ❌ 失败\n";
|
||
echo " 实际:$formattedPrivateKeyWithSpaces\n";
|
||
return false;
|
||
}
|
||
|
||
echo "\n✅ 所有证书内容格式化测试通过!\n";
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 测试文本模式配置结构
|
||
*/
|
||
public function testTextModeConfigStructure()
|
||
{
|
||
echo "\n开始测试文本模式配置结构...\n";
|
||
|
||
// 测试1:基本文本模式配置
|
||
echo "测试1:基本文本模式配置...";
|
||
$config1 = [
|
||
'app_id' => $this->configData['app_id'],
|
||
'mch_id' => $this->configData['mch_id'],
|
||
'private_key_text' => $this->configData['privateKey'],
|
||
'huawei_public_key_text' => $this->configData['huawei_public_key']
|
||
];
|
||
|
||
if (isset($config1['private_key_text']) && isset($config1['huawei_public_key_text'])) {
|
||
echo " ✅ 通过\n";
|
||
} else {
|
||
echo " ❌ 失败\n";
|
||
return false;
|
||
}
|
||
|
||
// 测试2:验证配置数据完整性
|
||
echo "测试2:验证配置数据完整性...";
|
||
$requiredFields = ['app_id', 'mch_id', 'mch_auth_id', 'privateKey', 'huawei_public_key'];
|
||
$missingFields = [];
|
||
|
||
foreach ($requiredFields as $field) {
|
||
if (!isset($this->configData[$field])) {
|
||
$missingFields[] = $field;
|
||
}
|
||
}
|
||
|
||
if (empty($missingFields)) {
|
||
echo " ✅ 通过\n";
|
||
} else {
|
||
echo " ❌ 失败,缺少字段:" . implode(', ', $missingFields) . "\n";
|
||
return false;
|
||
}
|
||
|
||
echo "\n✅ 所有文本模式配置结构测试通过!\n";
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 运行所有测试
|
||
*/
|
||
public function runTests()
|
||
{
|
||
echo "开始运行华为支付文本模式测试...\n\n";
|
||
|
||
try {
|
||
$formatResult = $this->testFormatCertificateContent();
|
||
$configResult = $this->testTextModeConfigStructure();
|
||
|
||
if ($formatResult && $configResult) {
|
||
echo "\n🎉 所有测试通过!华为支付文本模式功能正常工作!\n";
|
||
return true;
|
||
} else {
|
||
echo "\n💥 部分测试失败!\n";
|
||
return false;
|
||
}
|
||
} catch (Exception $e) {
|
||
echo "\n💥 测试失败:" . $e->getMessage() . "\n";
|
||
echo "错误位置:" . $e->getFile() . " 第" . $e->getLine() . "行\n";
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 如果直接运行此文件,则执行测试
|
||
if (basename(__FILE__) == basename($_SERVER['PHP_SELF'])) {
|
||
$test = new CertificateFormatTest();
|
||
$test->runTests();
|
||
} |