php-parser/test/PhpParser/CodeTestAbstract.php

57 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace PhpParser;
abstract class CodeTestAbstract extends \PHPUnit_Framework_TestCase
{
protected function getTests($directory, $fileExtension) {
$it = new \RecursiveDirectoryIterator($directory);
$it = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::LEAVES_ONLY);
$it = new \RegexIterator($it, '(\.' . preg_quote($fileExtension) . '$)');
2011-11-27 21:43:27 +01:00
$tests = array();
foreach ($it as $file) {
$fileName = realpath($file->getPathname());
$fileContents = file_get_contents($fileName);
// evaluate @@{expr}@@ expressions
$fileContents = preg_replace_callback(
'/@@\{(.*?)\}@@/',
2015-05-02 22:35:15 +02:00
function($matches) {
return eval('return ' . $matches[1] . ';');
},
$fileContents
);
2011-11-27 11:20:35 +01:00
// parse sections
2011-11-27 21:43:27 +01:00
$parts = array_map('trim', explode('-----', $fileContents));
// first part is the name
$name = array_shift($parts) . ' (' . $fileName . ')';
2011-11-27 21:43:27 +01:00
// multiple sections possible with always two forming a pair
foreach (array_chunk($parts, 2) as $chunk) {
list($expected, $mode) = $this->extractMode(canonicalize($chunk[1]));
$tests[] = array($name, $chunk[0], $expected, $mode);
2011-11-27 21:43:27 +01:00
}
}
return $tests;
}
2011-11-26 17:10:18 +01:00
private function extractMode($expected) {
$firstNewLine = strpos($expected, "\n");
if (false === $firstNewLine) {
2015-06-13 15:42:09 +02:00
$firstNewLine = strlen($expected);
}
$firstLine = substr($expected, 0, $firstNewLine);
if (0 !== strpos($firstLine, '!!')) {
return [$expected, null];
}
2015-06-13 15:42:09 +02:00
$expected = (string) substr($expected, $firstNewLine + 1);
return [$expected, substr($firstLine, 2)];
}
}