public function testParse() { $content = explode("\n", Sbp::testContent("<?\n__FILE . __DIR", __FILE__), 2); $content = $content[1]; $this->assertSame(trim($content), var_export(__FILE__, true) . ' . ' . var_export(__DIR__, true) . ';', "__FILE . __DIR should be __FILE__ . __DIR__"); $this->assertParse("ANameSpace\\BClass:CNameSpace\\DClass\n\t- \$var = 'a'", "class ANameSpace\\BClass extends CNameSpace\\DClass {\n\tprivate \$var = 'a';\n}"); }
public function testBenchmark() { $marker = 'Marker'; Sbp::benchmark(); usleep(200); Sbp::benchmark($marker); usleep(200); $content = Sbp::benchmark(Sbp::TEST_GET_BENCHMARK_HTML); $this->assertTrue(stripos($content, '<html') !== false); $this->assertTrue(strpos($content, $marker) !== false); }
public function testPluginNotCallable() { $message = ''; try { sbp_add_plugin('foo3', 'not_callable'); Sbp::parse("<?\necho 'Hello'"); } catch (SbpException $e) { $message = $e->getMessage(); } $this->assertTrue(strpos($message, 'callable') !== false); sbp_remove_plugin('foo3'); }
protected static function matchContent($from, $to) { $trim = static::IGNORE_BRACES ? "{}\n\r\t " : "\n\r\t "; $out = trim(file_get_contents($fromPath = $from)); $in = trim(Sbp::testFileContent($toPath = preg_replace('#^(.+)(/[^/]+)$#', '$1/.src$2', $from))); $to = str_replace(array("\n", "\r", "\t", ' '), '', $out); $from = str_replace(array("\n", "\r", "\t", ' '), '', $in); $cleaner = function ($value) use($trim) { $value = preg_replace('#/\\*.*\\*/#U', '', trim($value, $trim)); $value = preg_replace('#\\s*[\\[\\]\\(\\)\\{\\},!]\\s*#', '$1', $value); return $value; }; $to = $cleaner($to); $from = $cleaner($from); $lastDiffKey = -2 * static::WRAP_LINES; $lastPrintedKey = $lastDiffKey; if ($from !== $to) { echo "\n=====================================\n- {$toPath} (parsed)\n+ {$fromPath}\n"; foreach (array('in', 'out') as $var) { ${$var} = preg_split('#\\r\\n|\\r|\\n#', preg_replace('#(\\n[\\t ]*)(\\n[\\t ]*)}([\\t ]*)(?=\\S)#', '$1}$2$3', ${$var})); } foreach ($in as $key => $line) { if ($cleaner($line) === $cleaner(isset($out[$key]) ? $out[$key] : '')) { if ($key - $lastDiffKey === static::WRAP_LINES) { echo " [...]\n"; } elseif ($key - $lastDiffKey < static::WRAP_LINES && $key) { echo " " . str_replace("\t", ' ', $line) . "\n"; $lastPrintedKey = $key; } } else { for ($i = max(0, $lastPrintedKey + 1, $key - static::WRAP_LINES); $i < $key; $i++) { echo " " . str_replace("\t", ' ', $in[$i]) . "\n"; } echo "-" . str_replace("\t", ' ', $line) . "\n"; echo "+" . str_replace("\t", ' ', $out[$key]) . "\n"; $lastDiffKey = $key; $lastPrintedKey = $key; } } } return $from === $to; }