|  | 
|  | 1 | +<?php declare(strict_types=1); | 
|  | 2 | +/* | 
|  | 3 | + * This file is part of PHPUnit. | 
|  | 4 | + * | 
|  | 5 | + * (c) Sebastian Bergmann <[email protected]> | 
|  | 6 | + * | 
|  | 7 | + * For the full copyright and license information, please view the LICENSE | 
|  | 8 | + * file that was distributed with this source code. | 
|  | 9 | + */ | 
|  | 10 | +namespace PHPUnit\Runner\Phpt; | 
|  | 11 | + | 
|  | 12 | +use const DIRECTORY_SEPARATOR; | 
|  | 13 | +use function assert; | 
|  | 14 | +use function dirname; | 
|  | 15 | +use function explode; | 
|  | 16 | +use function file; | 
|  | 17 | +use function file_get_contents; | 
|  | 18 | +use function is_file; | 
|  | 19 | +use function is_readable; | 
|  | 20 | +use function is_string; | 
|  | 21 | +use function preg_match; | 
|  | 22 | +use function rtrim; | 
|  | 23 | +use function str_contains; | 
|  | 24 | +use function trim; | 
|  | 25 | +use PHPUnit\Runner\Exception; | 
|  | 26 | + | 
|  | 27 | +/** | 
|  | 28 | + * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit | 
|  | 29 | + * | 
|  | 30 | + * @internal This class is not covered by the backward compatibility promise for PHPUnit | 
|  | 31 | + * | 
|  | 32 | + * @see https://qa.php.net/phpt_details.php | 
|  | 33 | + */ | 
|  | 34 | +final readonly class Parser | 
|  | 35 | +{ | 
|  | 36 | +    /** | 
|  | 37 | +     * @param non-empty-string $phptFile | 
|  | 38 | +     * | 
|  | 39 | +     * @throws Exception | 
|  | 40 | +     * | 
|  | 41 | +     * @return array<non-empty-string, non-empty-string> | 
|  | 42 | +     */ | 
|  | 43 | +    public function parse(string $phptFile): array | 
|  | 44 | +    { | 
|  | 45 | +        $sections = []; | 
|  | 46 | +        $section  = ''; | 
|  | 47 | + | 
|  | 48 | +        $unsupportedSections = [ | 
|  | 49 | +            'CGI', | 
|  | 50 | +            'COOKIE', | 
|  | 51 | +            'DEFLATE_POST', | 
|  | 52 | +            'EXPECTHEADERS', | 
|  | 53 | +            'EXTENSIONS', | 
|  | 54 | +            'GET', | 
|  | 55 | +            'GZIP_POST', | 
|  | 56 | +            'HEADERS', | 
|  | 57 | +            'PHPDBG', | 
|  | 58 | +            'POST', | 
|  | 59 | +            'POST_RAW', | 
|  | 60 | +            'PUT', | 
|  | 61 | +            'REDIRECTTEST', | 
|  | 62 | +            'REQUEST', | 
|  | 63 | +        ]; | 
|  | 64 | + | 
|  | 65 | +        $lineNr = 0; | 
|  | 66 | + | 
|  | 67 | +        foreach (file($phptFile) as $line) { | 
|  | 68 | +            $lineNr++; | 
|  | 69 | + | 
|  | 70 | +            if (preg_match('/^--([_A-Z]+)--/', $line, $result)) { | 
|  | 71 | +                $section                        = $result[1]; | 
|  | 72 | +                $sections[$section]             = ''; | 
|  | 73 | +                $sections[$section . '_offset'] = $lineNr; | 
|  | 74 | + | 
|  | 75 | +                continue; | 
|  | 76 | +            } | 
|  | 77 | + | 
|  | 78 | +            if ($section === '') { | 
|  | 79 | +                throw new InvalidPhptFileException; | 
|  | 80 | +            } | 
|  | 81 | + | 
|  | 82 | +            $sections[$section] .= $line; | 
|  | 83 | +        } | 
|  | 84 | + | 
|  | 85 | +        if (isset($sections['FILEEOF'])) { | 
|  | 86 | +            $sections['FILE'] = rtrim($sections['FILEEOF'], "\r\n"); | 
|  | 87 | + | 
|  | 88 | +            unset($sections['FILEEOF']); | 
|  | 89 | +        } | 
|  | 90 | + | 
|  | 91 | +        $this->parseExternal($phptFile, $sections); | 
|  | 92 | +        $this->validate($sections); | 
|  | 93 | + | 
|  | 94 | +        foreach ($unsupportedSections as $unsupportedSection) { | 
|  | 95 | +            if (isset($sections[$unsupportedSection])) { | 
|  | 96 | +                throw new UnsupportedPhptSectionException($unsupportedSection); | 
|  | 97 | +            } | 
|  | 98 | +        } | 
|  | 99 | + | 
|  | 100 | +        return $sections; | 
|  | 101 | +    } | 
|  | 102 | + | 
|  | 103 | +    /** | 
|  | 104 | +     * @return array<non-empty-string, non-empty-string> | 
|  | 105 | +     */ | 
|  | 106 | +    public function parseEnvSection(string $content): array | 
|  | 107 | +    { | 
|  | 108 | +        $env = []; | 
|  | 109 | + | 
|  | 110 | +        foreach (explode("\n", trim($content)) as $e) { | 
|  | 111 | +            $e = explode('=', trim($e), 2); | 
|  | 112 | + | 
|  | 113 | +            if ($e[0] !== '' && isset($e[1])) { | 
|  | 114 | +                $env[$e[0]] = $e[1]; | 
|  | 115 | +            } | 
|  | 116 | +        } | 
|  | 117 | + | 
|  | 118 | +        return $env; | 
|  | 119 | +    } | 
|  | 120 | + | 
|  | 121 | +    /** | 
|  | 122 | +     * @param array<string>|string                                              $content | 
|  | 123 | +     * @param array<non-empty-string, array<non-empty-string>|non-empty-string> $ini | 
|  | 124 | +     * | 
|  | 125 | +     * @return array<non-empty-string, array<non-empty-string>|non-empty-string> | 
|  | 126 | +     */ | 
|  | 127 | +    public function parseIniSection(array|string $content, array $ini = []): array | 
|  | 128 | +    { | 
|  | 129 | +        if (is_string($content)) { | 
|  | 130 | +            $content = explode("\n", trim($content)); | 
|  | 131 | +        } | 
|  | 132 | + | 
|  | 133 | +        foreach ($content as $setting) { | 
|  | 134 | +            if (!str_contains($setting, '=')) { | 
|  | 135 | +                continue; | 
|  | 136 | +            } | 
|  | 137 | + | 
|  | 138 | +            $setting = explode('=', $setting, 2); | 
|  | 139 | +            $name    = trim($setting[0]); | 
|  | 140 | +            $value   = trim($setting[1]); | 
|  | 141 | + | 
|  | 142 | +            if ($name === 'extension' || $name === 'zend_extension') { | 
|  | 143 | +                if (!isset($ini[$name])) { | 
|  | 144 | +                    $ini[$name] = []; | 
|  | 145 | +                } | 
|  | 146 | + | 
|  | 147 | +                $ini[$name][] = $value; | 
|  | 148 | + | 
|  | 149 | +                continue; | 
|  | 150 | +            } | 
|  | 151 | + | 
|  | 152 | +            $ini[$name] = $value; | 
|  | 153 | +        } | 
|  | 154 | + | 
|  | 155 | +        return $ini; | 
|  | 156 | +    } | 
|  | 157 | + | 
|  | 158 | +    /** | 
|  | 159 | +     * @param non-empty-string                          $phptFile | 
|  | 160 | +     * @param array<non-empty-string, non-empty-string> $sections | 
|  | 161 | +     * | 
|  | 162 | +     * @throws Exception | 
|  | 163 | +     */ | 
|  | 164 | +    private function parseExternal(string $phptFile, array &$sections): void | 
|  | 165 | +    { | 
|  | 166 | +        $allowSections = [ | 
|  | 167 | +            'FILE', | 
|  | 168 | +            'EXPECT', | 
|  | 169 | +            'EXPECTF', | 
|  | 170 | +            'EXPECTREGEX', | 
|  | 171 | +        ]; | 
|  | 172 | + | 
|  | 173 | +        $testDirectory = dirname($phptFile) . DIRECTORY_SEPARATOR; | 
|  | 174 | + | 
|  | 175 | +        foreach ($allowSections as $section) { | 
|  | 176 | +            if (isset($sections[$section . '_EXTERNAL'])) { | 
|  | 177 | +                $externalFilename = trim($sections[$section . '_EXTERNAL']); | 
|  | 178 | + | 
|  | 179 | +                if (!is_file($testDirectory . $externalFilename) || | 
|  | 180 | +                    !is_readable($testDirectory . $externalFilename)) { | 
|  | 181 | +                    throw new PhptExternalFileCannotBeLoadedException( | 
|  | 182 | +                        $section, | 
|  | 183 | +                        $testDirectory . $externalFilename, | 
|  | 184 | +                    ); | 
|  | 185 | +                } | 
|  | 186 | + | 
|  | 187 | +                $contents = file_get_contents($testDirectory . $externalFilename); | 
|  | 188 | + | 
|  | 189 | +                assert($contents !== false && $contents !== ''); | 
|  | 190 | + | 
|  | 191 | +                $sections[$section] = $contents; | 
|  | 192 | +            } | 
|  | 193 | +        } | 
|  | 194 | +    } | 
|  | 195 | + | 
|  | 196 | +    /** | 
|  | 197 | +     * @param array<non-empty-string, non-empty-string> $sections | 
|  | 198 | +     * | 
|  | 199 | +     * @throws InvalidPhptFileException | 
|  | 200 | +     */ | 
|  | 201 | +    private function validate(array $sections): void | 
|  | 202 | +    { | 
|  | 203 | +        if (!isset($sections['FILE'])) { | 
|  | 204 | +            throw new InvalidPhptFileException; | 
|  | 205 | +        } | 
|  | 206 | + | 
|  | 207 | +        if (!isset($sections['EXPECT']) && | 
|  | 208 | +            !isset($sections['EXPECTF']) && | 
|  | 209 | +            !isset($sections['EXPECTREGEX'])) { | 
|  | 210 | +            throw new InvalidPhptFileException; | 
|  | 211 | +        } | 
|  | 212 | +    } | 
|  | 213 | +} | 
0 commit comments