我有一个 PHPUnit 测试,它使用正则表达式验证日志文件的输出。日志文件看起来像这样(第一行是空的,但我不知道如何在此处显示):
<empty line>
lfworker01:
-----------------------------------------------------------
Last update: 2012-06-14 11:43:17
Last Segment Sent: 2009-12-02 23:25:00 (1259792700)
Current Segement: 2009-12-03 00:25:00 (1259796300)
Clicks processed Segment: 3
Open sessions Segment: 1
Duration Segment: 0,06 sec
Speed Segment: 47,67 clicks/sec
Uptime: 0 days 00:00:00
Clicks processed overall: 3
Avg Speed overall: 81,70 clicks/sec
Current memory used: 16,75 MB
Max memory used: 16,75 MB
我正在使用正则表达式验证内容:
$strExpectedMeasurementLog = "#
lfworker01:
-----------------------------------------------------------
Last update: \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}
Last Segment Sent: 2009-12-02 23:25:00 \(1259792700\)
Current Segement: 2009-12-03 00:25:00 \(1259796300\)
Clicks processed Segment: 3
Open sessions Segment: 1
Duration Segment: \d*,\d{2} sec
Speed Segment: \d*,\d{2} clicks/sec
Uptime: 0 days 00:00:00
Clicks processed overall: 3
Avg Speed overall: \d*,\d{2} clicks/sec
Current memory used: \d*,\d{2} MB
Max memory used: \d*,\d{2} MB#";
$strActualMeasurementLog = file_get_contents( dirname( __FILE__)."/applogs/measurement.log");
self::assertRegExp( $strExpectedMeasurementLog, $strActualMeasurementLog);
PHPUnit 测试是在 Unix 环境中创建的。此测试在 unix 测试环境中通过,但在 windows 测试环境中失败。通过将此函数应用于 measure.log 文件,我已将 windows 换行符替换为 unix 换行符:
public function unixtodos( $strPathFile )
{
$strCurrent = file_get_contents( $strPathFile );
$strPattern = "|\r\n|";
$strReplace = "\n";
$strNew = preg_replace( $strPattern, $strReplace, $strCurrent );
file_put_contents( $strPathFile, $strNew );
}
它仍然不匹配,我没有想法:(