|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of PHPWord - A pure PHP library for reading and writing |
| 5 | + * word processing documents. |
| 6 | + * PHPWord is free software distributed under the terms of the GNU Lesser |
| 7 | + * General Public License version 3 as published by the Free Software Foundation. |
| 8 | + * For the full copyright and license information, please read the LICENSE |
| 9 | + * file that was distributed with this source code. For the full list of |
| 10 | + * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
| 11 | + * |
| 12 | + * @see https://github.com/PHPOffice/PHPWord |
| 13 | + * |
| 14 | + * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 15 | + */ |
| 16 | + |
| 17 | +namespace PhpOffice\PhpWordTests; |
| 18 | + |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use RecursiveDirectoryIterator; |
| 21 | +use RecursiveIteratorIterator; |
| 22 | +use RecursiveRegexIterator; |
| 23 | +use RegexIterator; |
| 24 | +use RuntimeException; |
| 25 | + |
| 26 | +class SampleTest extends TestCase |
| 27 | +{ |
| 28 | + protected static bool $alwaysTrue = true; |
| 29 | + |
| 30 | + /** |
| 31 | + * @preserveGlobalState disabled |
| 32 | + * |
| 33 | + * @runInSeparateProcess |
| 34 | + * |
| 35 | + * @dataProvider providerSample |
| 36 | + */ |
| 37 | + public function testSample(string $sample): void |
| 38 | + { |
| 39 | + ob_start(); |
| 40 | + require $sample; |
| 41 | + ob_end_clean(); |
| 42 | + |
| 43 | + self::assertTrue(self::$alwaysTrue); |
| 44 | + } |
| 45 | + |
| 46 | + public static function providerSample(): array |
| 47 | + { |
| 48 | + $skipped = []; |
| 49 | + if (getenv('SKIP_URL_IMAGE_TEST') === '1') { |
| 50 | + $skipped[] = 'Sample_13_Images.php'; |
| 51 | + $skipped[] = 'Sample_30_ReadHTML.php'; |
| 52 | + } |
| 53 | + $result = []; |
| 54 | + foreach (self::getSamples() as $samples) { |
| 55 | + foreach ($samples as $sample) { |
| 56 | + if (!in_array($sample, $skipped)) { |
| 57 | + $file = 'samples/' . $sample; |
| 58 | + $result[$sample] = [$file]; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return $result; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns an array of all known samples. |
| 68 | + * |
| 69 | + * @return string[][] [$name => $path] |
| 70 | + */ |
| 71 | + public static function getSamples(): array |
| 72 | + { |
| 73 | + // Populate samples |
| 74 | + $baseDir = realpath('samples'); |
| 75 | + if ($baseDir === false) { |
| 76 | + // @codeCoverageIgnoreStart |
| 77 | + throw new RuntimeException('realpath returned false'); |
| 78 | + // @codeCoverageIgnoreEnd |
| 79 | + } |
| 80 | + $directory = new RecursiveDirectoryIterator($baseDir); |
| 81 | + $iterator = new RecursiveIteratorIterator($directory); |
| 82 | + $regex = new RegexIterator($iterator, '/Sample_\\d+_.+[.]php$/', RecursiveRegexIterator::GET_MATCH); |
| 83 | + |
| 84 | + $files = []; |
| 85 | + /** @var string[] $file */ |
| 86 | + foreach ($regex as $file) { |
| 87 | + $file = str_replace(str_replace('\\', '/', $baseDir) . '/', '', str_replace('\\', '/', $file[0])); |
| 88 | + $info = pathinfo($file); |
| 89 | + $category = 'PhpWord'; |
| 90 | + $name = str_replace('_', ' ', (string) preg_replace('/(|\.php)/', '', $info['filename'])); |
| 91 | + if (!isset($files[$category])) { |
| 92 | + $files[$category] = []; |
| 93 | + } |
| 94 | + $files[$category][$name] = $file; |
| 95 | + } |
| 96 | + |
| 97 | + // Sort everything |
| 98 | + ksort($files); |
| 99 | + foreach ($files as &$f) { |
| 100 | + asort($f); |
| 101 | + } |
| 102 | + |
| 103 | + return $files; |
| 104 | + } |
| 105 | +} |
0 commit comments