CodeTestAbstract.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. abstract class PHPParser_Tests_CodeTestAbstract extends PHPUnit_Framework_TestCase
  3. {
  4. protected function getTests($directory, $fileExtension) {
  5. $it = new RecursiveDirectoryIterator($directory);
  6. $it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::LEAVES_ONLY);
  7. $it = new RegexIterator($it, '(\.' . preg_quote($fileExtension) . '$)');
  8. $tests = array();
  9. foreach ($it as $file) {
  10. // read file
  11. $fileContents = file_get_contents($file);
  12. // evaluate @@{expr}@@ expressions
  13. $fileContents = preg_replace_callback(
  14. '/@@\{(.*?)\}@@/',
  15. array($this, 'evalCallback'),
  16. $fileContents
  17. );
  18. // parse sections
  19. $parts = array_map('trim', explode('-----', $fileContents));
  20. // first part is the name
  21. $name = array_shift($parts);
  22. // multiple sections possible with always two forming a pair
  23. foreach (array_chunk($parts, 2) as $chunk) {
  24. $tests[] = array($name, $chunk[0], $chunk[1]);
  25. }
  26. }
  27. return $tests;
  28. }
  29. protected function evalCallback($matches) {
  30. return eval('return ' . $matches[1] . ';');
  31. }
  32. protected function canonicalize($str) {
  33. // trim from both sides
  34. $str = trim($str);
  35. // normalize EOL to \n
  36. $str = str_replace(array("\r\n", "\r"), "\n", $str);
  37. // trim right side of all lines
  38. return implode("\n", array_map('rtrim', explode("\n", $str)));
  39. }
  40. }