home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / xamp / xampp-win32.exe / xampp / .tmpparse_test_messages.phpt < prev    next >
Encoding:
Text File  |  2004-03-24  |  4.5 KB  |  228 lines

  1. --TEST--
  2. Parse messages in testdata dir
  3. --SKIPIF--
  4. <?php 
  5. /* vim600: sw=4 ts=4 fdm=marker syn=php
  6. */
  7. if (!extension_loaded("mailparse") || !extension_loaded("zlib")) print "skip"; ?>
  8. --FILE--
  9. <?php
  10. error_reporting(E_ALL ^ E_NOTICE);
  11.  
  12. $define_expect = isset($argv[1]) && $argv[1] == "define_expect";
  13. $force_test = isset($argv[1]) && !$define_expect ? $argv[1] : null;
  14. $testdir = dirname(__FILE__) . "/testdata";
  15.  
  16. $dir = opendir($testdir) or die("unable to open test dir!");
  17. $messages = array();
  18.  
  19. while (($f = readdir($dir)) !== false) {
  20.     if ($f == "CVS" || $f == "." || $f == "..")
  21.         continue;
  22.         
  23.     list($name, $suffix) = explode(".", $f, 2);
  24.     
  25.     switch($suffix) {
  26.         case "txt":
  27.         case "txt.gz":
  28.             $messages[$name]["testfile"] = $f;
  29.             break;
  30.         case "exp":
  31.             $messages[$name]["expectfile"] = $f;
  32.             break;
  33.     }
  34. }
  35.  
  36. if ($force_test !== null) {
  37.     $messages = array($force_test => $messages[$force_test]);
  38. }
  39.  
  40. if (function_exists("version_compare") && version_compare(phpversion(), "4.3", "ge")) {
  41.     $wrapper = "compress.zlib://";
  42. } else {
  43.     /* this section is here because it is useful to compare to the
  44.      * original implementaion of mailparse for PHP 4.2 */
  45.     $wrapper = "zlib:";
  46.     
  47.     function file_get_contents($filename)
  48.     {
  49.         $fp = fopen($filename, "rb");
  50.         $data = fread($fp, filesize($filename));
  51.         fclose($fp);
  52.         return $data;
  53.     }
  54. }
  55.  
  56. function diff_strings($left, $right)
  57. {
  58.     if (is_executable("/usr/bin/diff")) {
  59.         $lf = tempnam("/tmp", "mpt");
  60.         $rf = tempnam("/tmp", "mpt");
  61.  
  62.         $ok = false;
  63.  
  64.         $fp = fopen($lf, "wb");
  65.         if ($fp) {
  66.             fwrite($fp, $left);
  67.             fclose($fp);
  68.  
  69.             $fp = fopen($rf, "wb");
  70.             if ($fp) {
  71.                 fwrite($fp, $right);
  72.                 fclose($fp);
  73.  
  74.                 $ok = true;
  75.             }
  76.         }
  77.  
  78.         if ($ok) {
  79.             passthru("/usr/bin/diff -u $lf $rf");
  80.         }
  81.  
  82.         unlink($lf);
  83.         unlink($rf);
  84.  
  85.         if ($ok)
  86.             return;
  87.     }
  88.  
  89.     
  90.     $left = explode("\n", $left);
  91.     $right = explode("\n", $right);
  92.  
  93.     $n = max(count($left), count($right));
  94.  
  95.     $difflines = array();
  96.  
  97.     $runstart = null;
  98.     $runend = null;
  99.  
  100.     for ($i = 0; $i < $n; $i++) {
  101.         if ($left[$i] != $right[$i]) {
  102.             if ($runstart === null) {
  103.                 $runstart = $i;
  104.                 $runend = $i;
  105.             } else {
  106.                 /* part of the run */
  107.                 $runend = $i;
  108.             }
  109.         } else {
  110.             if ($runstart !== null) {
  111.                 $difflines[] = array($runstart, $runend);
  112.                 $runstart = null;
  113.                 $runend = null;
  114.             }
  115.         }
  116.     }
  117.     if ($runstart !== null)
  118.         $difflines[] = array($runstart, $runend);
  119.     
  120.     $lastprint = null;
  121.     foreach ($difflines as $run) {
  122.         list($start, $end) = $run;
  123.  
  124.         $startline = $start - 3;
  125.         if ($startline < 0)
  126.             $startline = 0;
  127.         $endline = $end;
  128.  
  129.         if ($lastprint === null) {
  130.             echo "@@ Line: " . ($startline+1) . "\n";
  131.         } else if ($startline <= $lastprint) {
  132.             $startline = $lastprint+1;
  133.         }
  134.  
  135.         if ($startline > $endline)
  136.             continue;
  137.  
  138.         /* starting context */
  139.         for ($i = $startline; $i < $start; $i++) {
  140.             echo " " . $left[$i] . "\n";
  141.             $lastprint = $i;
  142.         }
  143.  
  144.         /* diff run */
  145.         for ($i = $start; $i <= $end; $i++) {
  146.             echo "-" . $left[$i] . "\n";
  147.         }
  148.         for ($i = $start; $i <= $end; $i++) {
  149.             echo "+" . $right[$i] . "\n";
  150.         }
  151.         $lastprint = $i;
  152.     }
  153.     
  154. }
  155.  
  156. $skip_keys = array("headers", "ending-pos-body");
  157.  
  158. foreach ($messages as $name => $msgdata) {
  159.     $testname = $testdir . "/" . $msgdata["testfile"];
  160.     $expectname = $testdir . "/" . $msgdata["expectfile"];
  161.  
  162.     $use_wrapper = substr($testname, -3) == ".gz" ? $wrapper : "";
  163.     $use_wrapper = $wrapper;
  164.     $fp = fopen("$use_wrapper$testname", "rb") or die("failed to open the file!");
  165.  
  166.     $mime = mailparse_msg_create();
  167.     $size = 0;
  168.     while (!feof($fp)) {
  169.         $data = fread($fp, 1024);
  170.         //var_dump($data);
  171.         if ($data !== false) {
  172.             mailparse_msg_parse($mime, $data);
  173.             $size += strlen($data);
  174.         }
  175.     }
  176.     fclose($fp);
  177.     //var_dump($size);
  178.  
  179.     $struct = mailparse_msg_get_structure($mime);
  180.  
  181.     ob_start();
  182.     echo "Message: $name\n";
  183.     foreach($struct as $partname) {
  184.         $depth = count(explode(".", $partname)) - 1;
  185.         $indent = str_repeat("  ", $depth * 2);
  186.         $subpart = mailparse_msg_get_part($mime, $partname);
  187.         if (!$subpart) {
  188.             var_dump($partname);
  189.             echo "\n";
  190.             var_dump($struct);
  191.             break;
  192.         }
  193.  
  194.         $data = mailparse_msg_get_part_data($subpart);
  195.         echo "\n{$indent}Part $partname\n";
  196.         ksort($data);
  197.         foreach ($data as $key => $value) {
  198.             if (in_array($key, $skip_keys))
  199.                 continue;
  200.             echo "$indent$key => ";
  201.             var_dump($value);
  202.         }
  203.     }
  204.     $output = ob_get_contents();
  205.  
  206.     if ($define_expect) {
  207.         $fp = fopen($expectname, "wb");
  208.         fwrite($fp, $output);
  209.         fclose($fp);
  210.     } else {
  211.  
  212.         $expect = file_get_contents($expectname);
  213.  
  214.         if ($output != $expect) {
  215.             ob_end_flush();
  216.             diff_strings($expect, $output);
  217.             die("FAIL!");
  218.         }
  219.     }
  220.  
  221.     ob_end_clean();
  222. }
  223.  
  224. echo "All messages parsed OK!\n";
  225. ?>
  226. --EXPECT--
  227. All messages parsed OK!
  228.