comparison vendor/nikic/php-parser/test/PhpParser/CodeTestAbstract.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
12:7a779792577d 13:5fb285c0d0e3
1 <?php 1 <?php declare(strict_types=1);
2 2
3 namespace PhpParser; 3 namespace PhpParser;
4 4
5 abstract class CodeTestAbstract extends \PHPUnit_Framework_TestCase 5 use PHPUnit\Framework\TestCase;
6
7 require_once __DIR__ . '/CodeTestParser.php';
8
9 abstract class CodeTestAbstract extends TestCase
6 { 10 {
7 protected function getTests($directory, $fileExtension) { 11 protected function getTests($directory, $fileExtension, $chunksPerTest = 2) {
8 $directory = realpath($directory); 12 $parser = new CodeTestParser;
9 $it = new \RecursiveDirectoryIterator($directory); 13 $allTests = [];
10 $it = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::LEAVES_ONLY); 14 foreach (filesInDir($directory, $fileExtension) as $fileName => $fileContents) {
11 $it = new \RegexIterator($it, '(\.' . preg_quote($fileExtension) . '$)'); 15 list($name, $tests) = $parser->parseTest($fileContents, $chunksPerTest);
12
13 $tests = array();
14 foreach ($it as $file) {
15 $fileName = $file->getPathname();
16 $fileContents = file_get_contents($fileName);
17 $fileContents = canonicalize($fileContents);
18
19 // evaluate @@{expr}@@ expressions
20 $fileContents = preg_replace_callback(
21 '/@@\{(.*?)\}@@/',
22 function($matches) {
23 return eval('return ' . $matches[1] . ';');
24 },
25 $fileContents
26 );
27
28 // parse sections
29 $parts = preg_split("/\n-----(?:\n|$)/", $fileContents);
30 16
31 // first part is the name 17 // first part is the name
32 $name = array_shift($parts) . ' (' . $fileName . ')'; 18 $name .= ' (' . $fileName . ')';
33 $shortName = ltrim(str_replace($directory, '', $fileName), '/\\'); 19 $shortName = ltrim(str_replace($directory, '', $fileName), '/\\');
34 20
35 // multiple sections possible with always two forming a pair 21 // multiple sections possible with always two forming a pair
36 $chunks = array_chunk($parts, 2); 22 foreach ($tests as $i => list($mode, $parts)) {
37 foreach ($chunks as $i => $chunk) { 23 $dataSetName = $shortName . (count($parts) > 1 ? '#' . $i : '');
38 $dataSetName = $shortName . (count($chunks) > 1 ? '#' . $i : ''); 24 $allTests[$dataSetName] = array_merge([$name], $parts, [$mode]);
39 list($expected, $mode) = $this->extractMode($chunk[1]);
40 $tests[$dataSetName] = array($name, $chunk[0], $expected, $mode);
41 } 25 }
42 } 26 }
43 27
44 return $tests; 28 return $allTests;
45 }
46
47 private function extractMode($expected) {
48 $firstNewLine = strpos($expected, "\n");
49 if (false === $firstNewLine) {
50 $firstNewLine = strlen($expected);
51 }
52
53 $firstLine = substr($expected, 0, $firstNewLine);
54 if (0 !== strpos($firstLine, '!!')) {
55 return [$expected, null];
56 }
57
58 $expected = (string) substr($expected, $firstNewLine + 1);
59 return [$expected, substr($firstLine, 2)];
60 } 29 }
61 } 30 }