Skip to content

Commit bd64866

Browse files
Add integration and unit tests for YAML writing, Asciidoc formatting, and filesystem operations
1 parent b25e204 commit bd64866

3 files changed

Lines changed: 236 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Integration;
6+
7+
use OpenEHR\BmmPublisher\BmmSchemaCollection;
8+
use OpenEHR\BmmPublisher\Helper\OutputDir;
9+
use OpenEHR\BmmPublisher\Writer\BmmYaml;
10+
use PHPUnit\Framework\Attributes\Test;
11+
use PHPUnit\Framework\TestCase;
12+
13+
final class BmmYamlSmokeTest extends TestCase
14+
{
15+
private string $tempOutput;
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
$this->tempOutput = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'bmm_publisher_yaml_smoke_' . uniqid('', true);
21+
self::assertTrue(mkdir($this->tempOutput, 0700, true));
22+
putenv('BMM_OUTPUT_DIR=' . $this->tempOutput);
23+
OutputDir::reset();
24+
}
25+
26+
protected function tearDown(): void
27+
{
28+
putenv('BMM_OUTPUT_DIR');
29+
OutputDir::reset();
30+
if (is_dir($this->tempOutput)) {
31+
$this->deleteTree($this->tempOutput);
32+
}
33+
parent::tearDown();
34+
}
35+
36+
private function deleteTree(string $dir): void
37+
{
38+
$items = @scandir($dir);
39+
if ($items === false) {
40+
return;
41+
}
42+
foreach (array_diff($items, ['.', '..']) as $item) {
43+
$path = $dir . DIRECTORY_SEPARATOR . $item;
44+
is_dir($path) && !is_link($path) ? $this->deleteTree($path) : @unlink($path);
45+
}
46+
@rmdir($dir);
47+
}
48+
49+
#[Test]
50+
public function writesYamlFileForLoadedSchema(): void
51+
{
52+
$collection = new BmmSchemaCollection();
53+
$collection->load('openehr_term_3.0.0');
54+
55+
(new BmmYaml($collection))();
56+
57+
$expectedFile = $this->tempOutput . DIRECTORY_SEPARATOR . 'BMM-YAML' . DIRECTORY_SEPARATOR . 'openehr_term_3.0.0.bmm.yaml';
58+
self::assertFileExists($expectedFile);
59+
$content = (string) file_get_contents($expectedFile);
60+
self::assertNotSame('', $content);
61+
self::assertStringContainsString('rm_publisher', $content);
62+
}
63+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit;
6+
7+
use OpenEHR\BmmPublisher\BmmSchemaCollection;
8+
use OpenEHR\BmmPublisher\Writer\Formatter\AsciidocDefinition;
9+
use PHPUnit\Framework\Attributes\Test;
10+
use PHPUnit\Framework\TestCase;
11+
12+
final class AsciidocDefinitionFormatTest extends TestCase
13+
{
14+
private function makeFormatter(bool $legacyFormat = false): AsciidocDefinition
15+
{
16+
return new AsciidocDefinition(new BmmSchemaCollection(), $legacyFormat);
17+
}
18+
19+
#[Test]
20+
public function formatTextReturnsEmptyStringForNull(): void
21+
{
22+
$f = $this->makeFormatter();
23+
24+
self::assertSame('', $f->formatText(null));
25+
}
26+
27+
#[Test]
28+
public function formatTextTrimsWhitespace(): void
29+
{
30+
$f = $this->makeFormatter();
31+
32+
self::assertSame('x', $f->formatText(" x \n"));
33+
}
34+
35+
#[Test]
36+
public function formatTextAppliesTextReplacementMap(): void
37+
{
38+
$f = $this->makeFormatter();
39+
40+
self::assertSame('a&#124;b', $f->formatText('a|b'));
41+
self::assertSame('a\<=b', $f->formatText('a<=b'));
42+
self::assertSame('a.&#42;b', $f->formatText('a.*b'));
43+
}
44+
45+
#[Test]
46+
public function formatXrefReturnsEmptyWhenNoOpenehrMarker(): void
47+
{
48+
$f = $this->makeFormatter();
49+
$results = [];
50+
51+
self::assertSame('', $f->formatXref('', $results));
52+
self::assertSame([], $results);
53+
54+
self::assertSame('', $f->formatXref('com.example.only', $results));
55+
}
56+
57+
#[Test]
58+
public function formatXrefBuildsComponentModulePageAndResults(): void
59+
{
60+
$f = $this->makeFormatter();
61+
$results = [];
62+
63+
$xref = $f->formatXref('openehr_base_1.0.4.org.openehr.base.foundation_types.identification', $results);
64+
65+
self::assertSame('BASE:foundation_types:identification', $xref);
66+
self::assertSame(['BASE', 'foundation_types', 'identification'], $results);
67+
}
68+
69+
#[Test]
70+
public function formatXrefOmitsEmptySegmentsInOutput(): void
71+
{
72+
$f = $this->makeFormatter();
73+
$results = [];
74+
75+
$xref = $f->formatXref('x.org.openehr.base.foundation_types', $results);
76+
77+
self::assertSame('BASE:foundation_types', $xref);
78+
self::assertSame(['BASE', 'foundation_types', ''], $results);
79+
}
80+
}

tests/Unit/FilesystemTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit;
6+
7+
use OpenEHR\BmmPublisher\Helper\Filesystem;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use PHPUnit\Framework\TestCase;
10+
use Psr\Log\NullLogger;
11+
use RuntimeException;
12+
13+
final class FilesystemTest extends TestCase
14+
{
15+
private string $tempBase;
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
$this->tempBase = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'bmm_publisher_fs_test_' . uniqid('', true);
21+
self::assertTrue(mkdir($this->tempBase, 0700, true));
22+
}
23+
24+
protected function tearDown(): void
25+
{
26+
if (!is_dir($this->tempBase)) {
27+
parent::tearDown();
28+
29+
return;
30+
}
31+
$this->deleteTree($this->tempBase);
32+
parent::tearDown();
33+
}
34+
35+
private function deleteTree(string $dir): void
36+
{
37+
$items = @scandir($dir);
38+
if ($items === false) {
39+
return;
40+
}
41+
foreach (array_diff($items, ['.', '..']) as $item) {
42+
$path = $dir . DIRECTORY_SEPARATOR . $item;
43+
is_dir($path) && !is_link($path) ? $this->deleteTree($path) : @unlink($path);
44+
}
45+
@rmdir($dir);
46+
}
47+
48+
#[Test]
49+
public function assureDirCreatesNestedPath(): void
50+
{
51+
$dir = $this->tempBase . DIRECTORY_SEPARATOR . 'a' . DIRECTORY_SEPARATOR . 'b' . DIRECTORY_SEPARATOR . 'c';
52+
53+
Filesystem::assureDir($dir);
54+
55+
self::assertDirectoryExists($dir);
56+
self::assertTrue(is_writable($dir));
57+
}
58+
59+
#[Test]
60+
public function assureDirSucceedsWhenDirectoryAlreadyExists(): void
61+
{
62+
$dir = $this->tempBase . DIRECTORY_SEPARATOR . 'existing';
63+
self::assertTrue(mkdir($dir, 0700, true));
64+
65+
Filesystem::assureDir($dir);
66+
67+
self::assertDirectoryExists($dir);
68+
}
69+
70+
#[Test]
71+
public function assureDirThrowsWhenPathIsAFile(): void
72+
{
73+
$path = $this->tempBase . DIRECTORY_SEPARATOR . 'not_a_dir';
74+
self::assertSame(1, file_put_contents($path, 'x'));
75+
76+
$this->expectException(RuntimeException::class);
77+
$this->expectExceptionMessage('already exists but is not a directory');
78+
79+
Filesystem::assureDir($path);
80+
}
81+
82+
#[Test]
83+
public function writeFileWritesContent(): void
84+
{
85+
$file = $this->tempBase . DIRECTORY_SEPARATOR . 'out.txt';
86+
$content = "line1\nline2\n";
87+
88+
Filesystem::writeFile($file, $content, new NullLogger());
89+
90+
self::assertFileExists($file);
91+
self::assertSame($content, (string) file_get_contents($file));
92+
}
93+
}

0 commit comments

Comments
 (0)