I have a PHPUnit Test which validates the output of a logfile with a regexp. The logfile looks like this(First line is empty, but I don’t know how to display that here):
<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
I am validating the content with a regex:
$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);
The PHPUnit test was created in a unix environment. This test passes in a unix test environment, but fails in a windows test environment. I have replaced windows line breaks with unix line breaks by appyling this function to the measurement.log file:
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 );
}
It still doesn’t match and I am out of ideas 🙁
The solution is to always check the line breaks. My logfile created on a windows machine with php has unix line breaks:
The PHPUnit source file checked out of CVS in unix has unix line breaks:
The PHPUnit source file checkout out of CVS in windows has windows line breaks:
These line breaks broke my regex validation. So now I’m always replacing line breaks to unix line breaks.