Blame view

vendor/nikic/php-parser/test/PhpParser/CodeTestAbstract.php 2.12 KB
d06254b2   Etienne Pallier   bugfix plugin mig...
1
<?php
c4650843   Etienne Pallier   Ajout du dossier ...
2
3
4

namespace PhpParser;

d06254b2   Etienne Pallier   bugfix plugin mig...
5
6
7
8
9
10
11
abstract class CodeTestAbstract extends \PHPUnit_Framework_TestCase
{
    protected function getTests($directory, $fileExtension) {
        $directory = realpath($directory);
        $it = new \RecursiveDirectoryIterator($directory);
        $it = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::LEAVES_ONLY);
        $it = new \RegexIterator($it, '(\.' . preg_quote($fileExtension) . '$)');
c4650843   Etienne Pallier   Ajout du dossier ...
12

d06254b2   Etienne Pallier   bugfix plugin mig...
13
14
15
16
17
        $tests = array();
        foreach ($it as $file) {
            $fileName = $file->getPathname();
            $fileContents = file_get_contents($fileName);
            $fileContents = canonicalize($fileContents);
c4650843   Etienne Pallier   Ajout du dossier ...
18

d06254b2   Etienne Pallier   bugfix plugin mig...
19
20
21
22
23
24
25
26
27
28
29
            // evaluate @@{expr}@@ expressions
            $fileContents = preg_replace_callback(
                '/@@\{(.*?)\}@@/',
                function($matches) {
                    return eval('return ' . $matches[1] . ';');
                },
                $fileContents
            );

            // parse sections
            $parts = preg_split("/\n-----(?:\n|$)/", $fileContents);
c4650843   Etienne Pallier   Ajout du dossier ...
30
31

            // first part is the name
d06254b2   Etienne Pallier   bugfix plugin mig...
32
            $name = array_shift($parts) . ' (' . $fileName . ')';
c4650843   Etienne Pallier   Ajout du dossier ...
33
34
35
            $shortName = ltrim(str_replace($directory, '', $fileName), '/\\');

            // multiple sections possible with always two forming a pair
d06254b2   Etienne Pallier   bugfix plugin mig...
36
37
38
39
40
            $chunks = array_chunk($parts, 2);
            foreach ($chunks as $i => $chunk) {
                $dataSetName = $shortName . (count($chunks) > 1 ? '#' . $i : '');
                list($expected, $mode) = $this->extractMode($chunk[1]);
                $tests[$dataSetName] = array($name, $chunk[0], $expected, $mode);
c4650843   Etienne Pallier   Ajout du dossier ...
41
42
43
            }
        }

d06254b2   Etienne Pallier   bugfix plugin mig...
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
        return $tests;
    }

    private function extractMode($expected) {
        $firstNewLine = strpos($expected, "\n");
        if (false === $firstNewLine) {
            $firstNewLine = strlen($expected);
        }

        $firstLine = substr($expected, 0, $firstNewLine);
        if (0 !== strpos($firstLine, '!!')) {
            return [$expected, null];
        }

        $expected = (string) substr($expected, $firstNewLine + 1);
        return [$expected, substr($firstLine, 2)];
c4650843   Etienne Pallier   Ajout du dossier ...
60
61
    }
}