public function test_fix_broken_serialization() { $expectedData = array('Normal', 'High-value Char: ' . chr(231) . 'a-va?'); $brokenSerialization = 'a:2:{i:0;s:6:"Normal";i:1;s:23:"High-value Char: ▒a-va?";}'; $expectedError = array('errno' => 8, 'errstr' => 'unserialize(): Error at offset 55 of 60 bytes'); $reportedError = array(); set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use(&$reportedError) { $reportedError = compact('errno', 'errstr'); }); unserialize($brokenSerialization); $this->assertEquals($expectedError['errno'], $reportedError['errno']); // Because HHVM's unserialize() error message does not contain enough info to properly test. if (!defined('HHVM_VERSION')) { $this->assertEquals($expectedError['errstr'], $reportedError['errstr']); } restore_error_handler(); $fixedSerialization = util::fix_broken_serialization($brokenSerialization); $unserializedData = unserialize($fixedSerialization); $this->assertEquals($expectedData[0], $unserializedData[0], 'Did not properly fix the broken serialized data.'); $this->assertEquals(substr($expectedData[1], 0, 10), substr($unserializedData[1], 0, 10), 'Did not properly fix the broken serialized data.'); }