Skip to content

Commit 92e24f3

Browse files
Issue 932 - improve docs about JSONScript (#933)
* ReadmeContentsBuilder.php - added script to update the list of JSONScript test cases in README.md * docs (README.md): updated list of tests * docs (README.md): updated docs - added manual how to update list of tests - updated the result of executing command `composer integration -- --filter vcard`
1 parent b3e10e4 commit 92e24f3

2 files changed

Lines changed: 152 additions & 11 deletions

File tree

tests/phpunit/Integration/JSONScript/README.md

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1+
## How to update List of tests
2+
3+
The `readmeContentsBuilder.php` script can be used to update the list of available test cases, including their descriptions.
4+
5+
To use this script, navigate to the following directory in your project:
6+
- `extensions/SemanticResultFormats/tests/phpunit/Integration/JSONScript/`
7+
8+
Once there, run the script using PHP with the following command:
9+
- `php ReadmeContentsBuilder.php`
10+
11+
The script will automatically fetch the test cases, update the `README.md` file, and ensure that the list of tests is up to date with any changes made in the test case files.
12+
It will also generate the descriptions for each test based on the contents of the corresponding JSON files.
113

214
<!-- Begin of generated contents by readmeContentsBuilder.php -->
315

4-
## TestCases
16+
## List of tests
517

6-
Contains 21 files with a total of 62 tests:
18+
- Files: 33 (includes 131 tests)
19+
- Last update: 2025-04-01
20+
21+
### B
22+
* [bibtex-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/bibtex-01.json) Test `format=bibtex`
23+
24+
### C
25+
* [carousel-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/carousel-01.json) Test `format=carousel` html output (no JS validation)
726

827
### D
928
* [datatables-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/datatables-01.json) Test `format=datatables` html output (no JS validation)
@@ -15,9 +34,13 @@ Contains 21 files with a total of 62 tests:
1534
### F
1635
* [filtered-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/filtered-01.json) Filtered format: ...
1736
* [filtered-02.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/filtered-02.json) Filtered format: ...
37+
* [filtered-03.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/filtered-03.json) Filtered format: List view tests
1838

1939
### G
2040
* [gallery-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/gallery-01.json) Test `format=gallery` with file upload
41+
* [gallery-02.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/gallery-02.json) Test `format=gallery` with file upload and captiontemplate
42+
* [gallery-03.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/gallery-03.json) Test `format=gallery` with different different parameters like widget, redirects, overlay
43+
* [gantt-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/gantt-01.json) Test the gantt format
2144

2245
### I
2346
* [icalendar-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/icalendar-01.json) Test `format=icalendar`
@@ -26,12 +49,19 @@ Contains 21 files with a total of 62 tests:
2649

2750
### L
2851
* [latest-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/latest-01.json) Test for `format=latest`
52+
* [listwidget-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/listwidget-01.json) Listwidget format: widgets - unordered (#449 - `wgContLang=fr`, `wgLang=en`)
2953

3054
### M
3155
* [math-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/math-01.json) Test for math/sum result format in pt-br lang
56+
* [media-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/media-01.json) Test `format=media` with file upload
57+
58+
### O
59+
* [outline-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/outline-01.json) Test `format=outline` template output
60+
* [outline-02.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/outline-02.json) Test `format=outline` list output
3261

3362
### T
3463
* [tagcloud-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/tagcloud-01.json) Test `format=tagcloud` html output
64+
* [tagcloud-02.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/tagcloud-02.json) Test `format=tagcloud` html output, namespaced
3565
* [timeline-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/timeline-01.json) Test for timeline result format
3666
* [tree-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/tree-01.json) Tree format: Multi-page results
3767
* [tree-02.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/tree-02.json) Tree format: Simple one-page result
@@ -42,10 +72,9 @@ Contains 21 files with a total of 62 tests:
4272
* [tree-07.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/tree-07.json) Tree format: Simple one-page result
4373

4474
### V
75+
* [valuerank-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/valuerank-01.json) Test `format=valuerank` with all related parameters
4576
* [vcard-01.json](https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases/vcard-01.json) Test `format=vcard`
4677

47-
-- Last updated on 2017-11-11 by `readmeContentsBuilder.php`
48-
4978
<!-- End of generated contents by readmeContentsBuilder.php -->
5079

5180
## Writing a test case
@@ -59,21 +88,21 @@ combination with the `--filter` option allows to select a single specific case.
5988

6089
<pre>
6190
$ composer integration -- --filter vcard
62-
Using PHP 7.1.1
91+
Using PHP 8.1.31
6392

64-
Semantic Result Formats: 3.0.0-alpha
93+
Semantic Result Formats: 5.0.0-alpha
6594

66-
Semantic MediaWiki: 3.0.0-alpha (c352b6a, SMWSQLStore3, mysql)
67-
MediaWiki: 1.31.0-alpha (44c06df, MediaWiki vendor autoloader)
95+
Semantic MediaWiki: 5.0.1-alpha (25f24c1, SMWSQLStore, mysql)
96+
MediaWiki: 1.39.11 (MediaWiki vendor autoloader)
6897
Site language: en
6998

70-
Execution time: 2017-01-01 12:00
99+
Execution time: 2025-04-01 06:40
71100
Debug logs: Disabled
72101
Xdebug: Disabled (or not installed)
73102

74-
PHPUnit 4.8.35 by Sebastian Bergmann and contributors.
103+
PHPUnit 8.5.41 by Sebastian Bergmann and contributors.
75104

76-
Runtime: PHP 7.1.1
105+
Runtime: PHP 8.1.31
77106
Configuration: /var/www/html/w/extensions/SemanticResultFormats/phpunit.xml.dist
78107

79108
.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace SRF\Tests\Integration\JSONScript;
4+
5+
/**
6+
* Build contents from a selected folder and replaces the content of the
7+
* README.md from where the script was started.
8+
*
9+
* @license GPL-2.0-or-later
10+
* @since 4.2.1
11+
*
12+
* @author milic
13+
*/
14+
class ReadmeContentsBuilder {
15+
16+
/**
17+
* @var string
18+
*/
19+
public const REPLACE_START_MARKER = '<!-- Begin of generated contents by readmeContentsBuilder.php -->';
20+
public const REPLACE_END_MARKER = '<!-- End of generated contents by readmeContentsBuilder.php -->';
21+
22+
/**
23+
* @var array
24+
*/
25+
private $urlLocationMap = [
26+
'List of tests' => 'https://github.com/SemanticMediaWiki/SemanticResultFormats/tree/master/tests/phpunit/Integration/JSONScript/TestCases'
27+
];
28+
29+
/**
30+
* @since 4.2.1
31+
*/
32+
public function run() {
33+
$file = __DIR__ . '/README.md';
34+
35+
$replacement = self::REPLACE_START_MARKER . "\n\n";
36+
$replacement .= $this->doGenerateContentFor( 'List of tests', __DIR__ . '/TestCases' );
37+
38+
$replacement .= "\n" . self::REPLACE_END_MARKER;
39+
40+
$contents = file_get_contents( $file );
41+
$start = strpos( $contents, self::REPLACE_START_MARKER );
42+
$length = strrpos( $contents, self::REPLACE_END_MARKER ) - $start + strlen( self::REPLACE_END_MARKER );
43+
44+
file_put_contents(
45+
$file,
46+
substr_replace( $contents, $replacement, $start, $length )
47+
);
48+
}
49+
50+
private function doGenerateContentFor( $title, $path ) {
51+
$dateTimeUtc = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) );
52+
$urlLocation = $this->urlLocationMap[$title];
53+
54+
$counter = 0;
55+
$tests = 0;
56+
$previousFirstKey = '';
57+
$list = '';
58+
59+
$files = $this->findFilesFor( $path, 'json' );
60+
ksort( $files );
61+
62+
foreach ( $files as $key => $location ) {
63+
if ( $previousFirstKey !== $key[0] ) {
64+
$list .= "\n" . '### ' . ucfirst( $key[0] ) . "\n";
65+
}
66+
67+
$list .= '* [' . $key . '](' . $urlLocation . '/' . $key . ')';
68+
69+
$contents = json_decode( file_get_contents( $location ), true );
70+
71+
if ( $contents === null || json_last_error() !== JSON_ERROR_NONE ) {
72+
continue;
73+
}
74+
75+
if ( isset( $contents['description'] ) ) {
76+
$list .= " " . $contents['description'];
77+
}
78+
79+
if ( isset( $contents['tests'] ) ) {
80+
$tests += count( $contents['tests'] );
81+
}
82+
83+
$list .= "\n";
84+
$counter++;
85+
$previousFirstKey = $key[0];
86+
}
87+
88+
$head = "## $title\n\n";
89+
$head .= "- Files: $counter (includes $tests tests)\n";
90+
$head .= "- Last update: " . $dateTimeUtc->format( 'Y-m-d' ) . "\n";
91+
92+
return $head . $list;
93+
}
94+
95+
private function findFilesFor( $path, $extension ) {
96+
$files = [];
97+
98+
$directoryIterator = new \RecursiveDirectoryIterator( $path );
99+
100+
foreach ( new \RecursiveIteratorIterator( $directoryIterator ) as $fileInfo ) {
101+
if ( strtolower( substr( $fileInfo->getFilename(), -( strlen( $extension ) + 1 ) ) ) === ( '.' . $extension ) ) {
102+
$files[$fileInfo->getFilename()] = $fileInfo->getPathname();
103+
}
104+
}
105+
106+
return $files;
107+
}
108+
109+
}
110+
111+
$readmeContentsBuilder = new ReadmeContentsBuilder();
112+
$readmeContentsBuilder->run();

0 commit comments

Comments
 (0)