Skip to content

Commit 6af7984

Browse files
committed
Run Samples as Part of Unit Tests
1 parent fc15bea commit 6af7984

File tree

2 files changed

+106
-45
lines changed

2 files changed

+106
-45
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ jobs:
109109

110110
- name: Composer Config nightly
111111
if: matrix.php == 'nightly'
112-
run: composer config platform.php 8.4.99
112+
run: composer config platform.php 8.5.99
113113

114114
- name: Composer Install
115115
run: composer install --ansi --prefer-dist --no-interaction --no-progress
@@ -144,47 +144,3 @@ jobs:
144144
wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.4.3/php-coveralls.phar
145145
chmod +x php-coveralls.phar
146146
php php-coveralls.phar --coverage_clover=build/clover.xml --json_path=build/coveralls-upload.json -vvv
147-
148-
samples:
149-
name: Check samples
150-
runs-on: ubuntu-latest
151-
strategy:
152-
fail-fast: false
153-
matrix:
154-
experimental:
155-
- false
156-
php:
157-
- '7.1'
158-
- '7.2'
159-
- '7.3'
160-
- '7.4'
161-
- '8.0'
162-
- '8.1'
163-
- '8.2'
164-
- '8.3'
165-
- '8.4'
166-
167-
include:
168-
- php: 'nightly'
169-
experimental: true
170-
steps:
171-
- name: Setup PHP
172-
uses: shivammathur/setup-php@v2
173-
with:
174-
php-version: ${{ matrix.php }}
175-
extensions: gd, xml, zip
176-
coverage: xdebug
177-
178-
- uses: actions/checkout@v2
179-
180-
- name: Composer Config nightly
181-
if: matrix.php == 'nightly'
182-
run: composer config platform.php 8.4.99
183-
184-
- name: Composer Install
185-
run: composer install --ansi --prefer-dist --no-interaction --no-progress
186-
187-
- name: "Generate samples files (Experimental: ${{ matrix.experimental }})"
188-
env:
189-
FAILURE_ACTION: "${{ matrix.experimental == true }}"
190-
run: composer run samples || $FAILURE_ACTION

tests/PhpWordTests/SampleTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)